ELCT 302 / Mbed 2 deprecated drivelab3

Dependencies:   mbed

main.cpp

Committer:
benjaminstone
Date:
2021-02-01
Revision:
0:bd04e6ac5a2c
Child:
1:128dc77d025a

File content as of revision 0:bd04e6ac5a2c:

// 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
}