Sniff is a "Scratch-like" programming language that's designed to help Scratchers move gently from Scratch to more conventional languages. They can start writing programs, without having to learn a new language because Sniff is based on Scratch. They learn a little more about variables, compiling, syntax errors (!), and they can have fun controlling real hardware while they're doing it.

Monday, 21 July 2014

SniffBot Jr - with IR control

SniffBot has been a big hit at various STEM events, with kids of all ages having fun driving it. As a follow on, we're developing a kit we can use as the basis for a workshop so participants can actually build their own robot and start programming it.

Part of the challenge of this is COST. Sniffbot is super cheap, but the RC controller it uses isn't. While RC is by far the best way to drive a robot like this, we're hoping to put together a complete system that can do everything the full Sniffbot can do for less that £25. That includes all of the mechanical and electronic components for a programmable, remote control robot (batteries not included, but that's another story!). The RC kit I generally use costs about £60 for the controller, and £15 for the receiver. That's still within reasonable budget if you want to buy one yourself, but not if we're building 5 for a workshop...

IR control is cheap and easy. An IR receiver costs about £1, and for the transmitter you can use any old one one you have around - doesn't everyone have a draw full of them. However if you do need to buy a new one then they're again only about £1.

We can reuse some of the original SniffBot code to drive the motors:

##### NOW CONTROL THE MOTORS #####
make leftDrive number
make rightDrive number

make motorController device
make motor1 number
make motor2 number
make motor3 number
make motor4 number

when updateMotors
.set motor3 to leftDrive
.set motor4 to rightDrive

.tell motorController to "update"


SniffBot Jr will have only 2 driven wheels, so this code isn't really necessary, but it means we can use the same code for both the 2 and 4wd drive versions: set leftDrive and rightDrive and then call updateMotors. It's important to use motor3 for left and motor4 for right, as the timers used by the IR receiver clash with PWM on the M1 and M2 outputs of the Motor Shield.

To receive IR commands we need a receiver which demodulates the signal, such as the TSOP4838. These have three pins: Vcc, Gnd and signal, so they can easily be wired to plug into the servo pins on the Arduino motor controller shield (which appear as inputs D9 and 10).



To use the IR we just need to make an IR device and tell it to read:
make receiveIR device 9
make keyPressed number

when start
.forever
..tell receiveIR to "read"
..if keyPressed > 0
...say [ keyPressed ]



Calling read stores a unique number for the pressed key in the keyPressed variable. If no key is pressed then it has the value 0. Depending on the controller the value of 1 might be used to indicate that the previously pressed key is still being held down.

There are several standards and protocols that IR keys can and should use but to keep things simple in Sniff they're set up so that more or less any key press should return a quasi-unique value. If you run the above code you can test your controller and find out what numbers it generates. Then we can use the values we get to control the motors on the robot:

when start
.forever
..tell receiveIR to "read"
..
..if keyPressed = 55335
...set leftDrive to 1
...set rightDrive to 1
...broadcast updateMotors and wait
..
..if keyPressed = 22695
...set leftDrive to -1
...set rightDrive to -1
...broadcast updateMotors and wait
..
..if keyPressed = 8415
...set leftDrive to -1
...set rightDrive to 1
...broadcast updateMotors and wait
..
..if keyPressed = 24735
...set leftDrive to 1
...set rightDrive to -1
...broadcast updateMotors and wait
..
..if keyPressed = 41055
...set leftDrive to 0
...set rightDrive to 0

...broadcast updateMotors and wait


These values are for a spare, unbranded IR remote I happened to have. I used the previous code to figure out the number for each button I wanted to use, and dropped them in to make a forward, backwards, left, right and stop control.

This is the most basic control you could have, but it could easily be extended so that holding/repeatedly pressing accelerates or slows the robot, and/or that left/right steer, rather than just putting the thing into an on-the-spot spin. However the important thing is we now have control over our robot in a way that's cheap enough to use with a workshop of people.

No comments:

Post a Comment