Robot Reference
kevinbotlib.robot
kevinbotlib.robot.BaseRobot
estop_hooks = []
class-attribute
instance-attribute
IS_SIM = False
class-attribute
instance-attribute
telemetry = Logger()
instance-attribute
simulator = None
instance-attribute
fileserver = FileServer(LoggerDirectories.get_logger_directory(), host='0.0.0.0')
instance-attribute
comm_client = RedisCommClient(port=serve_port, unix_socket=serve_unix_socket)
instance-attribute
log_sender = ANSILogSender(self.telemetry, self.comm_client, self._ctrl_logs_key)
instance-attribute
metrics
property
enabled
property
writable
opmode
property
writable
opmodes
property
Get the list of opmodes supported by the robot.
Returns:
Type | Description |
---|---|
list[str]
|
Opmodes |
current_cps
property
Get the current cycles per second of the robot.
Returns:
Type | Description |
---|---|
float
|
Cycles/sec |
add_basic_metrics(robot, update_interval=2.0)
staticmethod
add_battery(robot, min_voltage, max_voltage, source)
staticmethod
register_estop_hook(hook)
staticmethod
__init__(opmodes, serve_port=6379, serve_unix_socket='/tmp/kevinbotlib.redis.sock', log_level=Level.INFO, print_level=Level.INFO, default_opmode=None, cycle_time=250, log_cleanup_timer=10.0, metrics_publish_timer=5.0, battery_publish_timer=0.1, robot_heartbeat_interval=1.0, robot_heartbeat_expiry=2.5, *, enable_stderr_logger=False, allow_enable_without_console=False)
Initialize the robot
Parameters:
Name | Type | Description | Default |
---|---|---|---|
opmodes
|
list[str]
|
List of operational mode names. |
required |
serve_port
|
int
|
Port for comm server. Shouldn't have to be changed in most cases. Defaults to 8765. |
6379
|
serve_unix_socket
|
str
|
Unix socket for comm server. Unix socket will be preferred over networked connection. |
'/tmp/kevinbotlib.redis.sock'
|
log_level
|
Level
|
Level to logging. Defaults to Level.INFO. |
INFO
|
print_level
|
Level
|
Level for print statement redirector. Defaults to Level.INFO. |
INFO
|
enable_stderr_logger
|
bool
|
Enable logging to STDERR, may cause issues when using signal stop. Defaults to False. |
False
|
default_opmode
|
str
|
Default Operational Mode to start in. Defaults to the first item of |
None
|
cycle_time
|
float
|
How fast to run periodic functions in Hz. Defaults to 250. |
250
|
log_cleanup_timer
|
float
|
How often to clean up logs in seconds. Set to 0 to disable log cleanup. Defaults to 10.0. |
10.0
|
metrics_publish_timer
|
float
|
How often to publish system metrics. This is separate from |
5.0
|
battery_publish_timer
|
float
|
How often to publish battery voltages. Set to 0 to disable battery publishing. Defaults to 0.1. |
0.1
|
robot_heartbeat_interval
|
float
|
How often to send a heartbeat to the control console. Defaults to 1.0. |
1.0
|
robot_heartbeat_expiry
|
float
|
How long the robot heartbeat will stay valid. Must be longer than robot_heartbeat_interval. Defaults to 2.0. |
2.5
|
allow_enable_without_console
|
bool
|
Allow the robot to be enabled without an active control console. Defaults to False. |
False
|
run()
Run the robot loop and parse command-line arguments. Method is final.
robot_start()
Run after the robot is initialized
robot_end()
Runs before the robot is requested to stop via service or keyboard interrupt
robot_periodic(opmode, enabled)
Periodically runs every robot cycle
Parameters:
Name | Type | Description | Default |
---|---|---|---|
opmode
|
str
|
The current OpMode |
required |
enabled
|
bool
|
WHether the robot is enabled in this opmode |
required |
opmode_init(opmode, enabled)
Runs when entering an opmode state (either enabled or disabled)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
opmode
|
str
|
The OpMode being entered |
required |
enabled
|
bool
|
Whether the robot is enabled in this opmode |
required |
opmode_exit(opmode, enabled)
Runs when exiting an opmode state (either enabled or disabled)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
opmode
|
str
|
The OpMode being exited |
required |
enabled
|
bool
|
Whether the robot was enabled in this opmode |
required |
estop()
Immediately trigger an emergency stop.
kevinbotlib.robot.InstanceLocker
Generate and release a lockfile for an entire application. Useful when trying to prevent multiple instances of an app.
Verifies if the application was killed without releasing the lockfile.
lockfile_name = lockfile_name
instance-attribute
pid = os.getpid()
instance-attribute
__init__(lockfile_name)
Initialize the InstanceLocker
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lockfile_name
|
str
|
The name of the lockfile (e.g., 'robot.lock'). |
required |
lock()
Attempt to acquire the lock by creating a lockfile with the current PID.
Returns:
Type | Description |
---|---|
bool
|
True if the lock was successfully acquired, False if another instance is running. |
unlock()
Release the lock by removing the lockfile.
is_locked(lockfile_name)
staticmethod
Check if the lockfile exists and corresponds to a running process.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lockfile_name
|
str
|
The name of the lockfile to check. |
required |
Returns:
Type | Description |
---|---|
int
|
-1 if not locked, PID of locking process |