Sean Lane / Mbed 2 deprecated LANE_A6_1

Dependencies:   mbed

Committer:
seanlane
Date:
Tue Nov 03 20:25:37 2020 +0000
Revision:
1:515b6f9e29b4
Parent:
0:01b067538192
Date Change

Who changed what in which revision?

UserRevisionLine numberNew contents of line
seanlane 1:515b6f9e29b4 1 //Sean Lane, 100648027, 11/03/2020
seanlane 0:01b067538192 2 //A6_1: PWM and Servo Control
seanlane 0:01b067538192 3 #include "mbed.h"
seanlane 0:01b067538192 4 PwmOut servo(p23);
seanlane 0:01b067538192 5 DigitalOut myled1(LED1);
seanlane 0:01b067538192 6 DigitalOut myled2(LED2);
seanlane 0:01b067538192 7 DigitalOut myled3(LED3);
seanlane 0:01b067538192 8 DigitalIn button(p17);
seanlane 0:01b067538192 9 Serial pc(USBTX,USBRX);
seanlane 0:01b067538192 10 float newangle;
seanlane 0:01b067538192 11
seanlane 0:01b067538192 12 //Run through a range of pulse widths, report to serial output:
seanlane 0:01b067538192 13 void servo_test(float period, float minPulse, float maxPulse, float pulseStep)
seanlane 0:01b067538192 14 {
seanlane 0:01b067538192 15 float currentPulse;
seanlane 0:01b067538192 16 servo.period(period); //set the PWM period
seanlane 0:01b067538192 17
seanlane 0:01b067538192 18 //Vary the pulse width from minimum to maximum in steps.
seanlane 0:01b067538192 19 pc.printf("Commencing Servo Test\r\n");
seanlane 0:01b067538192 20 for (currentPulse=minPulse; currentPulse<maxPulse; currentPulse=currentPulse+pulseStep) {
seanlane 0:01b067538192 21 servo.pulsewidth(currentPulse/1000000.000); //convert uSec to sec.
seanlane 0:01b067538192 22 pc.printf("Current pulse width is %f\r\n",currentPulse);
seanlane 0:01b067538192 23 wait(0.5);
seanlane 0:01b067538192 24 }
seanlane 0:01b067538192 25 pc.printf("Servo Test Finished\r\n");
seanlane 0:01b067538192 26 }
seanlane 0:01b067538192 27
seanlane 0:01b067538192 28
seanlane 0:01b067538192 29 //Sends correct pulse width to servo to achieve desired angle:
seanlane 0:01b067538192 30 void servo_set_angle(float angle)
seanlane 0:01b067538192 31 {
seanlane 0:01b067538192 32 float pulseCoeff = 10.0;
seanlane 0:01b067538192 33 float pulseOffset = 400;
seanlane 0:01b067538192 34 float pulseWidth;
seanlane 0:01b067538192 35
seanlane 0:01b067538192 36 //Check to make sure commanded angle is within min-max bounds:
seanlane 0:01b067538192 37 if (angle < 0) {
seanlane 0:01b067538192 38 angle = 0;
seanlane 0:01b067538192 39 } else if (angle > 180) {
seanlane 0:01b067538192 40 angle = 180.0;
seanlane 0:01b067538192 41 }
seanlane 0:01b067538192 42
seanlane 0:01b067538192 43 //Calculate pulsewidth for the desired angle and issue command:
seanlane 0:01b067538192 44 pulseWidth = pulseCoeff * angle + pulseOffset;
seanlane 0:01b067538192 45 servo.pulsewidth(pulseWidth/1000000.000);
seanlane 0:01b067538192 46 }
seanlane 0:01b067538192 47
seanlane 0:01b067538192 48 int main()
seanlane 0:01b067538192 49 {
seanlane 0:01b067538192 50 bool bs;
seanlane 0:01b067538192 51 //Leave some time to get connection set up properly
seanlane 0:01b067538192 52 wait(3);
seanlane 0:01b067538192 53 //test servo from 0 to 5000 uSec, steps of 500
seanlane 0:01b067538192 54 servo_test(0.01,0,3000,50);
seanlane 0:01b067538192 55
seanlane 0:01b067538192 56 //cycle through angles, repeat:
seanlane 0:01b067538192 57 float angle;
seanlane 0:01b067538192 58 servo.period(0.01);
seanlane 0:01b067538192 59
seanlane 0:01b067538192 60 while(1) {
seanlane 0:01b067538192 61 bs=button.read();
seanlane 0:01b067538192 62 angle = 0;
seanlane 0:01b067538192 63 pc.printf("Test servo behavior here!");
seanlane 0:01b067538192 64 //Run through angles from 0 to 180 ad nauseum:
seanlane 0:01b067538192 65 for (angle=0.0; angle<180.0; newangle=angle+30.0) {
seanlane 0:01b067538192 66 servo_set_angle(newangle);
seanlane 0:01b067538192 67 if (bs==1) {
seanlane 0:01b067538192 68 pc.printf("Button press detected");
seanlane 0:01b067538192 69 if (newangle=0.0)
seanlane 0:01b067538192 70 myled1=1;
seanlane 0:01b067538192 71 pc.printf("Servo Angle: %5.2f\r\n",newangle);
seanlane 0:01b067538192 72 if (newangle>0.0 and newangle>=90.0)
seanlane 0:01b067538192 73 myled2=1;
seanlane 0:01b067538192 74 pc.printf("Servo Angle: %5.2f\r\n",newangle);
seanlane 0:01b067538192 75 if (newangle>90.0 and newangle>=180.0)
seanlane 0:01b067538192 76 myled3=1;
seanlane 0:01b067538192 77 pc.printf("Servo Angle: %5.2f\r\n",newangle);
seanlane 0:01b067538192 78 if (bs==0) {
seanlane 0:01b067538192 79 pc.printf("Servo Angle: %5.2f\r\n",angle);
seanlane 0:01b067538192 80 pc.printf("No button press detected");
seanlane 0:01b067538192 81 wait(1);
seanlane 0:01b067538192 82 }
seanlane 0:01b067538192 83 }
seanlane 0:01b067538192 84 }
seanlane 0:01b067538192 85 }
seanlane 0:01b067538192 86 }