Go Down

Topic: Servo.read() and Servo.write() help (Read 2 times) previous topic - next topic

jeffmorris

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: [Select]

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



retrolefty


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: [Select]

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.  ;)

Lefty


PaulS

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?

retrolefty


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

PaulS

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.

zoomkat

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!
Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)

retrolefty

#6
Jan 06, 2013, 04:55 am Last Edit: Jan 06, 2013, 04:57 am by retrolefty Reason: 1

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'?  ;)

Lefty

ash901226


ash901226

if you open the servo.cpp file you gonna see this
Code: [Select]

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

zoomkat

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!  :)
Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)

JimboZA

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: [Select]
jimbo1=10;
jimbo2=20;
jimboservo.write(jimbo1+jimbo2);


Then presumably jimboservo.read() would return 30?
No PMs for help please. Not active on this forum any more.

retrolefty


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: [Select]
jimbo1=10;
jimbo2=20;
jimboservo.write(jimbo1+jimbo2);


Then presumably jimboservo.read() would return 30?


yes

ash901226


jeffmorris

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: [Select]

#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);
}

AWOL

It looks to me like your code would benefit from a healthy dose of arrays.
"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.

Go Up