ELCT 302 / Mbed 2 deprecated drivelab3

Dependencies:   mbed

Revision:
0:bd04e6ac5a2c
Child:
1:128dc77d025a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Feb 01 16:01:48 2021 +0000
@@ -0,0 +1,31 @@
+// open-loop control of the servo motor
+// reads analog input and converts it to a pwm signal
+ 
+#include "mbed.h"
+#include <iostream>
+ 
+AnalogIn input(PTB0);
+PwmOut output(PTE21);
+Ticker updater;
+ 
+const double fullSpeed = .9; // duty cycle for full speed
+const double slowSpeed = .4; // duty cycle for slow speed
+float v = 0;
+float d = fullSpeed;
+ 
+void pwmUpdate()
+{
+    v = input.read();
+    d = slowSpeed + v*(fullSpeed - slowSpeed);; // calculates duty cycle from input
+    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%
+         output.write(d);
+}
+ 
+int main(void)
+{
+    output.period(0.00005f); // 20 kHz control signal
+    // attaches pwmUpdate function to the ticker with a period of 20ms
+    updater.attach(&pwmUpdate,0.02f);
+    std::cout << "AnalogIn: " << v << " Duty Cycle: " << d << std::endl;
+    while(1) {} // do nothing
+}
\ No newline at end of file