Potreaders op motoren
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 #include "MODSERIAL.h" 00003 MODSERIAL pc(USBTX, USBRX); 00004 DigitalOut DirectionPin1(D4); 00005 DigitalOut DirectionPin2(D7); 00006 PwmOut PwmPin1(D5); 00007 PwmOut PwmPin2(D6); 00008 DigitalIn Knop1(D2); 00009 AnalogIn pot1 (A5); 00010 AnalogIn pot2 (A4); 00011 00012 Ticker mycontrollerTicker1; 00013 Ticker mycontrollerTicker2; 00014 Ticker Velo1; 00015 Ticker Velo2; 00016 00017 //const float maxVelocity=8.4; // in rad/s 00018 volatile float referenceVelocity1 = 0.5; //dit is de gecentreerde waarde en dus de nulstand 00019 volatile float referenceVelocity2 = 0.5; 00020 00021 void velocity1() 00022 { 00023 if ((pot1.read()>0.5f) || (Knop1 == true))//gezeik met die knop doet het niet ff uitzoeken nog 00024 { 00025 // Clockwise rotation 00026 referenceVelocity1 = (pot1.read()-0.5f) * 2.0f; 00027 } 00028 00029 else if (pot1.read() == 0.5f || !Knop1 == (pot1.read()<0.5f)) 00030 { 00031 referenceVelocity1 = pot1.read() * 0.0f; 00032 } 00033 00034 else if (pot1.read() < 0.5f) 00035 { 00036 // Counterclockwise rotation 00037 referenceVelocity1 = 2.0f * (pot1.read()-0.5f) ; 00038 } 00039 } 00040 00041 void velocity2() 00042 { 00043 if (pot2.read()>0.5f) 00044 { 00045 // Clockwise rotation 00046 referenceVelocity2 = (pot2.read()-0.5f) * 2.0f; 00047 } 00048 00049 else if (pot2.read() == 0.5f) 00050 { 00051 referenceVelocity2 = pot2.read() * 0.0f; 00052 } 00053 00054 else if (pot2.read() < 0.5f) 00055 { 00056 // Counterclockwise rotation 00057 referenceVelocity2 = 2.0f * (pot2.read()-0.5f) ; 00058 } 00059 } 00060 00061 void motor1() 00062 { 00063 float u = referenceVelocity1; 00064 DirectionPin1 = u < 0.0f; 00065 PwmPin1 = fabs(u); 00066 } 00067 00068 void motor2() 00069 { 00070 float u = referenceVelocity2; 00071 DirectionPin2 = u > 0.0f; 00072 PwmPin2 = fabs(u); 00073 } 00074 00075 int main() 00076 { 00077 pc.baud(115200); 00078 PwmPin1.period_us(120); //60 microseconds pwm period, 16.7 kHz 00079 mycontrollerTicker1.attach(motor1, 0.002);//500Hz 00080 Velo1.attach(velocity1, 0.002); 00081 mycontrollerTicker2.attach(motor2, 0.002); 00082 Velo2.attach(velocity2, 0.002); 00083 00084 while(true) 00085 { 00086 } 00087 }
Generated on Tue Aug 2 2022 02:54:14 by
1.7.2