ELCT 302 / Mbed 2 deprecated drivelab3

Dependencies:   mbed

Committer:
benjaminstone
Date:
Mon Feb 01 16:23:33 2021 +0000
Revision:
1:128dc77d025a
Parent:
0:bd04e6ac5a2c
Child:
2:01372422b0c7
updated;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
benjaminstone 0:bd04e6ac5a2c 1 // open-loop control of the servo motor
benjaminstone 0:bd04e6ac5a2c 2 // reads analog input and converts it to a pwm signal
benjaminstone 0:bd04e6ac5a2c 3
benjaminstone 0:bd04e6ac5a2c 4 #include "mbed.h"
benjaminstone 0:bd04e6ac5a2c 5 #include <iostream>
benjaminstone 0:bd04e6ac5a2c 6
benjaminstone 0:bd04e6ac5a2c 7 AnalogIn input(PTB0);
benjaminstone 0:bd04e6ac5a2c 8 PwmOut output(PTE21);
benjaminstone 0:bd04e6ac5a2c 9 Ticker updater;
benjaminstone 1:128dc77d025a 10
benjaminstone 1:128dc77d025a 11 float v = 0.0;
benjaminstone 1:128dc77d025a 12 float d = 0.0;
benjaminstone 0:bd04e6ac5a2c 13
benjaminstone 0:bd04e6ac5a2c 14 void pwmUpdate()
benjaminstone 0:bd04e6ac5a2c 15 {
benjaminstone 0:bd04e6ac5a2c 16 v = input.read();
benjaminstone 1:128dc77d025a 17 d = v; // sets DC to voltage
benjaminstone 0:bd04e6ac5a2c 18 if( abs(d - output.read()) > 0.001) //only updates the duty cycle if there is a change in the duty cycle greater than 0.3%
benjaminstone 0:bd04e6ac5a2c 19 output.write(d);
benjaminstone 0:bd04e6ac5a2c 20 }
benjaminstone 0:bd04e6ac5a2c 21
benjaminstone 0:bd04e6ac5a2c 22 int main(void)
benjaminstone 0:bd04e6ac5a2c 23 {
benjaminstone 0:bd04e6ac5a2c 24 output.period(0.00005f); // 20 kHz control signal
benjaminstone 0:bd04e6ac5a2c 25 // attaches pwmUpdate function to the ticker with a period of 20ms
benjaminstone 1:128dc77d025a 26 updater.attach(&pwmUpdate,0.00005f);
benjaminstone 0:bd04e6ac5a2c 27 std::cout << "AnalogIn: " << v << " Duty Cycle: " << d << std::endl;
benjaminstone 0:bd04e6ac5a2c 28 while(1) {} // do nothing
benjaminstone 0:bd04e6ac5a2c 29 }