Go Down

Topic: Serial Servo Control (Read 587 times) previous topic - next topic

deadtetra

Dec 05, 2012, 06:51 pm Last Edit: Dec 05, 2012, 06:54 pm by deadtetra Reason: 1
I've been working on a "robot" of sorts that is mobilized by two continuous rotation servos, which is controlled via my Arduino Uno R3.

Currently I drive the bot around by sending commands to the Arduino from the serial comm. terminal in the Arduino IDE, works fine, but is a bit annoying to control having to send one command at a time.

I understand Serial communication is asynchronous and can only do one bit at a time... But are there any tricks someone can point me towards where I can make it a bit more "real-time"?

i.e. -- I press the up-arrow key and while the key is being pressed the bot goes forward, as soon as I release the key the bot stops.

I do plan on making a stand alone controller down the road and I realize that'll make my life a lot easier, but in the interest of time, I have no other choice than to rely on serial comm. to control my bot (school project with a  due date).

----------------------------

Currently I have my two servos driven by a power circuit I built, which the Arduino uses as an interface to the servos (power board feeds servos 6V, Arduino operates servos via signal line to servos).
My code is currently just a switch statement which decides which action to take depending on key pressed, following is a portion of my code:

----------------------------Start code------------------------------------------------
#include <Servo.h>

Servo servoL;
Servo servoR;

int inByte;

void setup()                                                
{
  Serial.begin(9600);                                  
     
  servoL.attach (5);                                    
  servoR.attach (6);                                    
}

void loop()                                          
{
 if(Serial.available() > 0)                        
 {
    inByte = Serial.read();                      
    control();                                      
 }
}

void control()                                      
{
  switch(inByte)                              
  {
   case 'a':                                        
      servoLeft();
      break;
----------------------Continued---------------------------

void servoLeft()
{
   servoR.write(99);
   servoL.write(120);
   return;
}

-----------------------------------End code--------------------------------

Obviously I only included the parts of interest, but is there anyway to make the servo respond upon a closed key? I tried looking up how to stream data to Arduino but could not figure out how to make it work for my purposes, obviously I'm still learning here...

Thanks for any help, it is greatly appreciated!

-jay

P.S. -
Sorry if this is in the wrong section, wasn't sure if it should go in the motor section or this one, but I figured it was mainly a programming issue...

PaulS

Quote
i.e. -- I press the up-arrow key and while the key is being pressed the bot goes forward, as soon as I release the key the bot stops.

What application are you using to detect that key press? The usual way to deal with the "do something while I hold this key down; stop when I release it" is to have the application that detects key presses and key releases is for that app to send one thing (like A) when the a key is pressed and something else (like a) when the key is released. Then, the bot does one thing when it gets 'A' and something else when it gets 'a'.

PeterH

It's best to enclose your code in [ code ] [ /code ] tags to prevent the forum software from mangling it. You can insert these tags by clicking on the # button when you're editing your post.

If you use a different terminal application at the PC which sends characters as they are typed instead of buffering them until you enter a linefeed, then you could have each character trigger motion for a short period and rely on the keyboard autorepeat to trigger this repeatedly while the key is held down. It would need some trial and error to make sure the length of each action was slightly longer than the autorepeat interval, and this would also mean the 'bot carried on for a moment after you release the key, but it'd be better than the current situation.
I only provide help via the forum - please do not contact me for private consultancy.

deadtetra

Thanks for the advice on posting code in the forum, this is my first time posting here, but I have a feeling it won't be my last.

Do you have any recommendations for terminal applications like that which work well with Arduino?
I'm thinking I can get your suggestion working pretty well with while loops, or maybe while loops nested in switch statements...

deadtetra

Code: [Select]

#include <Servo.h>

Servo servoL;
Servo servoR;

int inByte;

void setup()                                                 
{
   Serial.begin(9600);                                   
     
   servoL.attach (5);                                     
   servoR.attach (6);                                     
}

void loop()                                           
{
  if(Serial.available() > 0)                         
  {
     inByte = Serial.read();                       
     control();                                       
  }
}

void control()                                       
{
   switch(inByte)                               
   {
    case 'a':                                       
       servoLeft();
       break;
----------------------Continued---------------------------

void servoLeft()
{
    servoR.write(99);
    servoL.write(120);
    return;
}


Here's my code the way you suggested btw, wasn't sure if you could see it the way i originally posted it or not, but this is my current set up...

deadtetra

Found one... Processing.org was a treasure trove of ideas.... Thought I'd mention this in case anyone else read this thread with same problem. Wrote a terminal app from scratch using bare minimum requirements for serial link using processing ide... Resembles Arduino IDE so much I think they may be related? .. Mad easy to use

retrolefty

Quote
Resembles Arduino IDE so much I think they may be related?


That's because they are. The processing IDE written in java is what the arduino folks ported over for use as the IDE for the arduino and that's why loading the arduino IDE distrubution onto one's PC one ends up with having Java installed even though we write your sketches in C/C++ to have compiled and uploaded to the board. The Arduino platform is a soup mix of various other independent open source projects combined into the arduino open source platform.

I guess on could say that the Arduino project is a project based on other projects. Or one could just call it a mutt.  ;)

Lefty

Go Up