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:60f29e7619ae, 2020-10-27 (annotated)
- Committer:
- markschwarzer
- Date:
- Tue Oct 27 22:49:25 2020 +0000
- Revision:
- 0:60f29e7619ae
- Child:
- 1:e1139c05dee9
initial, with edits to code given
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| markschwarzer | 0:60f29e7619ae | 1 | #include "mbed.h" |
| markschwarzer | 0:60f29e7619ae | 2 | |
| markschwarzer | 0:60f29e7619ae | 3 | PwmOut servo(p20); |
| markschwarzer | 0:60f29e7619ae | 4 | DigitalIn switch_input(p17); |
| markschwarzer | 0:60f29e7619ae | 5 | DigitalOut led1(LED1); |
| markschwarzer | 0:60f29e7619ae | 6 | DigitalOut led2(LED2); |
| markschwarzer | 0:60f29e7619ae | 7 | DigitalOut led3(LED3); |
| markschwarzer | 0:60f29e7619ae | 8 | DigitalOut led4(LED4); |
| markschwarzer | 0:60f29e7619ae | 9 | Serial pc(USBTX,USBRX); |
| markschwarzer | 0:60f29e7619ae | 10 | |
| markschwarzer | 0:60f29e7619ae | 11 | //Run through a range of pulse widths, report to serial output: |
| markschwarzer | 0:60f29e7619ae | 12 | void servo_test(float period, float minPulse, float maxPulse, float pulseStep) |
| markschwarzer | 0:60f29e7619ae | 13 | { |
| markschwarzer | 0:60f29e7619ae | 14 | float currentPulse; |
| markschwarzer | 0:60f29e7619ae | 15 | servo.period(period); //set the PWM period |
| markschwarzer | 0:60f29e7619ae | 16 | |
| markschwarzer | 0:60f29e7619ae | 17 | //Vary the pulse width from minimum to maximum in steps. |
| markschwarzer | 0:60f29e7619ae | 18 | pc.printf("Commencing Servo Test\r\n"); |
| markschwarzer | 0:60f29e7619ae | 19 | for (currentPulse=minPulse; currentPulse<maxPulse; currentPulse=currentPulse+pulseStep) { |
| markschwarzer | 0:60f29e7619ae | 20 | servo.pulsewidth(currentPulse/1000000.000); //convert uSec to sec. |
| markschwarzer | 0:60f29e7619ae | 21 | pc.printf("Current pulse width is %f\r\n",currentPulse); |
| markschwarzer | 0:60f29e7619ae | 22 | wait(0.5); |
| markschwarzer | 0:60f29e7619ae | 23 | } |
| markschwarzer | 0:60f29e7619ae | 24 | pc.printf("Servo Test Finished\r\n"); |
| markschwarzer | 0:60f29e7619ae | 25 | } |
| markschwarzer | 0:60f29e7619ae | 26 | |
| markschwarzer | 0:60f29e7619ae | 27 | |
| markschwarzer | 0:60f29e7619ae | 28 | //Sends correct pulse width to servo to achieve desired angle: |
| markschwarzer | 0:60f29e7619ae | 29 | void servo_set_angle(float angle) |
| markschwarzer | 0:60f29e7619ae | 30 | { |
| markschwarzer | 0:60f29e7619ae | 31 | float pulseCoeff = 10.0; |
| markschwarzer | 0:60f29e7619ae | 32 | float pulseOffset = 400; |
| markschwarzer | 0:60f29e7619ae | 33 | float pulseWidth; |
| markschwarzer | 0:60f29e7619ae | 34 | |
| markschwarzer | 0:60f29e7619ae | 35 | //Check to make sure commanded angle is within min-max bounds: |
| markschwarzer | 0:60f29e7619ae | 36 | |
| markschwarzer | 0:60f29e7619ae | 37 | if (angle == 0) { |
| markschwarzer | 0:60f29e7619ae | 38 | angle = 0; |
| markschwarzer | 0:60f29e7619ae | 39 | led1=1; |
| markschwarzer | 0:60f29e7619ae | 40 | } else if (0 < angle <= 90) { |
| markschwarzer | 0:60f29e7619ae | 41 | led2=1; |
| markschwarzer | 0:60f29e7619ae | 42 | } |
| markschwarzer | 0:60f29e7619ae | 43 | if (90 < angle < 180) { |
| markschwarzer | 0:60f29e7619ae | 44 | led3=1; |
| markschwarzer | 0:60f29e7619ae | 45 | } else if (angle == 180) { |
| markschwarzer | 0:60f29e7619ae | 46 | angle = 180.0; |
| markschwarzer | 0:60f29e7619ae | 47 | led4=1; |
| markschwarzer | 0:60f29e7619ae | 48 | } |
| markschwarzer | 0:60f29e7619ae | 49 | |
| markschwarzer | 0:60f29e7619ae | 50 | //Calculate pulsewidth for the desired angle and issue command: |
| markschwarzer | 0:60f29e7619ae | 51 | pulseWidth = pulseCoeff * angle + pulseOffset; |
| markschwarzer | 0:60f29e7619ae | 52 | servo.pulsewidth(pulseWidth/1000000.000); |
| markschwarzer | 0:60f29e7619ae | 53 | } |
| markschwarzer | 0:60f29e7619ae | 54 | |
| markschwarzer | 0:60f29e7619ae | 55 | int main() |
| markschwarzer | 0:60f29e7619ae | 56 | { |
| markschwarzer | 0:60f29e7619ae | 57 | //Leave some time to get connection set up properly |
| markschwarzer | 0:60f29e7619ae | 58 | wait(3); |
| markschwarzer | 0:60f29e7619ae | 59 | //test servo from 0 to 5000 uSec, steps of 500 |
| markschwarzer | 0:60f29e7619ae | 60 | servo_test(0.01,0,3000,50); |
| markschwarzer | 0:60f29e7619ae | 61 | |
| markschwarzer | 0:60f29e7619ae | 62 | //cycle through angles, repeat: |
| markschwarzer | 0:60f29e7619ae | 63 | float angle; |
| markschwarzer | 0:60f29e7619ae | 64 | servo.period(0.01); |
| markschwarzer | 0:60f29e7619ae | 65 | |
| markschwarzer | 0:60f29e7619ae | 66 | while(1) { |
| markschwarzer | 0:60f29e7619ae | 67 | angle = 0; |
| markschwarzer | 0:60f29e7619ae | 68 | pc.printf("Test servo behavior here!"); |
| markschwarzer | 0:60f29e7619ae | 69 | //Run through angles from 0 to 180 ad nauseum: |
| markschwarzer | 0:60f29e7619ae | 70 | for (angle=0.0; angle<180.0; angle=angle+10.0) { |
| markschwarzer | 0:60f29e7619ae | 71 | servo_set_angle(angle); |
| markschwarzer | 0:60f29e7619ae | 72 | pc.printf("Servo Angle: %5.2f\r\n",angle); |
| markschwarzer | 0:60f29e7619ae | 73 | wait(1); |
| markschwarzer | 0:60f29e7619ae | 74 | } |
| markschwarzer | 0:60f29e7619ae | 75 | } |
| markschwarzer | 0:60f29e7619ae | 76 | } |