Friday, December 25, 2015

Robot Christmas

AVA Disco Mode


The robot received a set of Ikea Dioder LED strips for Christmas. Below is a quick video of AVA in disco mode. There will be a separate post on controlling these strips using an Arduino at some stage.




AVA Christmas


In keeping with the Christmas theme here is AVA singing a Christmas Carol using the EMIC 2 Speech Synthesizer. The Emic 2 Text-to-Speech Module is a multi-language voice synthesizer that converts a stream of digital text into speech. You can select either the Epson (default) or DECtalk parsers.


Note that the Emic 2 uses DECtalk version 5.0.E1. The Flame of Hope web site has a heap of DECtalk songs but they are for earlier versions of the DECtalk parser and require tweaking to work with the EMIC 2. Thanks to Ron on the Parallax forum for these tips on converting the text files:

  • you often have to change an "L" to "LL"
  • sometimes you need a space between any character preceding the "ey" phonetic symbol.

The following is the string used to produce the song in the video above, should you wish to try out your EMIC 2's singing ability.


"[:phone arpa speak on][:rate 190][:n2][:dv ap 200 sm 100 ri 100][R EY<200,17>N<100>DRAO<200,24>PS<100>AO<200>N<100>ROW<300,19>ZIX<200,17>Z<100>AE<150>N<100>D<50>WIH<300,12>SKRR<200,17>Z<100>AO<200>N<100>KIH<300,19>TAH<150,17>N<100>Z<50>_<300>BRAY<200>T<100>KAO<300,24>PRR<300>K EH<300,19>TEL<200,17>Z<100>AE<150>N<100>D<50>War<200,12>M<100>WUH<300,17>LL EH<200>N<100>MIH<300,19>TAH<150,17>N<100>Z<50>_<300>BRAW<200>N<100>PEY<300,24>PRR<300,22>PAE<300,17>KIH<300,19>JHIX<200,15>Z<100>TAY<200>D<100>AH<200,22>P<100>WIH<200,20>TH<100>STRIH<300,13>NX<200>Z<100>_<300>DHIY<200,12>Z<100>AR<300,13>AX<300,15>FYU<300,17>AH<200,18>V<100>MAY<300,20>FEY<300,22>VRR<300,24>EH<200,22>T<100>THIH<500,16>NX<300>Z<100>][:n0]"


Arduino Code



To make it easy for our Arduino Mega 2650 to communicate with the EMIC2 we created a library called synthesizer.h. As you can see the Arduino connects to the EMIC 2 via one of its serial ports.



To use this class, I have created a robot class which looks like:



Then in the main.ino file all you need to do is:



You don't have to use C++ classes like I did but it makes it easier to maintain your code. You can also ignore the bits which refer to the motors and other sensors, I just leave it in here for completeness.


Wednesday, December 23, 2015

Moving to embedXcode...

The Problem


We have been using the Arduino IDE to develop on. Which is fine (and free) but it does have some limitations. In particular, we ran into the problem of having too many tabs (files) in our main controller sketch. This came about as a result of another issue, which is not unrelated.

As with most people we started developing our robot code organically using mostly straight c with c++ functionality reserved for included libraries. By the time you add 13 sensors, speech synthesis, speech recognition, 2 motor controllers, serial comms for the Bluetooth remote (which will be described in a subsequent post) and an I2C bus connecting the real time clock, pressure / altitude module and 2 auxiliary Arduino's used for display and logging, the code gets messy and hard to maintain. A simplified schematic of the robot so far is shown below. The schematic was produced using Fritzing which I have mentioned previously and strongly recommend.



As an aside, we are using SPI for the logging Arduino to communicate with its OLED shield, so we have all the available communication options covered (with the possible exceptions of WiFi and RF - stay tuned)!

To fix the messy, unmaintainable code issue, we started to translate our c code into c++ classes where it made sense. This made the code much more modular but each class requires a new tab which brings us back to the first issue mentioned above. Once the number of tabs fill up the top row of the IDE, it becomes a bit of a pain to move to the tabs not displayed. There is a keyboard shortcut to go to the next and previous tab, but that is a bit clunky and was the straw that sent me on a search for a better IDE to develop Arduino code on.


The other short comings of the Arduino IDE are:
  1. No code completion; and
  2. The syntax highlighting is a bit hit and miss. You could fix a particular issue by editing the keywords.txt file in the appropriate library but that is pretty fragile.

The Solution


A quick google search will turn up LOTS of different options for programming the Arduino. Our preferred solution would incorporate the following:
  1. Minimal learning curve;
  2. Preferably free;
  3. Works on a Mac;
  4. Can upload a sketch from within the IDE;
  5. Includes a serial terminal similar to the serial monitor provided by the Arduino IDE; 
  6. Has code completion and syntax highlighting; and
  7. Handles multiple files well; and
  8. Doesn't create any new issues.
With this shopping list in mind, the field quickly narrows to a plug in for Xcode called embedXcode. This ticks all our requirements and has (almost) no learning curve since we use Xcode for our App Development work.



The other IDE we considered was Eclipse which is the basis of Android Studio that we use for Android Development. What swayed or decision was that the setup looked a lot easier for Xcode and we were more familiar with this IDE as well. Have a look at this blog by a chap in the US, he had the same issues but decided on Eclipse. Going with what you are already familiar with makes a lot of sense.

Setting up embedXcode


This is straight forward and described in detail in the (large) user manual. In summary:
  1. Install Xcode if you don't have it already.
  2. Download embedXcode. We went for the free version to evaluate how good the plug in is. Should it work out we will either upgrade to embedXcode+ or pay a donation. We want to encourage ongoing development of the plugin. We know that Xcode changes frequently and with each new release normally breaks legacy code.
  3. Install the plug in and open up Xcode. That's it! You should now have the option to open a new embedXcode AVR project (see below).

Importing Sketches


Unfortunately you can't just import sketches that you have done with the Arduino IDE. There is a process that you need to follow:
  1. Open a new project in Xcode. You can call it the same name as your sketch. Make sure you select the correct board type (e.g. Uno) in the new project options. The scope options are sketch or library and this also sets the extension of the main file (.ino for sketches and .cpp for libraries).
  2. Open your sketch in the Arduino IDE and copy the contents into the ino file in your Xcode project. There is some pre-processor definitions used for code sensing at the top of the ino template file in Xcode. Don't overwrite these unless you know what you are doing (e.g. if you are using a recent Arduino you can replace the lot with #include "Arduino.h".
  3. The next step is to find your Arduino libraries folder in Finder and drag it across to the Sketchbook group. When the choose options for adding these files dialog comes up, don't select "copy items if needed" (unless you want multiple copies of your library files), select index in "add to targets" (see below). You also need to change the type from plain text to "c++ source". This option is found in the right hand pane of Xcode after selecting the main sketch (shown in the second screenshot below).
  4. The thing I stuffed up initially was not also listing the libraries used in the makefile. If you get an error saying a library can't be found, this may be the answer To fix it, select the makefile in the left hand pane and add the names of the libraries used.  There are two types of libraries you will use most often, application libraries and user defined libraries. Generally, the application libraries are the ones defined using <> (e.g.  #define <SoftwareSerial>) and the user libraries are defined using"" (e.g. #define "blunoAccessory"). However the <TimerOne> library was an exception to this rule and needed to be defined as a user library (e.g. "APP_LIBS_LIST = SoftwareSerial" and "USER_LIBS_LIST = U8glib blunoAccessory TimerOne". Different libraries are separated by a space and don't show the file extension. Read the user manual if you are still confused, it covers this topic in some detail.



Once you have completed the four steps above, you should be able to build (compile) your project, but only if your board is connected.

New Issues?


The only issues which have arisen so far are fairly minor apart from the speed of compilation and uploading. This is significantly slower than the Arduino IDE. If you purchased  embedXcode+ then you have two options:

  1. Build -> All targets which builds and links everything, and is slow.
  2. Make -> Fast -> additional targets only, which builds the main sketch and the libraries in the local folder, so it is much faster (up to 10x according to the developer).

If you only downloaded the free version then you are stuck with option 1. Other areas to be aware of are:
  1. You can't Build (compile) a project unless you are connected to your board via USB. This is a bit of a pain as you could just compile using the Arduino IDE without being connected to the board. Solved this. From the Product menu item select Scheme -> Build.
  2. Instead of Serial Monitor in the Arduino IDE you use Terminal on the Mac. The default baud rate for terminal is 9600. Which is ok if that is the baud rate you want to use on the serial port but I have a sketch which communicates with a BlueTooth LE shield on the same comm port that needs 115200 baud. Theoretically you can change the baud rate in Terminal using stty (e.g. stty 115200), but this doesn't seem to work. I have also tried "screen /dev/tty.usbmodemfd121 115200", but no cigar. I will keep working on this but the work around for now is to use 9600 baud if you can or use the old Arduino Serial Monitor if all else fails. Other solutions are welcome, so feel free to comment below. I have found the answer to this as well. There is an option in the makefile that you can use to set the baud rate, namely:  "SERIAL_BAUDRATE = 115200".
  3. Getting code sense to work for me took a bit of tweaking. First make sure that the sketch (ino file) has index selected for Target Membership (click on this file to check). Save the file and close the project. A small update is required to the user manual instructions. In the current version of Xcode (v7.2), projects is now a separate dialogue to Organiser but it is still found under the Windows menu in Xcode. Delete your derived data for your project and then reopen your project. After indexing, code sense for the Arduino specific definitions should work. You can change the syntax colouring in preferences, fonts and color.

If anything else comes up, I will add it here. All in all I am pretty happy with this solution. We will still use the Arduino IDE for simple sketches but for something as complicated as a robot, an IDE like Xcode is essential. Well done embedXcode!

Update 27th December 2015


I was so impressed with embedXcode that I upgraded to embedXcode+. This is worth doing if you use the plugin regularly. It also encourages the developer to keep supporting the product which is important given the frequent updates to Xcode.

Error: Serial port not available (Step2.mk)




If you come across this error (I did), there is a simple fix. First open up terminal and find out the name of your serial port. Type the following command: ls /dev/tty.usb*


This will provide the name of your serial port. Copy this and then open the main Makefile in Xcode uncomment the line:

#BOARD_PORT = /dev/tty.usbmodem*

and specify either a more general name:  e.g. BOARD_PORT = /dev/tty.usbmodem*

or the specific USB port name of the board that you got from Terminal e.g. in my case:

BOARD_PORT = /dev/tty.usbmodemfd121









Wednesday, December 9, 2015

Parallax HB-25 Motor Control Library for Arduino

Moving AVA


We are using the Arlo robotics platform from Parallax to build up AVA. As you can see in the schematic below, this incorporates two HB-25 motor controllers (Part Number: #29144) which look after the left and right wheels.


After an exhaustive search we were not able to find an existing Arduino library for the HB25, so we were forced to write our own. This involved delving into the incomplete version of C++ which Arduino uses. Before getting to this, let's look at how you can control the HB25 just treating it as a servo, which will be fine for most folks.

Wiring the HB-25




If you are using the Parallax Motor Mount and wheel kits (part numbers #28962 - aluminium or #28963 - plastic) then the red cable should be connected to M1 on the HB-25 and the blue cable should be connected to M2. If these are reversed then the FORWARD and REVERSE commands will be reversed.



Make sure that the jumper is in place for mode 1 operation. In this mode, you need a separate digital output on your Arduino for each HB-25 that you want to control.

Controlling the HB-25


From the HB-25 data sheet, we can establish the following:

  1. The HB-25 operates like a servo. You only need to send a single pulse (in mode 1) to change direction or speed. Pulse width determines the HB-25 output.
  2. Valid pulse widths are 0.8 ms to 2.2 ms. If the HB-25 receives a pulse width which is outside this range, the motor will be stopped until it receives a valid pulse.
  3. The minimum time between pulses (HOLD_OFF_TIME) is 5.25 ms + pulse time (max 2.2 ms). Thus the worst case hold off time needs to be 7.45 ms. We have used 8 ms.
  4. The maximum time between pulses is unlimited, since a single pulse will be latched by the HB-25. An exception to this would be if the Communication Timeout feature of the HB-25 has been enabled. You can read more about this on the HB-25 data sheet (https://www.parallax.com/downloads/hb-25-motor-controller-product-documentation).
  5. Regardless of the mode, the HB-25 signal pin should be brought low immediately upon power up. The Library does this when you instantiate a HB25MotorControl object.
  6. Pulse width (1 ms = 1000 microseconds) will control the HB-25 as follows:

                        - 1.0 ms Full Reverse
                        - 1.5 ms Neutral (STOP)
                        - 2.0 ms Full Forward

Arduino Code


So making use of the above, we can control a HB-25 using the following Arduino code. Note that you will need to set controlPin to whatever digital output pin is connected to your HB-25. After you have initialised your HB-25 you can control it by writing a value between 1000 and 2000 to it using the servo.writeMicroseconds() method. You need to ensure that two commands are not sent within the minimum hold off time. This is taken care of for you in the library below.

#include <Servo.h>

#define REVERSE       1000
#define STOP          1500
#define FORWARD       2000
#define HOLD_OFF_TIME 8

Servo servo;

// HB-25 initialisation time (5ms)
delay(5);                                           
pinMode(controlPin, OUTPUT);
// Set control pin low on power up
digitalWrite(controlPin, LOW);  
// Attach HB-25 to the control pin & set valid range                    
servo.attach(controlPin, 800, 2200);
servo.writeMicroseconds(STOP);

Arduino Library


To make using the HB-25 a bit easier and to hide some of the complexity we wrote an Arduino library for it. This consists of four files (click to download):

1. HB25MotorControl.h;
2. HB25MotorControl.cpp;
3. Keywords.txt; and
4. HB-25_Test.ino

To use this library, you need to create a new folder in your Arduino libraries folder called HB25MotorControl. Copy HB25MotorControl.h, HB25MotorControl.cpp and keywords.txt into this folder. Then create a sub-folder called examples and place HB-25_Test.ino into that.

You need to restart the Arduino IDE (if it is already open) to be able to see and use this library.

The example sketch should demonstrate how you use the library, but at its simplest:

#include <Servo.h>
#include <HB25MotorControl.h>

const byte controlPin = 9;              //  Pin Definition

HB25MotorControl motorControl(controlPin);

void setup() {
  motorControl.begin();
  motorControl.moveAtSpeed(500);
}

void loop() {
  
}

Valid speed ranges for the forwardAtSpeed and reverseAtSpeed methods are 0 (stop) to 500 (maximum speed). For rampToSpeed and moveAtSpeed you can use from -500 (full reverse) to 500 (full forward). As before, a speed of 0 will stop the motor.

Feel free to modify and reuse the library as you like. If you do improve it, then let us know. Attribution is nice but not necessary, and as usual this library comes with no warranties, so use at your own risk.