Pages

Wednesday, 13 August 2014

C3Pi: The Teensy and Motor Driver are talking

C3Pi Teensy 3.0 and Motor Driver
At the end of the previous post I let myself get a bit carried away!

C3Pi has always used a Solarbotics motor driver. In the past it's been controlled by an Arduino Pro Mini.

While the software for the Teensy is very similar, I needed to make a couple of changes to the code and more significant changes to the pinout.

Slow and steady

So my rush to solder up some stripbaord was premature. I've breadboarded the circuit instead.

I've now got the Teesny 3.0 controlling it, and the Teensy is controlled by serial input.

Since I've already checked that the Pi can drive the Teensy's serial port, I'm ready to go.

Tomorrow's plan

Tomorrow I will prepare the stripboard, connect the motors and take C3PI for his first outing. Initially I will control C3Pi via ssh over wifi.

Come back tomorrow to find out how the project is going. If you want to meet C3Pi in person, come along to this Saturday's Southend Raspberry Jam. There will be lost of other exhibits and talks, and it's going to be a lot of fun.

Book your free tickets via Eventbrite.


C3PI: the Pi and Teensy are talking



The Pi and the Teensy are talking.

The Teensy is running the fortune cookie sketch. I've been using pyserial on the Pi, and using interactive python.

Here's a screenshot of an ssh session on the Pi:


Next Step

Next I'm going to solder up a mini-stripboard for the Teensy.

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
  1. program the Teensy 3.0 from my workstation
  2. powered the Teesny from one of the Raspberry Pi's USB ports
  3. establish two-way Serial communications between the workstation the Teensy
  4. 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!