Targeting System - Serial Interface Servo Control
Please feel free to leave comments and suggestions. I am very open to any ideas anyone has that will make this implementation simpler, more robust, totally rad, ect ect. Feel free to leave any comments, or questions and I will get back to you as soon as I can. As always, my notebook pages will be under construction pretty much indefinetly =P
Goal: Rework the TSP senior design project to use the MBED instead of PC104.
Tasks:
1. Interface MATLAB by using RS232 Comminucation to use the existing MATLAB video/image processing model for object detection.
Done - 2. Interface the servo by using PWM for control.
Here we go! - Serial Port Communication Thru MBED USB
To see what's going on the serial port I use RealTerm, which can be found here http://realterm.sourceforge.net/ . It works great on Win7.
First I needed to write a simple program that would read serial data through the USB interface provided by the MBED board.
#include "mbed.h" Serial pc(USBTX, USBRX); // USBTX - Tranmit on USB USBRX - receive on USB int main() { int angle; char read[10]; // Set the Serial variables pc.baud(9600); pc.format(8, Serial::None, 1); while(1) { wait(1); printf("Heartbeat \r"); if ( pc.readable() ) { printf(" There's something to read\r"); pc.scanf("%s", &read); printf(" Reading %s \r", read); } } }
Yes, it is rather simple but it's effective for our purposes. Now fire up RealTerm and see what we get and try to send some inputs:
With an input of '100' sent as ASCII:
Notice that the CR box for the EOL character. This is important or else the code will hang on the scanf() call because it hasn't found the end of the message. Experimentation with Realterm also showed that LF alone also works but not both CR and LF together.
GREAT! It's working! Since the main goal is to interface the MATLAB Simulink Model with the servo to control it's position we are going to have to convert the string into a float data type. Good thing atof() is around to do this for us. Let's convert the string of numbers coming in to a float and add 10 to prove we are working with the correct data type. Use this code as the main loop:
int angle; // variable to use as the string to int while(1) { wait(1); printf("Heartbeat \r"); if ( pc.readable() ) { printf(" There's something to read \r"); pc.scanf("%s", &read); printf(" Reading %s \r", read); angle = atoi(read); printf(" Converted string to %i \r", angle); angle = angle + 10; printf(" Added 10. Value is now %i \r\r", angle); } }
What does Realterm say?
Fantastic! Now to lets try have the MBED control a servo based on the values sent from RealTerm. Import the Servo ( http://mbed.org/projects/cookbook/svn/Servo/trunk ) library and use this code:
#include "mbed.h" #include "Servo.h" Servo servo (p21); Serial pc(USBTX, USBRX); // USBTX - Tranmit on USB USBRX - receive on USB int main() { float angle; char read[10]; // Set the Serial variables pc.baud(9600); pc.format(8, Serial::None, 1); // Set the servo to initialize in the 'middle' servo = .5; while(1) { //wait(1); //printf("Heartbeat \r"); if ( pc.readable() ) { printf(" Reading Command \r"); pc.scanf("%s", &read); printf(" Command Received: %s \r", read); angle = atof(read); printf(" Setting angle to %f percent \r", angle); servo = angle/100.0; printf(" Servo set to %f \r\r", servo.read() ); } } }
Don't forget to change angle to a FLOAT type or else you get either get 0% or 100% on the servo angle. All those printf() calls may seem unnecessary but they always seems to save me time in debuging. I guess I still need those warm fuzzies to know my code is playing nice. Lets also comment out the 'Hearbeat' and wait lines for a more realtime response from the servo and serial interface. The response time seems pretty quick from the time the command is sent from the RealTerm application as shown by the short video below
<<< INSERT SHORT SERVO APPLICATION VIDEO HERE >>>
Further research:
Can I use a CMOS camera or something of the sort to bring the image straight to the MBED and do image processing there (Thresholding, Background Subtraction, Feature based tracking)? *Ayi yi yi.... the code would be beastly*
3 comments
You need to log in to post a comment
What image sensory devices do you use now?