Pages: [1]   Go Down
Author Topic: Servo.read() and Servo.write() help  (Read 1910 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 3
Posts: 221
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How do I use Servo.read() to read the angle of the servo, add or subtract the angle value, and use Servo.write() to move the servo to the new angle value? Using the Serial Monitor, I enter 10. Servo.read() is supposed to return 90. I add 10 to 90. Servo.write() is supposed to move the servo to 100 degrees. How can I fix the problem?

Code:
oldvalue = servo1.read();
- or -
servo1.read(oldvalue);
newvalue = oldvalue + inputvalue;
servo1.write(newvalue);

Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17301
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How do I use Servo.read() to read the angle of the servo, add or subtract the angle value, and use Servo.write() to move the servo to the new angle value? Using the Serial Monitor, I enter 10. Servo.read() is supposed to return 90. I add 10 to 90. Servo.write() is supposed to move the servo to 100 degrees. How can I fix the problem?

Code:
oldvalue = servo1.read();
- or -
servo1.read(oldvalue);
newvalue = oldvalue + inputvalue;
servo1.write(newvalue);



How do I use Servo.read() to read the angle of the servo,

You can't. The function name is somewhat misleading. What Servo.read returns is the last position value you sent to the servo with a Servo.write command. There is no way to 'read' what position a servo is presently at as there is no electrical feedback signal coming from the servo back to the arduino. When you send a position value to the servo you can have no idea where it's at as it moves, or indeed even when it's reached that position and stopped, that information is simply unavailable to the common man or to the arduino.  smiley-wink

Lefty

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 637
Posts: 50294
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How are you reading the data from the serial port?

Assuming that the servo can actually get to the commanded position, servo.read() can tell you where the servo is. Adding to or subtracting from that value to get a new position is easy.

Quote
How can I fix the problem?
The first step is admitting that you have a problem. What problem do you have?
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17301
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How are you reading the data from the serial port?

Assuming that the servo can actually get to the commanded position, servo.read() can tell you where the servo is. Adding to or subtracting from that value to get a new position is easy.

Quote
How can I fix the problem?
The first step is admitting that you have a problem. What problem do you have?

Again I state that servo.read just returns what value you last used with a servo.write command, and it's an assumption that the servo has actually reached that value as the element of time and servo speed (loaded or unloaded) is not accounted for.

Lefty
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 637
Posts: 50294
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
it's an assumption that the servo has actually reached that value as the element of time and servo speed (loaded or unloaded) is not accounted for.
Good point about the servo having actually had time to get to the commanded position.
Logged

0
Offline Offline
Tesla Member
***
Karma: 145
Posts: 9686
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Assuming that the servo can actually get to the commanded position, servo.read() can tell you where the servo is.

You can get the same thing when a servo is not even connected to the arduino. Spooky!
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17301
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Assuming that the servo can actually get to the commanded position, servo.read() can tell you where the servo is.

You can get the same thing when a servo is not even connected to the arduino. Spooky!

Not spooky at all. The arduino can't detect (how would it know with no electrical feedback from the servo?) if something is actually wired to one of it's output pins or not, and the read command just reads the value you sent to the servo library on the last write command.

You can write and run a blink program but if there is no led wired to the pin then nothing happens, but that's not spooky, why is a servo no hooked up 'spooky'?  smiley-wink

Lefty
« Last Edit: January 05, 2013, 10:57:27 pm by retrolefty » Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

you could however to get the angle feedback if you dont mind hacking your servo acording to this 2 article
http://forums.trossenrobotics.com/tutorials/how-to-diy-128/get-position-feedback-from-a-standard-hobby-servo-3279/
and
http://www.lynxmotion.net/viewtopic.php?f=2&t=2748
Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

if you open the servo.cpp file you gonna see this
Code:
int Servo::read() // return the value as degrees
{
  return  map( this->readMicroseconds()+1, SERVO_MIN(), SERVO_MAX(), 0, 180);     
}

this is the only think that it does
Logged

0
Offline Offline
Tesla Member
***
Karma: 145
Posts: 9686
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
You can write and run a blink program but if there is no led wired to the pin then nothing happens, but that's not spooky, why is a servo no hooked up 'spooky'?

I'm trolling tonight, and boy, are the fish biting!  smiley
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Johannesburg. UTC+2
Offline Offline
Faraday Member
**
Karma: 105
Posts: 4689
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As the reference says:

Quote
Read the current angle of the servo (the value passed to the last call to write()).

So my supplementary question is just to verify that if there were variables inside that last write(), then read() will return the calculated value?

If I had say:

Code:
jimbo1=10;
jimbo2=20;
jimboservo.write(jimbo1+jimbo2);

Then presumably jimboservo.read() would return 30?
Logged

The Elders of the Internet know who I am
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17301
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As the reference says:

Quote
Read the current angle of the servo (the value passed to the last call to write()).

So my supplementary question is just to verify that if there were variables inside that last write(), then read() will return the calculated value?

If I had say:

Code:
jimbo1=10;
jimbo2=20;
jimboservo.write(jimbo1+jimbo2);

Then presumably jimboservo.read() would return 30?

yes
Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

supposedly yes
Logged

Offline Offline
Full Member
***
Karma: 3
Posts: 221
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Look at the Fine-Tune code. If I use leftfoot.read() and it returns "90". If I enter "-15a,", the code could add/subtract inputed values to/from leftfootvalue. The new leftfootvalue would be 75 and use leftfoot.write() to set the servo at 75 degrees from 90 degrees.

Code:
#include <Servo.h>
String readString;
Servo leftfoot;
Servo rightfoot;
Servo leftankle;
Servo rightankle;
Servo leftknee;
Servo rightknee;
Servo lefthip;
Servo righthip;
Servo leftlowertorso;
Servo rightlowertorso;
Servo leftuppertorso;
Servo rightuppertorso;
Servo leftshoulder;
Servo rightshoulder;
Servo leftelbow;
Servo rightelbow;
Servo leftwrist;
Servo rightwrist;
Servo neck;
Servo head;
int leftfootvalue = 90;
int rightfootvalue = 90;
int leftanklevalue = 92;
int rightanklevalue = 87;
int leftkneevalue = 91;
int rightkneevalue = 87;
int lefthipvalue = 85;
int righthipvalue = 90;
int leftlowertorsovalue = 90;
int rightlowertorsovalue = 91;
int leftuppertorsovalue = 95;
int rightuppertorsovalue = 89;
int leftshouldervalue = 90;
int rightshouldervalue = 90;
int leftelbowvalue = 90;
int rightelbowvalue = 90;
int leftwristvalue = 90;
int rightwristvalue = 90;
int neckvalue = 90;
int headvalue = 90;

void setup()
{
  Serial.begin(9600);
  Serial.println("Biped Robot Program");
  leftfoot.attach(12);
  rightfoot.attach(11);
  leftankle.attach(10);
  rightankle.attach(9);
  leftknee.attach(8);
  rightknee.attach(7);
  lefthip.attach(6);
  righthip.attach(5);
  leftlowertorso.attach(4);
  rightlowertorso.attach(3);
  leftuppertorso.attach(2);
  rightuppertorso.attach(22);
  leftshoulder.attach(23);
  rightshoulder.attach(24);
  leftelbow.attach(25);
  rightelbow.attach(26);
  leftwrist.attach(27);
  rightwrist.attach(28);
  neck.attach(29);
  head.attach(30);
  leftfoot.write(leftfootvalue);
  rightfoot.write(rightfootvalue);
  leftankle.write(leftanklevalue);
  rightankle.write(rightanklevalue);
  leftknee.write(leftkneevalue);
  rightknee.write(rightkneevalue);
  lefthip.write(lefthipvalue);
  righthip.write(righthipvalue);
  leftlowertorso.write(leftlowertorsovalue);
  rightlowertorso.write(rightlowertorsovalue);
  leftuppertorso.write(leftuppertorsovalue);
  rightuppertorso.write(rightuppertorsovalue);
  leftshoulder.write(leftshouldervalue);
  rightshoulder.write(rightshouldervalue);
  leftelbow.write(leftelbowvalue);
  rightelbow.write(rightelbowvalue);
  leftwrist.write(leftwristvalue);
  rightwrist.write(rightwristvalue);
  neck.write(neckvalue);
  head.write(headvalue);
}
void loop()
{
  if (Serial.available()) 
  {
    char c = Serial.read();
    if (c == 'w')
    {
      Serial.println("Robot Walking");
      while (Serial.available()==0)
      {
        robotwalk01();
        delay(1000);
        robotwalk02();
        delay(1000);
      }
    }
    if (c == 's')
    {
      Serial.println("Robot Standing");
      robotstand();
    }
    if (c == ',')
    {
      Serial.println("Fine-Tune Servos");
      if (readString.length() >1)
      {
        Serial.println(readString);
        int n = readString.toInt();
        if(n >= 500)
        {
          Serial.print("writing Microseconds: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) leftfoot.writeMicroseconds(n);
          if(readString.indexOf('b') >0) rightfoot.writeMicroseconds(n);
          if(readString.indexOf('c') >0) leftankle.writeMicroseconds(n);
          if(readString.indexOf('d') >0) rightankle.writeMicroseconds(n);
          if(readString.indexOf('e') >0) leftknee.writeMicroseconds(n);
          if(readString.indexOf('f') >0) rightknee.writeMicroseconds(n);
          if(readString.indexOf('g') >0) lefthip.writeMicroseconds(n);
          if(readString.indexOf('h') >0) righthip.writeMicroseconds(n);
          if(readString.indexOf('i') >0) leftlowertorso.writeMicroseconds(n);
          if(readString.indexOf('j') >0) rightlowertorso.writeMicroseconds(n);
          if(readString.indexOf('k') >0) leftuppertorso.writeMicroseconds(n);
          if(readString.indexOf('l') >0) rightuppertorso.writeMicroseconds(n);
          if(readString.indexOf('m') >0) leftshoulder.writeMicroseconds(n);
          if(readString.indexOf('n') >0) rightshoulder.writeMicroseconds(n);
          if(readString.indexOf('o') >0) leftelbow.writeMicroseconds(n);
          if(readString.indexOf('p') >0) rightelbow.writeMicroseconds(n);
          if(readString.indexOf('q') >0) leftwrist.writeMicroseconds(n);
          if(readString.indexOf('r') >0) rightwrist.writeMicroseconds(n);
          if(readString.indexOf('s') >0) neck.writeMicroseconds(n);
          if(readString.indexOf('t') >0) head.writeMicroseconds(n);
        }
        else
        {   
          Serial.print("writing Angle: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) leftfoot.write(n);
          if(readString.indexOf('b') >0) rightfoot.write(n);
          if(readString.indexOf('c') >0) leftankle.write(n);
          if(readString.indexOf('d') >0) rightankle.write(n);
          if(readString.indexOf('e') >0) leftknee.write(n);
          if(readString.indexOf('f') >0) rightknee.write(n);
          if(readString.indexOf('g') >0) lefthip.write(n);
          if(readString.indexOf('h') >0) righthip.write(n);
          if(readString.indexOf('i') >0) leftlowertorso.write(n);
          if(readString.indexOf('j') >0) rightlowertorso.write(n);
          if(readString.indexOf('k') >0) leftuppertorso.write(n);
          if(readString.indexOf('l') >0) rightuppertorso.write(n);
          if(readString.indexOf('m') >0) leftshoulder.write(n);
          if(readString.indexOf('n') >0) rightshoulder.write(n);
          if(readString.indexOf('o') >0) leftelbow.write(n);
          if(readString.indexOf('p') >0) rightelbow.write(n);
          if(readString.indexOf('q') >0) leftwrist.write(n);
          if(readString.indexOf('r') >0) rightwrist.write(n);
          if(readString.indexOf('s') >0) neck.write(n);
          if(readString.indexOf('t') >0) head.write(n);
        }
         readString="";
      }
    } 
    else
    {     
      readString += c;
    }
  }
}
void robotstand()
{
  leftfoot.write(leftfootvalue);
  rightfoot.write(rightfootvalue);
  leftankle.write(leftanklevalue);
  rightankle.write(rightanklevalue);
  leftknee.write(leftkneevalue);
  rightknee.write(rightkneevalue);
  lefthip.write(lefthipvalue);
  righthip.write(righthipvalue);
  leftlowertorso.write(leftlowertorsovalue);
  rightlowertorso.write(rightlowertorsovalue);
  leftuppertorso.write(leftuppertorsovalue);
  rightuppertorso.write(rightuppertorsovalue);
  leftshoulder.write(leftshouldervalue);
  rightshoulder.write(rightshouldervalue);
  leftelbow.write(leftelbowvalue);
  rightelbow.write(rightelbowvalue);
  leftwrist.write(leftwristvalue);
  rightwrist.write(rightwristvalue);
  neck.write(neckvalue);
  head.write(headvalue);

void robotwalk01()
{
  leftfoot.write(leftfootvalue);
  rightfoot.write(rightfootvalue);
  leftankle.write(leftanklevalue);
  rightankle.write(rightanklevalue);
  leftknee.write(leftkneevalue-30);
  rightknee.write(rightkneevalue-30);
  lefthip.write(lefthipvalue-30);
  righthip.write(righthipvalue-30);
  leftlowertorso.write(leftlowertorsovalue);
  rightlowertorso.write(rightlowertorsovalue);
  leftuppertorso.write(leftuppertorsovalue);
  rightuppertorso.write(rightuppertorsovalue);
  leftshoulder.write(leftshouldervalue);
  rightshoulder.write(rightshouldervalue);
  leftelbow.write(leftelbowvalue);
  rightelbow.write(rightelbowvalue);
  leftwrist.write(leftwristvalue);
  rightwrist.write(rightwristvalue);
  neck.write(neckvalue);
  head.write(headvalue);

void robotwalk02()
{
  leftfoot.write(leftfootvalue);
  rightfoot.write(rightfootvalue);
  leftankle.write(leftanklevalue);
  rightankle.write(rightanklevalue);
  leftknee.write(leftkneevalue+30);
  rightknee.write(rightkneevalue+30);
  lefthip.write(lefthipvalue+30);
  righthip.write(righthipvalue+30);
  leftlowertorso.write(leftlowertorsovalue);
  rightlowertorso.write(rightlowertorsovalue);
  leftuppertorso.write(leftuppertorsovalue);
  rightuppertorso.write(rightuppertorsovalue);
  leftshoulder.write(leftshouldervalue);
  rightshoulder.write(rightshouldervalue);
  leftelbow.write(leftelbowvalue);
  rightelbow.write(rightelbowvalue);
  leftwrist.write(leftwristvalue);
  rightwrist.write(rightwristvalue);
  neck.write(neckvalue);
  head.write(headvalue);
}
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26495
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It looks to me like your code would benefit from a healthy dose of arrays.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1]   Go Up
Jump to: