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