I know that there is the servo.read() function in the servo library, but according to the reference this only works with the last written value. That seems to be how it operates too.
Is there a way to read the servo position if no value has been written to it yet? For example, in the setup part of the code, when the Arduino has first had the code uploaded or been reset, I want to know the position of a servo that is attached to it but hasn't been moved yet (e.g. in the last position it had been set to before it lost power/signal).
No, of course not. There is no feedback signal from the Servo for a Arduino to read. Only with external components, say a pot coupled to the servo wheel, or perhaps tapping into the servo's internal position pot would this be possible. Also I believe that just of act of attaching the servo to the software routine causes it to move to a default position (1.5msec position?), at least that is what a recent posting talked about.
That's what I thought, but I had hoped the Arduino would be able to read the pot signal.
I might hack the servo in the future then. I moderate my servo speed because if it moves to fast it can torque my hardware. I'll just have to damp it on the hardware side for now though.
When I try to do a read of the position value it outputs 93 initially. Oddly enough this is the value of a constant variable that I've defined as my relative neutral. It might just be a coincidence though.
In my experience, the servo doesn't really move when initially attached, though if its 1.5ms I might just not be able to notice it.
In my experience, the servo doesn't really move when initially attached, though if its 1.5ms I might just not be able to notice it.
Well a simple experiment should prove out the behaviour. With power off, just move the servo wheel slowly to one end of travel. Reapply power and attach to the servo and see what it does.
what if you solder two wires, the 1st to one pin of the servo pot and the 2nd to the washer pin of the pot and then try to read (with an analogread) the value of the voltage? :
what if you solder two wires, the 1st to one pin of the servo pot and the 2nd to the washer pin of the pot and then try to read (with an analogread) the value of the voltage?
Should require only one additonal wire, the wiper terminal of the pot, as there is alread a common ground wire back to the Arduino.
Another option you might want to look into is the "robotics servo" (but be careful with this term, as it means different things to different manufacturers).
This is a servo that has an integrated microcontroller, and you communicate with it using a serial protocol (I2C or SPI, or something similar); typically they can be daisy chained on a "bus" so that a large number of the servos can be connected and controlled using a minimum of data lines from the controller (in your case, the Arduino). They generally include direct feedback reading capabilities for the internal components (sometimes even temperature and other variables).
They were designed for use in walking robots (usually hexapods and bipedal robots), where the need to have feedback from a large number of servos was paramount. They also tend to be fairly robust and powerful (generally using ball bearings for the shafts and metal geartrains). As such, they are generally much more expensive than a typical servo.
While you can hack a servo to add a fourth line to sense the potentiometer directly, on some servos this can be difficult, and I have read scattered reports of the extra line interferring with the servo's own feedback circuitry, causing problems with positioning (as well as jittery output on the line added). But it is definitely worth trying, especially if you are trying to keep costs low.
Thanks for the info it's all very useful. I think hacking the servo is the best option for now, because as noted, the robotics servo's are probably quite expensive.
I'll give it a shot if I get the chance. Thanks for the link brainbooster, looks like that should do the trick if I go about it.