We have detected your current browser version is not the latest one. Xilinx.com uses the latest web technologies to bring you the best online experience possible. Please upgrade to a Xilinx.com supported browser:Chrome, Firefox, Internet Explorer 11, Safari. Thank you!

Showing results for 
Search instead for 
Did you mean: 
Registered: ‎06-29-2017

control servomotor with PWM signal Nexys 4 ddr - code c++

I'm trying, but I could not do it, I need your help, exercise 13.7.6 FPGA book Prototyping by VHDL Examples: Xilinx MicroBlaze MCS SoC, 2nd Edition, It says deduct controller routines (are in gpio_core.h and gpio_core.cpp) for the servomotor treated in section 13.6. Write a test program (PWM_LED.cpp) and verify its operation. I am using the servo Tower pro 996r. What I want to do is put a certain angle, 30, 60, or 180 (like when I used arduino or raspberrypi), and the servo is positioned in that particular angle. I leave the datasheet, the driver files from where I must deduct the routines, and an explanation in jpg taken from the book.

datasheets servo


0 Kudos
4 Replies
Scholar u4223374
Registered: ‎04-26-2015

Re: control servomotor with PWM signal Nexys 4 ddr - code c++

This does not look difficult. Pretty much all you have to do is modify the duty cycle so that instead of going from 0% to 100%, it goes from 5% to 10%. A tiny modification in the inner loop will do that.

Registered: ‎06-20-2017

Re: control servomotor with PWM signal Nexys 4 ddr - code c++

I'm not sure how accurate you need to be, but if accuracy is required, you'll need to calibrate each servo individually.  Low cost servos are not accurate devices.  The alignment of the teeth on the servo mount shaft compared to the servo case can and does vary from one servo to the next.  The offset angle of the servo arm to the servo mount shaft will also vary.  This will be a DC offset.  Lastly, the gain from one servo to the next will differ, especially on uncalibrated low cost servos, and is also often non-linear.  Next, I've found the nominal useful pulse width is 900 uS to 2.1 mS, not 1 mS to 2 mS.  Lastly, the calibration may require a curve, depending on your application and desired accuracy. 


Pulse Width    Nominal angle
----------- -------------
900 uS -MAX_SAFE (or +MAX_SAFE) 1500 us 0 2100 uS +MAX_SAFE (or -MAX_SAFE)


Driving a servo outside of the pulse widths listed above could result in damage to the servo gears.


Based on the timer you are using, and the rate at which it is clocked, you will need a function to convert from angle to pulse width.  Then you'll need to program in that pulse width.  You should also program in the period of your PWM to be 22 mS, not 20 mS.  if your servo can handle it, you could have a period of 11 mS, or even 7 mS.  These are better for a low cost servo:




For a higher end servo, you might get 7 mS or 11 mS period.


Note that you will need to provide 5V or more on the Vcc in the above diagram.  The PWM signal can be referenced to 3.3 V, but the servo motor usually needs more than 3.3V.

0 Kudos
Registered: ‎06-29-2017

Re: control servomotor with PWM signal Nexys 4 ddr - code c++

I made the change dividing MAX = 1.0 / 10.0 so that the maximum cycle is 10%, and then I could work it from the code with a constant P20, so that it goes from 5% to 10%, I made my test program but there is a drawback, the servo does not stay goes from -90 to 90, + but it turns to +90 and it returns again to -90 again and again, but what I want is that it stays in +90 or 0, normally in arduino and raspberrypi that you did it with the setAngle function, some help in which I'm wrong.

0 Kudos
Registered: ‎06-29-2017

Re: control servomotor with PWM signal Nexys 4 ddr - code c++

Thanks for your answer, to raise the voltage from 3.3v to 5v I use something simple, transistors and resistors. With respect to precision, I still do not need it, what I need is to first move the servo, as could be done with arduino or raspberrypi (setAngle (0) or setAngle (90)), modifying the controller code that replaces the setAngle function (that it is used in arduino or raspberrypi.

0 Kudos