Controlling the motors

Dependencies:   FastPWM HIDScope MODSERIAL QEI mbed

Committer:
Mirjam
Date:
Tue Sep 25 14:02:15 2018 +0000
Revision:
4:c73ced5d5754
Parent:
3:7421e53bf9bd
Child:
5:348cd7d2a094
Working version Milestone 1. Hooray!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Mirjam 0:50c494034326 1 #include "mbed.h"
Mirjam 0:50c494034326 2 #include "FastPWM.h"
Mirjam 0:50c494034326 3 #include "MODSERIAL.h"
Mirjam 0:50c494034326 4 #include "HIDScope.h"
Mirjam 0:50c494034326 5
Mirjam 0:50c494034326 6
Mirjam 0:50c494034326 7 AnalogIn potmeter1(PTC10);
Mirjam 0:50c494034326 8 AnalogIn potmeter2(PTC11);
Mirjam 0:50c494034326 9 MODSERIAL pc(USBTX, USBRX);
Mirjam 0:50c494034326 10 //D4 is a digital input for the microcontroller, so should be an digitalOut
Mirjam 0:50c494034326 11 //from the K64F. It will tell the motor shiel to let Motor1 turn clockwise
Mirjam 0:50c494034326 12 //of count clockwise (CW of CCW). D4 for motor 2
Mirjam 0:50c494034326 13 DigitalOut directionM1(D4);
Mirjam 0:50c494034326 14 DigitalOut directionM2(D7);
Mirjam 0:50c494034326 15 //D5 is a PWM input for the motor controller and determines the PWM signal
Mirjam 0:50c494034326 16 //that the motor controller gives to Motor 1. Higher PWM, higer average voltage.
Mirjam 0:50c494034326 17 // D6 for motor 2
Mirjam 0:50c494034326 18 FastPWM motor1_pwm(D5);
Mirjam 0:50c494034326 19 FastPWM motor2_pwm(D6);
Mirjam 0:50c494034326 20
Mirjam 2:840f7aa50e55 21 // Voor het laten zien van de data op de pc.
Mirjam 2:840f7aa50e55 22 HIDScope scope(2); // Aantal kanalen wat doorgegeven wordt aan de hidscope
Mirjam 2:840f7aa50e55 23 Ticker AInTicker;
Mirjam 4:c73ced5d5754 24 float U1;
Mirjam 4:c73ced5d5754 25 float U2;
Mirjam 2:840f7aa50e55 26 float potwaarde1;
Mirjam 2:840f7aa50e55 27 float potwaarde2;
Mirjam 2:840f7aa50e55 28
Mirjam 2:840f7aa50e55 29 void ReadAnalogIn()
Mirjam 2:840f7aa50e55 30 {
Mirjam 4:c73ced5d5754 31 scope.set(0,U1); // Zet de potwaarde in de eerste plot bij de HID scope. Deze wordt automatisch tegen de tijd geplot
Mirjam 4:c73ced5d5754 32 scope.set(1,U2);
Mirjam 2:840f7aa50e55 33 scope.send(); // Zendt de waardes naar de pc
Mirjam 2:840f7aa50e55 34 }
Mirjam 2:840f7aa50e55 35
Mirjam 0:50c494034326 36 int main(void)
Mirjam 0:50c494034326 37 {
Mirjam 1:d6acc3c6261a 38 motor1_pwm.period_ms(60); // period is 60 ms
Mirjam 2:840f7aa50e55 39 AInTicker.attach(&ReadAnalogIn,0.01f); //Elke 0.01 sec. Lees de analoge waarde
Mirjam 2:840f7aa50e55 40
Mirjam 0:50c494034326 41 while(true){
Mirjam 4:c73ced5d5754 42 potwaarde1 = potmeter1.read(); // Lees de potwaardes uit. Tussen 0 en 1
Mirjam 2:840f7aa50e55 43 potwaarde2 = potmeter2.read();
Mirjam 4:c73ced5d5754 44
Mirjam 4:c73ced5d5754 45 U1 = potwaarde1*2 -1; // Scale van -1 tot 1 ipv. 0 tot 1
Mirjam 4:c73ced5d5754 46 U2 = potwaarde2*2 -1;
Mirjam 4:c73ced5d5754 47
Mirjam 4:c73ced5d5754 48 // Gebruik de absolute waarde van de scaled U waardes als input voor de motor.
Mirjam 4:c73ced5d5754 49 // Negatieve waardes kunnen niet naar de motor gestuurd worden.
Mirjam 4:c73ced5d5754 50 motor1_pwm.write(fabs(U1));
Mirjam 4:c73ced5d5754 51 motor2_pwm.write(fabs(U2));
Mirjam 4:c73ced5d5754 52
Mirjam 4:c73ced5d5754 53 directionM1 = U1 > 0.0f; //either true or false
Mirjam 4:c73ced5d5754 54 directionM2 = U2 > 0.0f;
Mirjam 4:c73ced5d5754 55
Mirjam 4:c73ced5d5754 56 wait(0.002f);
Mirjam 0:50c494034326 57 }
Mirjam 0:50c494034326 58
Mirjam 0:50c494034326 59 }