API Reference
Sensor API
Read sensor data from your State classes. The Engine handles polling at 1000Hz so you don't have to.
Sensing Methods
These methods are available on any class that inherits from kenate.BaseState.
| Method | Returns |
|---|---|
| get_distance(sensor_id) | Distance in centimeters (float) |
| get_encoder(motor_id) | Raw encoder ticks (int) |
| get_battery_voltage() | System voltage (float) |
| get_imu_heading() | Compass heading 0-360 (float) |
get_distance(sensor_id)
Returns distance from an ultrasonic or IR sensor. Units are centimeters because we're civilized.
def on_update(self):
front = self.get_distance(0) # Front sensor
if front < 30:
self.change_state("Avoid")
else:
self.set_motor_speed(0, 50)get_encoder(motor_id)
Returns raw encoder ticks. Use for odometry or PID control. The numbers go up when wheels spin.
def on_enter(self):
self.start_ticks = self.get_encoder(0)
def on_update(self):
# Move forward 1000 ticks
current = self.get_encoder(0)
if current - self.start_ticks >= 1000:
self.change_state("Done")
else:
self.set_motor_speed(0, 50)get_battery_voltage()
Returns system voltage. Useful for low-battery warnings. Nobody likes a dead robot.
def on_update(self):
voltage = self.get_battery_voltage()
if voltage < 11.0:
self.log("LOW BATTERY!")
self.change_state("ReturnToBase")get_imu_heading()
Returns compass heading from IMU (0-360 degrees). North is 0. East is 90. You get the idea.
def on_update(self):
heading = self.get_imu_heading()
# Turn to face North (0 degrees)
error = heading - 0
self.set_motor_speed(0, error * 0.5)
self.set_motor_speed(1, -error * 0.5)