Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@0:6eab244a5208, 2017-09-22 (annotated)
- Committer:
- sthompson
- Date:
- Fri Sep 22 15:10:54 2017 +0000
- Revision:
- 0:6eab244a5208
3bb
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| sthompson | 0:6eab244a5208 | 1 | /*********************************************************************************** |
| sthompson | 0:6eab244a5208 | 2 | / Test program to demonstrate the Parallax standard and continuous rotation servos |
| sthompson | 0:6eab244a5208 | 3 | / |
| sthompson | 0:6eab244a5208 | 4 | / The program oscillates the standard servo position and oscillates the speed and |
| sthompson | 0:6eab244a5208 | 5 | / direction of the continuous rotation servo |
| sthompson | 0:6eab244a5208 | 6 | / |
| sthompson | 0:6eab244a5208 | 7 | / Due to the smaller scale of proportional speed values for the continuous rotation |
| sthompson | 0:6eab244a5208 | 8 | / servo, its resulting command magnitude is reduced by CR_SCALE |
| sthompson | 0:6eab244a5208 | 9 | / |
| sthompson | 0:6eab244a5208 | 10 | / Due to a slight directional bias, the contuous rotation servo command is centred |
| sthompson | 0:6eab244a5208 | 11 | / about 0.515, rather than the standard 0.5 value |
| sthompson | 0:6eab244a5208 | 12 | / (on a scale of 0.0-1.0, 0.5 represents 'centred' for standard servos and 'stop' |
| sthompson | 0:6eab244a5208 | 13 | / for continuous rotation servos) |
| sthompson | 0:6eab244a5208 | 14 | /***********************************************************************************/ |
| sthompson | 0:6eab244a5208 | 15 | #include "mbed.h" |
| sthompson | 0:6eab244a5208 | 16 | #include "Servo.h" |
| sthompson | 0:6eab244a5208 | 17 | |
| sthompson | 0:6eab244a5208 | 18 | Serial pc(USBTX, USBRX); // USB serial interface |
| sthompson | 0:6eab244a5208 | 19 | Servo cr_srv(p21); // continuous rotation hobby servo |
| sthompson | 0:6eab244a5208 | 20 | Servo std_srv(p22); // standard hobby servo |
| sthompson | 0:6eab244a5208 | 21 | AnalogIn A_in(p20); // analogue input pin for reference values |
| sthompson | 0:6eab244a5208 | 22 | |
| sthompson | 0:6eab244a5208 | 23 | #define CR_SCALE 12.0 |
| sthompson | 0:6eab244a5208 | 24 | |
| sthompson | 0:6eab244a5208 | 25 | void init_servo() { |
| sthompson | 0:6eab244a5208 | 26 | // calibrate the servos for +/-5ms over +/-45deg |
| sthompson | 0:6eab244a5208 | 27 | cr_srv.calibrate(0.0005,45); |
| sthompson | 0:6eab244a5208 | 28 | std_srv.calibrate(0.0005,45); |
| sthompson | 0:6eab244a5208 | 29 | } |
| sthompson | 0:6eab244a5208 | 30 | |
| sthompson | 0:6eab244a5208 | 31 | int main() { |
| sthompson | 0:6eab244a5208 | 32 | |
| sthompson | 0:6eab244a5208 | 33 | float Ain_readings=0.0; |
| sthompson | 0:6eab244a5208 | 34 | float pos_val=0.5; // variable to hold position values |
| sthompson | 0:6eab244a5208 | 35 | float step_size=0.01; // position increment/decrement value |
| sthompson | 0:6eab244a5208 | 36 | float cr_srv_cmd=0.5; // continuous rotation servo command value (range: 0.0 - 1.0) |
| sthompson | 0:6eab244a5208 | 37 | float std_srv_cmd=0.5; // standard servo command value (range: 0.0 - 1.0) |
| sthompson | 0:6eab244a5208 | 38 | |
| sthompson | 0:6eab244a5208 | 39 | // set the USB serial interface baud rate |
| sthompson | 0:6eab244a5208 | 40 | pc.baud(921600); |
| sthompson | 0:6eab244a5208 | 41 | |
| sthompson | 0:6eab244a5208 | 42 | init_servo(); |
| sthompson | 0:6eab244a5208 | 43 | |
| sthompson | 0:6eab244a5208 | 44 | while(1) { |
| sthompson | 0:6eab244a5208 | 45 | // use a sin function to create an oscillating servo postion with values |
| sthompson | 0:6eab244a5208 | 46 | // between 0.0 and 1.0 for the standard servo |
| sthompson | 0:6eab244a5208 | 47 | std_srv_cmd = sin(pos_val*2*3.14159)/2.0+0.5; |
| sthompson | 0:6eab244a5208 | 48 | // use a smaller scale for the continuous rotation servo, due to its limited sensitivity |
| sthompson | 0:6eab244a5208 | 49 | cr_srv_cmd = sin(pos_val*2*3.14159)/CR_SCALE+0.515; |
| sthompson | 0:6eab244a5208 | 50 | |
| sthompson | 0:6eab244a5208 | 51 | // sernd the servo command to the servos themselves |
| sthompson | 0:6eab244a5208 | 52 | cr_srv.write(cr_srv_cmd); // write to the continuous rotation servo |
| sthompson | 0:6eab244a5208 | 53 | std_srv.write(std_srv_cmd); // write to the standard servo |
| sthompson | 0:6eab244a5208 | 54 | |
| sthompson | 0:6eab244a5208 | 55 | // export the readings to a terminal program via the USB cable |
| sthompson | 0:6eab244a5208 | 56 | pc.printf("pos value: %6.4f, cr servo: %6.4f std servo: %6.4f\r", pos_val, cr_srv_cmd, |
| sthompson | 0:6eab244a5208 | 57 | std_srv_cmd); |
| sthompson | 0:6eab244a5208 | 58 | |
| sthompson | 0:6eab244a5208 | 59 | pos_val = Ain_readings + A_in.read(); // increment/decrement the position |
| sthompson | 0:6eab244a5208 | 60 | if (pos_val > 1.0) pos_val = 0.0; // if the position is out of bounds, reset the pos value |
| sthompson | 0:6eab244a5208 | 61 | |
| sthompson | 0:6eab244a5208 | 62 | wait(0.250); // wait for 0.25s to give time for the display to show |
| sthompson | 0:6eab244a5208 | 63 | } |
| sthompson | 0:6eab244a5208 | 64 | } |