PWM precision servomotor movement

Status
Not open for further replies.
HI everybody,

I use pwm control servomotor.
Range is 0..180, so I put 1 by 1 (89,90,91...) degre.


How can I do to obtain 0.1 by 0.1 degre movement ?
I have try increase by 0.1 starting at 87 movement appear at 88 not in 87. to 87.9.

Seem the pwm servo library is set with 4096 (12bits) so I should obtain 180/4096=0.044° increment a least.
I redo this in setup using analogWriteResolution(12);
Seem that with int you have'nt 0.1°...

My clock speed is 96MHz.

Sample code :
Code:
#define axeX_PWM_PIN 5
#define axeY_PWM_PIN 6
float valeurCdeX=90,valeurCdeY=90;

void setup (){
  analogWriteResolution(12);  // analogWrite value 0 to 4095, or 4096 for high
  myservoX.attach(axeX_PWM_PIN);
  myservoX.write(90);
...

Code:
//sample use  in loop

myservoX.write(valeurCdeX);

Laurent.
 
Last edited:
Code:
servo.writeMicroseconds (...)
is used in the standard Servo library for higher precision. 1500 microseconds
corresponds to mid-travel (90 degrees) by convention, and 544 and 2400 microseconds for 0 and 180 seems
to be the default scaling in the Servo library.

This is all from the standard Arduino Servo library, not sure if Teensy version differs much for this, or which servo
library you are suing anyway as you didn't say (the forum rule exists for a reason!)
Its possible some libraries allow float as arg to write().
 
thank's.

Seem this library as you speak about Servo library it is not compatible with Teensy3.2.

Code:
Servo.h:77:2: error: #error "This library only supports boards with an AVR, SAM, SAMD, NRF52 or STM32F4 processor."
 #error "This library only supports boards with an AVR, SAM, SAMD, NRF52 or STM32F4 processor."

I have use this one:

Code:
#include <PWMServo.h>
 
Servo library it is not compatible with Teensy3.2.

Arduino must be using the wrong library, because the Teensyduino installer definitely does give you a copy of the Servo library which works with Teensy 3.2 (and all Teensy models).

Look in the console panel for info about duplicate library. Arduino should always show this when you have a compiler error. Without an error, it only appears if you have turned on verbose output while compiling in File > Preferences.

The duplicate library info will tell you the full pathname of the Servo library is used, and the pathnames of any others it ignored. You probably just need to move or delete the copy Arduino is erroneously choosing, so it will then use the copy which came from the Teensyduino installer.
 
Hi Paul
Thanks to your reply and advice. You're right.
After destroy an arduino library I can compile writeMicroseconds function...

Seem much precise with Modelcraft servo RS2MG I can obtain 0.5° against 1°.

sample code:

Code:
//code precis writeuseconde
     float valeurCdeXus=(valeurCdeX/180*1000)+1000; //convertit en useconde 180°=1000us pour arange suposed 1000..2000
     if (sbpa_add_sortie=='X')  {myservoX.writeMicroseconds(valeurCdeXus);};

regard's.
Laurent.
 
Pardon the n00bie Q, but I'm getting the same error about board support in Servo.h. Is there any way to set up the Arduino IDE 1.8.15 (using Teensyduino 1.54) to support both sets of CPUs, meaning Arduino and Teensy? I run lots of different projects with CPUs of various flavors.

I could destroy/delete the Arduino servo.h but would rather not, need Arduino support. I could move it when I compile for the Teensy, move it back for Arduino, but perhaps not the most elegant thing. Any other options?
 
tried an experiment, renamed this:

C:\Documents\Arduino\code\libraries\Servo

to this:

C:\Documents\Arduino\code\libraries\Servo1

and the IDE switched to the Teensy servo library ok, even with it open (didn't have to close or quit).

Renamed it back, error came back of course.

so switching isn't too terrible, as long as you remember to rename.
 
Is there any way to set up the Arduino IDE 1.8.15 (using Teensyduino 1.54) to support both sets of CPUs, meaning Arduino and Teensy? I run lots of different projects with CPUs of various flavors.

Yes. The way it's meant to work is each type of board has a copy of the Servo library in its own libraries folder. For Teensy, that folder should be {Arduino}/hardware/teensy/avr/libraries. For other boards, you could research where they have their libraries folder, but the simplest way is to just pay attention of the duplicate libraries info Arduino prints after compiling your code. You can also use File > Preferences to turn on verbose info during compile. That will cause Arduino to show the full pathname for every library it uses, even if there aren't conflicts or errors.

If Arduino doesn't find the library in a platform's libraries folder, it tries the main libraries folder as a final fallback.

You also must NOT put a copy of Servo into your sketchbook libraries folder, which by default is usually {Documents}/Arduino/libraries, but can be configured to other locations using File > Preferences (so check the prefs if unsure). Arduino has a feature where any library you place there, which is what the library manager does, overrides all other copies. Normally that's good, so you can take control and especially if you want to experiment with editing the library.

But for a library like Servo, you probably want the copy Arduino publishes in the main libraries folder. Then you want platforms like Teensy to have their copy in their own platform folder, so the Arduino IDE uses the correct copy. But you *never* want to put a copy of Servo into {Documents}/Arduino/libraries. If you have a copy needed for a particular board, find that place where that type of board has its own libraries and put its copy of Servo there.

Again, paying careful attention to the full library pathnames Arduino prints after compiling is the key. Use File > Preferences to turn on verbose info if you're not seeing enough info.
 
I was wondering about that very thing!

Ok, I'll give that a try. Sounds much easier than the renaming thing, one less thing to have to remember.

And sure enough, it works like a charm! Just pick the target board in the IDE and the rest is automatic. Perfect!

Thanks much!

makes me wonder what other libs need to be purged from the sketchbook that conflict with the Teensy versions.
 
Status
Not open for further replies.
Back
Top