Go Down

Topic: Servo control - from Processing? (Read 1 time) previous topic - next topic

daidalos

Hello everyone,

Ok so I've been working all day but to no avail.....  

I'm attempting to control a servo motor via serial communication (characters sent from a simple Processing sketch)
Since I've never done this before I tried to use the LED example (that detects if the mouse cursor is over the square and if so, send power to the LED) as a starting point.  

I assumed that it would be as easy as changing the code to send a pulse to the servo as the serial reads a ASCI character, but I'm getting no movement.

The other problems occur when I attempt to run the Processing sketch - it sometimes disconnects the serial connection leaving error messages etc.

heres the code for the ARDUINO:

Code: [Select]
int outputPin = 13;
int val;

int minPulse = 1000;
int maxPulse = 2500;
int pulse = 0;

int lastPulse = 0;
int refreshTime = 20;



void setup()
{
 pinMode(outputPin, OUTPUT);
 pulse = maxPulse;
 Serial.begin (9600);
}

void loop()
{
 if (Serial.available()) {
   val = Serial.read();
   if (val == 'H') {
     pulse = minPulse;
   }
   if (val == 'L') {
     pulse = maxPulse;
   }
 }
updateServo();
}


void updateServo()
{
  if (millis() - lastPulse >= refreshTime) {
   digitalWrite(outputPin, HIGH);
   delayMicroseconds(pulse);
   digitalWrite(outputPin, LOW);
   
   lastPulse = millis();
  }
}



heres the code for PROCESSING
Code: [Select]
import processing.serial.*;

Serial port;

void setup()
{
 size (200,200);
 noStroke();
 frameRate(10);
 
 println(Serial.list());
 
 port = new Serial(this, Serial.list()[0], 9600);
}

boolean mouseOverRect()
{
 return ((mouseX >= 50) && (mouseX <=150) &&(mouseY >= 50) && (mouseY <= 150));
}

void draw()
{
 background(#222222);
 if(mouseOverRect())
 {
   fill(#BBBBB0);
   port.write('H');
 }
 else
 {
   fill(#666660);
   port.write('L');
 }
 rect(50,50,100,100);
}



If anyone has some insight on Serial Servo control, I would greatly appreciate it.
Cheers

- Nick

Daniel

hi

can you make the Arduino side code work by sending "H" or "L" through the serial monitor?

D

daidalos

I gave that a shot. and it didn't work....  :-?

Daniel

ah.. so that means there is something wrong on the Arduino side.

Next I would try:
- checking the connections. If using a battery to power the servos, you need to connect the battery gnd to Arduino GND.
- removing the serial checking loop entirely and having the code run the servo back and forth once a second.
- using another output pin, as 13 has an integral resistor.


D

daidalos

I've also just noticed there is something else occurring here....

If I power the board and servo through the USB, a tried and tested code using a potentiometer to move the servo runs perfectly, however that same code does not work correctly when the board is powered with a 9v power source....
There is a massive lag from turning the POT and when the values are actually received by arduino....

stumped on this one  :-/

Daniel

#5
Jul 22, 2007, 08:25 pm Last Edit: Jul 22, 2007, 08:31 pm by Daniel Reason: 1
That sounds like the USB connection is making lots of servo current available, and that the servo may be underpowered by the 5V regulator. The on-board regulator has a little over 400ma available for external connections, and servos can draw a lot of current.

D


zitron

#6
Jul 23, 2007, 06:44 pm Last Edit: Jul 23, 2007, 06:44 pm by zitron Reason: 1
I think Daniel is correct. You can try powering the servos with a different 5V power source which should have no problem delivering 1A. I have powered servos from a 9V source directly that's also powering the Arduino, that worked, but it's probably not very good for the servos, since they are designed to run at 5V.

-Z-

daidalos

Thanks for the replies.

Yes that seemed to be the case - I switched out the servo for another to test it out and it worked fine.

Going back to the Serial communication - I've got the servo working from keypresses (within the serial monitor) so the serial works fine within Arduino - I'm having a little difficulty on the Processing side - I tell it to send a character "k" for instance upon a specific event , but it does not exactly send that information to Arduino thus not moving the motor.

I'm very new at serial - do I need a carriage return in there to clean up the strings and allow arduino it read it clearly?

perhaps I should do some reading on serial communication - i've searched the site for tutorials, anyone have more?


John_Ryan

I had similar problems here:-

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1185507207

Until I added an array for receiving bytes, and then running a sub once a certain number of bytes are received.

I used 'A' like a hand-shake, then two additional bytes are sent over determined by an array stored in a text file. Since 'A' is the starting point, the subsequent bytes are received in the order they are sent. Without that, I had a hard time getting them in the right order.

The code can be easily adapted to work on servo's. I was planning on doing that at some point in time since I have a need for running servo's from a hex script (.txt), so you can either wait, or try hacking the code yourself.


Go Up