C3Pi - the Raspberry Pi meets Teensy 3.0
Teensy 3.0 |
Yesterday I started to set up the C3Pi's new brains - the Raspberry Pi model B+, with its lower power requirements, extra mounting holes and four USB ports. The USB ports are really useful; while I'm developing the new software, I need to connect a keyboards and mouse, a wifi dongle and the Teensy 3.0 which will be used for motor control and other tasks.
Teensies rule
If you haven't come across the Teensies they are Arduino-compatible miniature boards developed by Paul Stoffregen. The early Teensies used ATmega chips, but the Teesny 3.0 and 3.1 use ARM processors. Paul has done an amazing job of extending the Arduino IDE and porting many of the libraries to work with the ARM chips. As a result the Teensy family are just as developer-friendly as the original Arduinos.The main attraction of the Teensy 3.0 is that it's faster, has more I/O, and much more RAM than the Arduino Pro. The fact that the Teensy runs at 3.3 volts is an added bonus, since it's safe to connect GPIO pins directly to the Pi.
Using a Serial USB link
At present, though, I'm using a simple USB connection between the Pi and the Teensy.C3Pi previously used I2C to link the Pi and an Arduino Pro Mini. That worked, but there are a number of attractions to using a serial link, including physical reliability. The previous C3Pi wiring was based on jumper leads and was prone to intermittent failure, usually during demos. The USB connection should be much more robust.
Checking it out
My first job today was to verify that I could- program the Teensy 3.0 from my workstation
- powered the Teesny from one of the Raspberry Pi's USB ports
- establish two-way Serial communications between the workstation the Teensy
- establish two-way communications between the Teensy and the Pi.
The eagle-eyed will notice that I take baby steps, introducing one change at a time. I love to work that way: progress is slow, but it's steady.
The first snag: serialEvent
The first two checks went fine, but the third test hit an unexpected problem.
I'd decided to use a bit of sample code from my Life after Blink workshop. One of the experiments ends up with a simple fortune cookie server running on the Arduino. You can see a snippet of code below.
void loop() {
// print the string when a newline arrives:
if (reply) {
int index = (int) random(MAX);
Serial.println(greetings[index]);
reply = false;
}
}
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char c = (char) Serial.read();
if (c == '\n') {
reply = true;
}
}
}
I uploaded the full sketch to the Teensy and opened the Arduino serial terminal. It locked up.
After a series of experiments, I found that serial comms was working fine but serialEvent does not seem to be supported on the Teensy. When I folded the body of serialEvent into my loop the code worked perfectly.
void loop() {
// print the string when a newline arrives:
while (Serial.available()) {
// get the new byte:
char c = (char) Serial.read();
if (c == '\n') {
reply = true;
}
}
if (reply) {
int index = (int) random(MAX);
Serial.println(greetings[index]);
reply = false;
}
}
What next?
My next step will be to connect the Teensy to the Pi and see if I can drive the cookie server on the Teensy using a terminal on the Pi.Watch this space!
Comments
Post a Comment