Laurence B / Mbed 2 deprecated Motorcontrol

Dependencies:   FastPWM MODSERIAL mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "FastPWM.h"
00003 #include "MODSERIAL.h"
00004 
00005 FastPWM motor1_pwm(D5);
00006 DigitalOut motor1_richting(D4);
00007 FastPWM motor2_pwm(D6);
00008 DigitalOut motor2_richting(D7);
00009 
00010 AnalogIn pot1(A1);
00011 AnalogIn pot2(A0);
00012 MODSERIAL pc(USBTX, USBRX);
00013 
00014 
00015 
00016 int main()
00017 {
00018     int frequency_pwm = 16700; //16.7 kHz PWM
00019         
00020        
00021     motor1_pwm.period(1.0/frequency_pwm); // T = 1/f
00022     
00023     while(true){
00024         
00025         pc.baud(115200);
00026         
00027         float AnalogVoltage1 = pot1.read()*2 - 1;
00028         float AnalogVoltage2 = pot2.read()*2 - 1;
00029         pc.printf("pot1 = %f \t pot2 = %f \r\n", AnalogVoltage1, AnalogVoltage2);
00030         
00031         //Motor1
00032         
00033         if (AnalogVoltage1 <= 0) {
00034             motor1_richting = 0;
00035             motor1_pwm.write(-AnalogVoltage1); //write Duty cycle 
00036             }
00037         if (AnalogVoltage1 >= 0) {
00038             motor1_richting = 1;
00039             motor1_pwm.write(AnalogVoltage1); //write Duty cycle 
00040             }
00041             
00042         //Motor 2
00043             
00044         if (AnalogVoltage2 <= 0) {
00045             motor2_richting = 0;
00046             motor2_pwm.write(-AnalogVoltage2); //write Duty cycle 
00047             }
00048         if (AnalogVoltage2 >= 0) {
00049             motor2_richting = 1;
00050             motor2_pwm.write(AnalogVoltage2); //write Duty cycle 
00051             }
00052             
00053                     
00054              
00055         }
00056 }