SoccerboardJS
This module allows you to connect nearly any linux device to the qfix SoccerBoard and control it using the USB serial port and nodeJS.
You can install this module using npm install soccerboard
.
This module requires cu
to be installed. If it is not, please install it using apt-get install cu
.
It is also required to upload the C program stored in /sb/compiled/
to the SoccerBoard.
Make sure the program on the SoccerBoard is running while you are controlling it using SoccerboardJS.
Initialisation
To connect to the SoccerBoard you first have to find out to which serial port it is connected. For this you can use the scan(array ports, function callback)
function.
var sb = require('soccerboard');
sb.scan(['ttyUSB0','ttyUSB1','ttyUSB2','ttyUSB3'],function(ports){
//ports will be an array including all of the specified ports
//which are connected to an Soccerboard
});
If no active board was found, ports
will be false
.
If ports
is not false
, you can connect to the board using the init(string port, object callback)
function.
var sb = require('soccerboard');
sb.scan(['ttyUSB0','ttyUSB1','ttyUSB2','ttyUSB3'],function(ports){
if(ports){
sb.init(ports[0],{
'connect': function(connection){
//successfully connected
},
'close': function(exit_code){
//connection closed
},
'error': function(err){
//error
}
});
}
});
connect
is called after successfully connecting to the board and provides an connection
object which is needed for sending commands to the board.
close
is called when the connection to the board closes. It provides the exit code of the cu child process.
error
is optional and will be called if any error occurs while running cu.
Examples
var sb = require('soccerboard');
sb.scan(['ttyUSB0','ttyUSB1','ttyUSB2','ttyUSB3'],function(ports){
if(ports){
sb.init(ports[0],{
'connect': function(connection){
setInterval(function(){//get state of button 1 every 50 ms
sb.board.button(connection,1,function(pressed){
sb.board.led(connection,1,pressed);//set state of led 1 to the state of button 1
});
},50);
},
'close': function(exit_code){
process.exit();//exit process
},
'error': function(err){
console.log(err)//log error
}
});
}
});
Turn led 1
on if button 1
is pressed.
var sb = require('soccerboard');
sb.scan(['ttyUSB0','ttyUSB1','ttyUSB2','ttyUSB3'],function(ports){
if(ports){
sb.init(ports[0],{
'connect': function(connection){
sb.board.forward(connection,100);//start driving forward
setInterval(function(){//get value of analog sensor 0 every 50 ms
sb.board.analog(connection,0,function(value){
if(value > 90){//turning all motors off if the value value is greater than 90
sb.board.motorsOff(connection);
}
});
},50);
},
'close': function(exit_code){
process.exit();//exit process
},
'error': function(err){
console.log(err)//log error
}
});
}
});
Driving forward until the value of analog sensor 0
reaches 90
.
var sb = require('soccerboard');
sb.scan(['ttyUSB0','ttyUSB1','ttyUSB2','ttyUSB3'],function(ports){
if(ports){
sb.init(ports[0],{
'connect': function(connection){
sb.lcd.clear(connection);//clear display
sb.lcd.print(connection,'Hello World',0,0);//print 'Hello World' at position 0,0 to the display
},
'close': function(exit_code){
process.exit();//exit process
},
'error': function(err){
console.log(err)//log error
}
});
}
});
Printing Text to the lcd display.
Functions
In the following, conn
is the connection
object provided by the init
function.
sb.board.analog(object conn, int sensor, function callback)
...transmits a analog sensor value (int
) to the callback
function.
sensor
specifies an analog sensor port (0
- 7
).
sb.board.digital(object conn, int sensor, function callback)
...transmits a digital sensor value (bool
) to the callback
function.
sensor
specifies an digital sensor port (0
- 7
).
sb.board.button(object conn, int button, function callback)
...transmits the value of a button (bool
) to the callback
function.
button
specifies an button (0
- 1
).
sb.board.ledOn(object conn, int led)
...turns an led on.
led
specifies an led (0
- 1
).
sb.board.ledOff(object conn, int led)
...turns an led off.
led
specifies an led (0
- 1
).
sb.board.led(object conn, int led, bool state)
...sets the state of an led to state
.
led
specifies an led (0
- 1
).
sb.board.ledMeter(object conn, int value)
...displays an interger value using the leds. (Really pointless because there are only two of them.)
value
specifies an value between 0
and 255
.
sb.board.ledsOff(object conn)
...turns all leds off.
sb.board.motor(object conn, int motor, int speed)
...sets the speed of an motor to speed
(-255
- 255
).
motor
specifies an motor port (0
- 5
).
sb.board.motorsOff(object conn)
...turns all motors off.
sb.board.forward(object conn, int speed)
...sets speed of motor 0
and 1
to speed
.
sb.board.rotate(object conn, int speed)
...sets speed of motor 0
, 1
and 2
to speed
.
sb.board.powerOn(object conn, int output)
...turns an power output on.
output
specifies an power output.
sb.board.powerOff(object conn, int output)
...turns an power output off.
output
specifies an power output.
sb.board.power(object conn, int output, bool state)
...sets the state of an power output to state
.
output
specifies an power output.