ELCT 302 / Mbed 2 deprecated drivelab3

Dependencies:   mbed

Committer:
benjaminstone
Date:
Mon Feb 01 16:01:48 2021 +0000
Revision:
0:bd04e6ac5a2c
Child:
1:128dc77d025a
code for drive team

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 0:bd04e6ac5a2c 10
benjaminstone 0:bd04e6ac5a2c 11 const double fullSpeed = .9; // duty cycle for full speed
benjaminstone 0:bd04e6ac5a2c 12 const double slowSpeed = .4; // duty cycle for slow speed
benjaminstone 0:bd04e6ac5a2c 13 float v = 0;
benjaminstone 0:bd04e6ac5a2c 14 float d = fullSpeed;
benjaminstone 0:bd04e6ac5a2c 15
benjaminstone 0:bd04e6ac5a2c 16 void pwmUpdate()
benjaminstone 0:bd04e6ac5a2c 17 {
benjaminstone 0:bd04e6ac5a2c 18 v = input.read();
benjaminstone 0:bd04e6ac5a2c 19 d = slowSpeed + v*(fullSpeed - slowSpeed);; // calculates duty cycle from input
benjaminstone 0:bd04e6ac5a2c 20 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 21 output.write(d);
benjaminstone 0:bd04e6ac5a2c 22 }
benjaminstone 0:bd04e6ac5a2c 23
benjaminstone 0:bd04e6ac5a2c 24 int main(void)
benjaminstone 0:bd04e6ac5a2c 25 {
benjaminstone 0:bd04e6ac5a2c 26 output.period(0.00005f); // 20 kHz control signal
benjaminstone 0:bd04e6ac5a2c 27 // attaches pwmUpdate function to the ticker with a period of 20ms
benjaminstone 0:bd04e6ac5a2c 28 updater.attach(&pwmUpdate,0.02f);
benjaminstone 0:bd04e6ac5a2c 29 std::cout << "AnalogIn: " << v << " Duty Cycle: " << d << std::endl;
benjaminstone 0:bd04e6ac5a2c 30 while(1) {} // do nothing
benjaminstone 0:bd04e6ac5a2c 31 }