nnspike.unit.etrobot

Classes

ETRobot([port])

Interface for communicating with LEGO SPIKE Prime robot over serial connection.

class nnspike.unit.etrobot.ETRobot(port='/dev/ttyACM0')[source]

Interface for communicating with LEGO SPIKE Prime robot over serial connection.

This class provides methods to send commands to and receive sensor data from a LEGO SPIKE Prime robot. It maintains a continuous background thread to receive sensor updates and handles motor commands.

Command IDs should be as same as (spike/slot_prod.py) the script in LEGO Spike Prime.

COMMAND_SET_MOTOR_FORWARD_SPEED_ID

Command ID for setting forward motor speed.

Type:

int

COMMAND_SET_MOTOR_BACKWARD_SPEED_ID

Command ID for setting backward motor speed.

Type:

int

COMMAND_SET_MOTOR_RELATIVE_POSITION_ID

Command ID for relative position movement.

Type:

int

COMMAND_STOP_MOTOR_ID

Command ID for stopping motors.

Type:

int

COMMAND_MOVE_ARM_ID

Command ID for moving robot arm.

Type:

int

CMD_FLAG

Command flag prefix for serial communication.

Type:

bytes

DUMMY

Dummy value for command parameters.

Type:

int

COMMAND_SET_MOTOR_FORWARD_SPEED_ID = 201
COMMAND_SET_MOTOR_BACKWARD_SPEED_ID = 202
COMMAND_SET_MOTOR_RELATIVE_POSITION_ID = 203
COMMAND_STOP_MOTOR_ID = 204
COMMAND_MOVE_ARM_ID = 205
CMD_FLAG = b'CF:'
DUMMY = 1
__init__(port='/dev/ttyACM0')[source]

Initialize the ETRobot with a serial connection.

Parameters:

port (str, optional) – The serial port to connect to. Defaults to “/dev/ttyACM0”.

receive()[source]

Update ETRobot motor and sensor status by the received data from the GPIO port.

Note

The update rate should be less than the rate of sending sensor data in LEGO Prime Hub (0.0005 seconds).

Return type:

None

get_spike_status()[source]

Get the spike status with last known good sensor values.

Returns:

Spike status object with consistent sensor data

Return type:

SpikeStatus

set_motor_relative_position(left_positon, right_position)[source]
Return type:

None

retrieve_motors_relative_position()[source]

Retrieve the relative positions of the motors.

Returns:

The sum of the absolute values of the relative positions of both motors.

Return type:

int

set_motor_speed(left_speed, right_speed)[source]

Set the ETRobot motor’s speed.

Parameters:
  • left_speed (int) – Left motor speed (-100-100).

  • right_speed (int) – Right motor speed (-100-100).

Return type:

None

brake()[source]

Brake the motors of the ETRobot.

Return type:

None

move_arm(action, duration=0.5)[source]

Move the arm up or down.

Parameters:

action (int) – Action to perform (0 = move up, 1 = move down, 2 = stop arm).

Return type:

None

stop()[source]

Stop the robot and close the serial port.

Return type:

None