Close

Using Teensy 3.1

A project log for Tote

Affordable spider robot

dehipudeʃhipu 07/02/2015 at 13:490 Comments

One of the topics I intend to cover in those logs is how to extend Tote with various boards and modules. I've already had one log on how to use Pro Micro instead of Pro Mini, and one on adding an ESP8266 board with Micropython. Now it's time for the Teensy 3.1.

The Teensy 3.1 is an ARM board very similar in shape to the Pro Mini, but with much more computing power and lots of pins, both digital and analog. It's really a great choice if you have outgrown the Pro Mini. You can program it just like Arduino, using the Teensyduino IDE. So I figured my code should just work, right? Wrong.

It actually took me almost 2 weeks to get my code to work. Here's the story.

Connecting the Teensy 3.1 is a little bit tricky. Although the pin pitch is right, you can't just plug it into the Pro Mini socket on the Tote, because the GND pin has been moved to the other side of the board. Sigh. That's fine, though, you can just use short wires, or, if you have a socket, a set of extra-long pin headers and some Dupont cables:

Then it simply plugs into the Tote socket. Looks a little bit weird, but should work just fine:

Now just adjust the pin numbers in servos.ino, remove the IR code (it's based on AVR interrupts, so probably won't work on Teensy), and we are ready to go. Another super-easy conversion.

Except that it doesn't work. As soon as the robot is witched on, it gets an attack of epilepsy, twitching its legs randomly. Why? Why?

Now, the "fun" part of the project started. I began with the absolute low-level, checking first if the Servo library example works. It worked correctly. Then I proceeded to debug the servos.ino code, making sure that each servo really moves to the angle it has been told to move to. Everything seems to be in order. Hmph.

That means that the error must be somewhere in the inverse kinematics code. I was afraid of that. But the code works perfectly fine on the Pro Mini and other AVR-based Arduinos. So what could be wrong?

Someone told me that the abs() funtion may be not defined for floating point numbers, so I replaced it with a call to max(a, -a). No joy. Someone else told me that the AVR doesn't really have "double" type, and that it is in fact just normal "float". So I tried changing all types to "float". Nope. Then changing all the trigonometric functions to their float versions. No. Verifying all loops for buffer overruns and adding sentinel conditions checking for correct ranges. Still nothing.

Finally, the time has come to disconnect the Teensy 3.1 from Tote and do some serious debugging over the serial. After placing a lot of strategically planned print statements, a pattern started to emerge. It seemed that sometimes a value would suddenly be multiplied by 255. How come? Add more print statement to also display the constants and tables I used, and I found the problem.

Turns out that the char type is unsigned by default on ARM. And I had some tables with just values like 1 and -1 for tuning various parameters. And of course all those -1s have become 255s, and the robot went crazy.

After replacing the chars with ints, everything went more or less smoothly:

Remember kids, don't write "char" when you mean "int8"!

Discussions