Skip to content

Simulation Framework

sim-framework-marketing-image.png

The KevinbotLib Simulation Framework allows for the testing of robot code before deployment.

Architecture

simulator-diagram-light.svg simulator-diagram-dark.svg

Built-in Simulator WindowViews

Process Time

procinfo.png

Display Process robot process running time, and robot cycle time

Telemetry

telemetry.png

Display logs in real-time

Metrics

metrics.png

Display Robot Metrics

Robot State

state.png

Set the current OpMode, Enable, Disable, or E-Stop the robot

Additional Simulator WindowViews

Serial Devices

serial.png

Display each used Serial Interface in a tab

View more info here

Using the Simulator Within a Robot

BaseRobot.run() will automatically detect the --simulate command line flag.

The simulator can be detected at runtime using the BaseRobot.IS_SIM attribute, or if BaseRobot.simulator is a SimulationFramework class.

Examples

Any robot that correctly extends BaseRobot is capable of running the simulator by using the --simulate command-line flag.

examples/robot/robot.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from kevinbotlib.logger import Level
from kevinbotlib.robot import BaseRobot


class DemoRobot(BaseRobot):
    def __init__(self):
        super().__init__(
            opmodes=[
                "TestOp1",
                "TestOp2",
                "TestOp3",
                "TestOp4",
            ],  # robot's operational modes
            log_level=Level.TRACE,  # lowset logging level
            enable_stderr_logger=True,
            cycle_time=20,  # loop our robot code 20x per second - it is recommended to run much higher in practice
            metrics_publish_timer=0,  # the test robot doesn't use metrics - see the metrics_robot.py example for a metrics usage example
        )

    def robot_start(self) -> None:  # runs once as the robot starts
        super().robot_start()
        print(
            "Starting robot..."
        )  # print statements are redirected to the KevinbotLib logging system - please don't do this in production

    def robot_periodic(self, opmode: str, enabled: bool) -> None:
        super().robot_periodic(opmode, enabled)

        print(f"OpMode {'enabled' if enabled else 'disabled'}... {opmode}")

    def opmode_init(self, opmode: str, enabled: bool) -> None:
        super().opmode_init(opmode, enabled)

        print(f"OpMode {'enabled' if enabled else 'disabled'} init... {opmode}")

    def opmode_exit(self, opmode: str, enabled: bool) -> None:
        super().opmode_exit(opmode, enabled)

        print(f"OpMode {'enabled' if enabled else 'disabled'} exit... {opmode}")

    def robot_end(self) -> None:  # runs as the robot propares to shutdown
        super().robot_end()
        print("Ending robot...")


if __name__ == "__main__":
    DemoRobot().run()

Run

python robot.py --simulate

See Also

Simulation Framework Reference