Introduction
Welcome to the LocoRobo API.
LocoRobo
Setup API
from locorobo import LocoRobo
# OSX/Linux
LocoRobo.setup('/dev/tty.usbmodem1')
# Windows
LocoRobo.setup('COM4')
var LocoRobo = require('locorobo');
// OSX/Linux
LocoRobo.setup('/dev/tty.usbmodem1')
// Windows
LocoRobo.setup('COM4')
// OSX/Linux
LR = LocoRobo('/dev/tty.usbmodem1')
// Windows
LR = LocoRobo('COM4')
Argument | Description |
---|---|
Serial Port / Address | Serialport of the LocoRobo Dongle |
Scanning for Robots
robots = LocoRobo.scan(duration=2)
robot = robots['80:105:163:128:7:0']
// startScanning returns a promise that is resolved after Scan Time argument.
LocoRobo.scan(2000)
.then(function(robots){
const robot = robots['80:105:163:128:7:0'];
})
robots = LR.scan(2000)
% Matlab differs from others here because the mac address is in hex, not dec
robot = robots('50:69:A3:80:07:00')
Argument | Description |
---|---|
Scan Time | How many milliseconds to scan for before returning |
Returns
Type | Description |
---|---|
Map<String, Robot> | Map of found Robots where the key is the mac address |
Robot Methods
Connect
robot.connect()
robot.connect();
robot = robot.connect();
Returns when robot is connected. Throws error if connection times out
Disconnect
robot.disconnect()
robot.disconnect();
robot = robot.disconnect();
Returns when robot is disconnected
Activate
robot.activate_motors()
robot.activateMotors();
robot.activateMotors();
Enables the robots motors
Deactivate
robot.deactivate_motors()
robot.deactivateMotors();
robot.deactivateMotors();
Disables the robots motors
Move
from locorobo import MotorDirection
robot.move(MotorDirection.FORWARD, MotorDirection.BACKWARD, 1, 1)
var LocoRobo = require('locorobo');
var MotorDirection = LocoRobo.MotorDirection;
robot.move(MotorDirection.FORWARD, MotorDirection.BACKWARD, 1, 1);
cc = ControlCodes();
robot.move(cc.MotorDirection.FORWARD, cc.MotorDirection.BACKWARD, 1, 1);
Argument | Description |
---|---|
MotorDirection | Left motor direction |
MotorDirection | Right motor direction |
MotorSpeed | Left motor speed. Float range 0 to 1 |
MotorSpeed | Right motor speed. Float range 0 to 1 |
Use Wait | Optional Boolean, True to use wait, False to not use wait setup by setup wait |
MotorDirection | Description |
---|---|
FORWARD | Forward Motor Direction |
BACKWARD | Backward Motor Direction |
Setup Wait
Use with Move command to tell the robot to move until a sensor condition is met.
from locorobo import WaitType
from locorobo import MotorDirection
# Move forward at full speed for 1 second
robot.setup_wait(WaitType.TIME, 1000)
robot.move(MotorDirection.FORWARD, MotorDirection.FORWARD, 1.000000, 1.000000, True)
var LocoRobo = require('locorobo');
var MotorDirection = LocoRobo.MotorDirection;
var Data = LocoRobo.Data;
// Move forward at full speed for 1 second
robot.setupWait(WaitType.TIME, 1000)
.then(function() {
return robot.move(MotorDirection.FORWARD, MotorDirection.FORWARD, 1.000000, 1.000000, true);
})
cc = ControlCodes();
% Move forward at full speed for 1 second
robot = robot.setupWait(cc.WaitType.TIME, 1000)
robot.move(cc.MotorDirection.FORWARD, cc.MotorDirection.FORWARD, 1.000000, 1.000000, true)
Argument | Description |
---|---|
WaitType | Wait to use |
WaitType | Description | Input Unit |
---|---|---|
TIME | Time based sensor | Milliseconds |
ULTRASONIC_GREATER_THAN | Sensor waits until ultrasonic value is greater than | Centimeters * 1000 |
ULTRASONIC_LESS_THAN | Sensor waits until ultrasonic value is less than | Centimeters * 1000 |
SONG | Sensor waits until ultrasonic value is less than | None |
DISTANCE | Sensor waits until robot has travelled distance | Centimeters * 1000 |
ROTATION | Sensor waits until robot has rotated by angle | Degrees * 1000 |
Enable Sensor
Enable/disable the robot sending sensor data to the api in the background. Calling this is required for getSensorValue to work
from locorobo import Data
robot.enable_sensor(Data.ULTRASONIC, True)
robot.enable_sensor(Data.ULTRASONIC_RAW, True)
robot.enable_sensor(Data.ACCELEROMETER, True)
robot.enable_sensor(Data.ACCELEROMETER_RAW, True)
robot.enable_sensor(Data.GYROSCOPE_RAW, True)
robot.enable_sensor(Data.RUNNING_ENCODERS, True)
robot.enable_sensor(Data.TEMPERATURE, True)
LocoRobo.wait(2.0)
var LocoRobo = require('locorobo');
var Data = LocoRobo.Data;
robot.enableSensor(Data.ULTRASONIC, true)
robot.enableSensor(Data.ULTRASONIC_RAW, true)
robot.enableSensor(Data.ACCELEROMETER, true)
robot.enableSensor(Data.ACCELEROMETER_RAW, true)
robot.enableSensor(Data.GYROSCOPE_RAW, true)
robot.enableSensor(Data.RUNNING_ENCODERS, true)
robot.enableSensor(Data.TEMPERATURE, true)
LocoRobo.wait(2.0)
Sensor |
---|
ULTRASONIC |
ULTRASONIC_RAW |
ACCELEROMETER |
ACCELEROMETER_RAW |
GYROSCOPE_RAW |
RUNNING_ENCODERS |
TEMPERATURE |
Get Sensor Value
from locorobo import Data
robot.enable_sensor(Data.ULTRASONIC, True)
robot.enable_sensor(Data.ULTRASONIC_RAW, True)
robot.enable_sensor(Data.ACCELEROMETER, True)
robot.enable_sensor(Data.ACCELEROMETER_RAW, True)
robot.enable_sensor(Data.GYROSCOPE_RAW, True)
robot.enable_sensor(Data.RUNNING_ENCODERS, True)
robot.enable_sensor(Data.TEMPERATURE, True)
LocoRobo.wait(2.0)
distance = robot.get_sensor_value(Data.ULTRASONIC)
distance = robot.get_sensor_value(Data.ULTRASONIC_RAW)
x, y, z = robot.get_sensor_value(Data.ACCELEROMETER)
x, y, z = robot.get_sensor_value(Data.ACCELEROMETER_RAW)
x, y, z = robot.get_sensor_value(Data.GYROSCOPE_RAW)
tickcounts = robot.get_sensor_value(Data.RUNNING_ENCODERS)
temperature = robot.get_sensor_value(Data.TEMPERATURE)
left = tickcounts['left']
right = tickcounts['right']
var LocoRobo = require('locorobo');
var Data = LocoRobo.controlCodes.Data;
robot.enableSensor(Data.ULTRASONIC, true)
robot.enableSensor(Data.ULTRASONIC_RAW, true)
robot.enableSensor(Data.ACCELEROMETER, true)
robot.enableSensor(Data.ACCELEROMETER_RAW, true)
robot.enableSensor(Data.GYROSCOPE_RAW, true)
robot.enableSensor(Data.RUNNING_ENCODERS, true)
robot.enableSensor(Data.TEMPERATURE, true)
LocoRobo.wait(2.0)
var distance = robot.getSensorValue(Data.ULTRASONIC);
var distance = robot.getSensorValue(Data.ULTRASONIC_RAW);
var result = robot.getSensorValue(Data.ACCELEROMETER);
var result = robot.getSensorValue(Data.ACCELEROMETER_RAW);
var result = robot.getSensorValue(Data.GYROSCOPE_RAW);
var encoderResult = robot.getSensorValue(Data.RUNNING_ENCODERS);
var temperature = robot.getSensorValue(Data.TEMPERATURE);
var x = result[0];
var y = result[1];
var z = result[2];
const left = encoderResult.left
const right = encoderResult.right
cc = ControlCodes();
distance = robot.getSensorValue(cc.Data.ULTRASONIC);
distance = robot.getSensorValue(Data.ULTRASONIC_RAW);
result = robot.getSensorValue(cc.Data.ACCELEROMETER);
result = robot.getSensorValue(Data.ACCELEROMETER_RAW);
result = robot.getSensorValue(Data.GYROSCOPE_RAW);
encoderResult = robot.getSensorValue(Data.RUNNING_ENCODERS);
temperature = robot.getSensorValue(Data.TEMPERATURE);
x = result(0);
y = result(1);
z = result(2);
enableSensor must be called before using getSensorValue except on Matlab
Ultrasonic units are centimeters, Accelerometer units are g, Gyroscope units are radians/second, Encoder units are ticks, Temperature units are Celcius.
Returns the sensor value for sensor
Sensor | Return Value / Type |
---|---|
ULTRASONIC | Integer |
ULTRASONIC_RAW | Integer |
ACCELEROMETER | Array of [X, Y, Z] axis values |
ACCELEROMETER_RAW | Array of [X, Y, Z] axis values |
GYROSCOPE_RAW | Array of angle values |
RUNNING_ENCODERS | Map of {left: number, right: number} values of running encoder ticks |
TEMPERATURE | Temperature in c |
Set Light
// LED Ring
robot.set_light(0, 255, 0, 255)
// LED Ring
robot.setLight(0, 255, 0, 255);
// LED Ring
robot.setLight(0, 255, 0, 255);
Light color does not get applied until Sync Lights is called
LED Ring Arguments
Argument | Description |
---|---|
Light Index | Light index to set |
Red | 0-255 Intensity of color |
Green | 0-255 Intensity of color |
Blue | 0-255 Intensity of color |
Sync Light
robot.set_light(0, 255, 0, 255)
robot.set_light(1, 200, 0, 255)
robot.set_light(2, 180, 0, 255)
robot.sync_lights()
robot.setLight(0, 255, 0, 255);
robot.setLight(1, 200, 0, 255);
robot.setLight(2, 180, 0, 255);
robot.syncLights();
robot.setLight(0, 255, 0, 255);
robot.setLight(1, 200, 0, 255);
robot.setLight(2, 180, 0, 255);
robot.syncLights();
Applies light color changes set by Set Light
Play Note
# Play note and don't wait
robot.play_note(Note.C, 1000, False)
# Play Note and wait
robot.setup_wait(WaitType.SONG)
robot.play_note(Note.C, 1000, True)
// Play note and don't wait
robot.playNote(cc.Note.C, 1000, false)
// Play Note and wait
robot.setupWait(cc.WaitType.SONG)
robot.playNote(cc.Note.C, 1000, true)
% Play note and don't wait
robot.playNote(cc.Note.C, 1000, false)
% Play Note and wait
robot.setupWait(cc.WaitType.SONG)
robot.playNote(cc.Note.C, 1000, true)
playNote plays a note, for a duration, and either waits until the note is done executing to continue with the next instruction, or does not wait and keeps going.
Notes
Note | Value |
---|---|
C | 0 |
CSharp | 1 |
D | 2 |
EFlat | 3 |
E | 4 |
F | 5 |
FSharp | 6 |
G | 7 |
GSharp | 8 |
A | 9 |
BFlat | 10 |
B | 11 |
Play Song
# Play song and don't wait
robot.play_song(Song.StarWars, False)
# Play Song and wait
robot.setup_wait(WaitType.SONG)
robot.play_song(Song.StarWars, True)
// Play song and don't wait
robot.playSong(cc.Song.StarWars, false)
// Play Song and wait
robot.setupWait(cc.WaitType.SONG)
robot.playSong(cc.Song.StarWars, true)
% Play song and don't wait
robot.playSong(cc.Song.StarWars, false)
% Play Song and wait
robot.setupWait(cc.WaitType.SONG)
robot.playSong(cc.Song.StarWars, true)
playSong plays a song, for a duration, and either waits until the song is done executing to continue with the next instruction, or does not wait and keeps going.
Song | Value |
---|---|
StarWars | 0 |
Rocky | 1 |
EyeOfTheTiger | 2 |
Mario | 3 |
Pokemon | 4 |
Constants
MotorDirection | Description |
---|---|
FORWARD | Forward Motor Direction |
BACKWARD | Backward Motor Direction |
WaitType | Description | Input Unit |
---|---|---|
TIME | Time based sensor | Milliseconds |
ULTRASONIC_GREATER_THAN | Sensor waits until ultrasonic value is greater than | Centimeters * 1000 |
ULTRASONIC_LESS_THAN | Sensor waits until ultrasonic value is less than | Centimeters * 1000 |
SONG | Sensor waits until ultrasonic value is less than | None |
DISTANCE | Sensor waits until robot has travelled distance | Centimeters * 1000 |
ROTATION | Sensor waits until robot has rotated by angle | Degrees * 1000 |
Sensor |
---|
ULTRASONIC |
ULTRASONIC_RAW |
ACCELEROMETER |
ACCELEROMETER_RAW |
GYROSCOPE_RAW |
RUNNING_ENCODERS |
TEMPERATURE |