Template reference (IN DEVELOPMENT)

advanced_game

class MyJoystick(io)

Bases: surrortg.inputs.joystick.Joystick

Advanced game documentation

async handle_coordinates(x, y, seat=0)

Coordinate based Joystick control

Middle position means x=0, y=0. Right means x is positive, left means x is negative. Top means y is positive, bottom means y is negative.

Parameters
  • x (float) – x-coordinate, between -1.0 and 1.0

  • y (float) – y-coordinate, between -1.0 and 1.0

  • seat (int) – Robot seat

async reset(seat=0)

Joystick reset functionality

Defaults to x=0, y=0

Parameters

seat (int) – Robot seat

class AdvancedGame

Bases: surrortg.game.Game

Advanced game documentation2

async on_init()

Initialize the game before connecting to the game engine

The input devices are registered here with a self.io.register_inputs({…}) call.

async on_config()

Do things before the game engine starts fetching new players

For example new inputs can be registered here if they change based on self.configs.

async on_prepare()

Do some preparations before the players connect

async on_pre_game()

Do some preparations after the players connect

The game won’t start before every robot has called self.io.send_pre_game_ready(), or maximum pre game time is reached. If the player inputs are needed here, they can be enabled with self.io.enable_inputs() call. On default all inputs are disabled here.

The return value of this method will set as the self._current_seat value (which is an integer).

async on_countdown()

Do things during the game countdown

async on_start()

Inputs are now enabled. Scores, laps and progress are counted.

self._current_seat is set to 0 by default, if it has not been set in on_pre_game().

The game engine will choose the winner based on the game type set on the admin panel. self.io.send_playing_ended() call can be used to signal the game engine that some seat has finished.

async on_finish()

Do something when the robot finishes the game

The game is finished for example when someone has enough score or laps or when the time is up. These rules can be set from the admin panel.

async on_exit(reason, exception)

This method is called just before the program exits

The exit reason code and exception are passed as parameters.

Exit reasons:

Code

Reason

-1

Unknown exit reason

0

Uncaught exception

1

Interrupted (SIGINT)

2

Terminated (SIGTERM)

3

Update (SIGUSR1)

Parameters
  • reason (int) – Reason code of the program exit.

  • exception (Exception/None) – Exception that caused the exit

car_game

class PwmActuator(pi, pin, delta_max=300, calibration=0)

Bases: surrortg.inputs.linear_actuator.LinearActuator

PwmActuator acts as controller for ESC and steering servo that take standard pwm servo input

It receives values -1 … 1 and maps that to matching pwm pulsewidth value where 0 input maps to 1500 pulsewidth. This correlates to middle position on servos, and 0 on a ESC.

Parameters
  • pi – instance of pigpio gpio control class

  • pin (int) – GPIO pin number to output the pwm signal

  • calibration – Pulsewidth value that modifies the middle position used for operating. Default value 0 does not alter the 1500 middle position. This value can be used to fine tune the steering servo so that the wheels of the car are straight with the middle value.

Param

delta_max: Maximum value of pulsewidth dirrence from the middle (1500) position. Default value of 300 translates to range of 1200 … 1800. Be careful with this value as overturning servo might cause physical damage to the servo or the device it is attached to.

Variables
  • middle – Middle position used for 0 input mapping. Defaults to 1500 and adds the calibration value to itself.

  • current_delta – Active value that is used to control the actuator. Defaults to half of delta_max, and can be modified with increase_delta and reduce_delta methods.

async drive_actuator(val, seat=0)

This function is called when input is received. It maps -1 … 1 value to corresponding pulsewidth value. For example value -1 translates to 1500 (middle) minus current_delta which would be 1350 with default values.

Parameters

val – float value from ranging -1 to 1. If keyboard input is used it will be -1, 0 or 1.

async increase_delta(inc)

Method that is called from ShiftGear class to increase throttle or steering value. Changing the value will take effect only, after the input is cycled off-on once (release and press key).

Parameters

inc – Amount to increase the current_delta value

Type

inc: int

async reduce_delta(inc)

Method that is called from ShiftGear class to decrease throttle or steering value. Changing the value will take effect only, after the input is cycled off-on once (release and press key).

Param

inc: Amount to decrease the current_delta value

Type

inc: int

class ShiftGear(actuator, inc=10)

Bases: surrortg.inputs.linear_actuator.LinearActuator

Virtual actuator class that implements shifting gears or adjusting the amount of steering.

Param

actuator: Instance of PwmActuator to be modified

Param

inc: Amount of pulsewidth to modify with each shifting. Defaults to 10.

Type

inc: Int

async drive_actuator(val, seat=0)

Driving this virtual actuator increases the value of the connected actuator if the input is positive value, and decreases the value if it is negative. Off position (0) does nothing.

class CarGame

Bases: surrortg.game.Game

async on_init()

First initialize the pigpio class. For this you have to have pigpio installed on your system and the pigpiod daemon running!

async on_exit(reason, exception)

This method is called just before the program exits

The exit reason code and exception are passed as parameters.

Exit reasons:

Code

Reason

-1

Unknown exit reason

0

Uncaught exception

1

Interrupted (SIGINT)

2

Terminated (SIGTERM)

3

Update (SIGUSR1)

Parameters
  • reason (int) – Reason code of the program exit.

  • exception (Exception/None) – Exception that caused the exit

simple_game

class MySwitch

Bases: surrortg.inputs.switch.Switch

async on(seat=0)

Switch turned on functionality

Parameters

seat (int) – Robot seat

async off(seat=0)

Switch turned off functionality

Parameters

seat (int) – Robot seat

class MyJoystick

Bases: surrortg.inputs.joystick.Joystick

async handle_coordinates(x, y, seat=0)

Coordinate based Joystick control

Middle position means x=0, y=0. Right means x is positive, left means x is negative. Top means y is positive, bottom means y is negative.

Parameters
  • x (float) – x-coordinate, between -1.0 and 1.0

  • y (float) – y-coordinate, between -1.0 and 1.0

  • seat (int) – Robot seat

async reset(seat=0)

Joystick reset functionality

Defaults to x=0, y=0

Parameters

seat (int) – Robot seat

class SimpleGame

Bases: surrortg.game.Game

async on_init()

Initialize the game before connecting to the game engine

The input devices are registered here with a self.io.register_inputs({…}) call.

async on_start()

Simple game simulates a 3 player game where timestamp scores are sent for each seat and the game is ended with the final score parameter in the third send score method.

convert_to_ms(time)

When a game is using timestamp scores, the controller must provide the scores as milliseconds. This is a simple conversion function that converts seconds to milliseconds.

Parameters

time – Time in seconds

Returns

Time in milliseconds