Programming Milestone 1 Group 7 BMT M9: Making robots move at different velocities/directions by adjusting potmeter.

Dependencies:   FastPWM MODSERIAL mbed

Fork of Programming_Milestone_1 by Marie-Laure Snijders

Committer:
brass_phoenix
Date:
Mon Oct 15 10:11:21 2018 +0000
Revision:
7:e4b475fceb86
Parent:
6:b5fc5007f228
Child:
8:e69799b5cce1
* Refactoring. Works the same as the previous commit.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MAHCSnijders 0:1e216b50d323 1 #include "mbed.h"
MAHCSnijders 1:67b19c59b8c9 2 #include "FastPWM.h"
MAHCSnijders 1:67b19c59b8c9 3 #include "MODSERIAL.h"
MAHCSnijders 1:67b19c59b8c9 4
brass_phoenix 7:e4b475fceb86 5 Ticker motorTicker; // Ticker function
MAHCSnijders 2:6d3f3d1bcef7 6 FastPWM pwmpin1(D5); // SPECIFIC PIN (hoeft niet aangesloten te worden) Tells you how fast the motor has to go (later: pwmpin.write will tell you the duty cycle, aka how much voltage the motor gets)
MAHCSnijders 2:6d3f3d1bcef7 7 FastPWM pwmpin2(D6); // SPECIFIC PIN (hoeft niet aangesloten te worden) Tells you how fast the motor has to go (later: pwmpin.write will tell you the duty cycle, aka how much voltage the motor gets)
MAHCSnijders 1:67b19c59b8c9 8 DigitalOut directionpin1(D4); // SPECIFIC PIN (hoeft niet aangesloten te worden) Direction value (0-1) that the mbed will give the motor: in which direction the motor must rotate
MAHCSnijders 1:67b19c59b8c9 9 DigitalOut directionpin2(D7); // SPECIFIC PIN (hoeft niet aangesloten te worden) Direction value (0-1) that the mbed will give the motor: in which direction the motor must rotate
MAHCSnijders 6:b5fc5007f228 10 AnalogIn potmeter1(A4); // Analoge input van potmeter 1 -> Motor 1
MAHCSnijders 6:b5fc5007f228 11 AnalogIn potmeter2(A2); // Analoge input van potmeter 2 -> Motor 2
MAHCSnijders 0:1e216b50d323 12
brass_phoenix 7:e4b475fceb86 13
brass_phoenix 7:e4b475fceb86 14 // Updates a motor connected to the specified pins with the given speed.
brass_phoenix 7:e4b475fceb86 15 // The speed can be both positive and negative.
brass_phoenix 7:e4b475fceb86 16 void update_motor(DigitalOut dir, FastPWM pwm, float speed) {
brass_phoenix 7:e4b475fceb86 17 // either true or false, determines direction (0 or 1)
brass_phoenix 7:e4b475fceb86 18 dir = speed > 0;
brass_phoenix 7:e4b475fceb86 19 // pwm duty cycle can only be positive, floating point absolute value (if value is >0, the there still will be a positive value).
brass_phoenix 7:e4b475fceb86 20 pwm = fabs(speed);
brass_phoenix 7:e4b475fceb86 21 }
MAHCSnijders 2:6d3f3d1bcef7 22
brass_phoenix 7:e4b475fceb86 23 // Normalizes a potmeter value from it's original range of [0, 1] to [-1, 1]
brass_phoenix 7:e4b475fceb86 24 float normalize_pot(float pot_value) {
brass_phoenix 7:e4b475fceb86 25 // scales value potmeter from 0-1 to -1 - 1.
brass_phoenix 7:e4b475fceb86 26 return pot_value * 2 - 1;
brass_phoenix 7:e4b475fceb86 27 };
brass_phoenix 7:e4b475fceb86 28
brass_phoenix 7:e4b475fceb86 29
brass_phoenix 7:e4b475fceb86 30 void motorfunction() {
brass_phoenix 7:e4b475fceb86 31 // reads out value potmeter 1 between 0-1
brass_phoenix 7:e4b475fceb86 32 float pot1 = potmeter1.read();
brass_phoenix 7:e4b475fceb86 33 float motor1_speed = normalize_pot(pot1);
brass_phoenix 7:e4b475fceb86 34 // reads out value potmeter 2 between 0-1
brass_phoenix 7:e4b475fceb86 35 float pot2 = potmeter2.read();
brass_phoenix 7:e4b475fceb86 36 float motor2_speed = normalize_pot(pot2);
brass_phoenix 7:e4b475fceb86 37
brass_phoenix 7:e4b475fceb86 38 // Update both motors.
brass_phoenix 7:e4b475fceb86 39 update_motor(directionpin1, pwmpin1, motor1_speed);
brass_phoenix 7:e4b475fceb86 40 update_motor(directionpin2, pwmpin2, motor2_speed);
MAHCSnijders 1:67b19c59b8c9 41 }
MAHCSnijders 0:1e216b50d323 42
brass_phoenix 7:e4b475fceb86 43
MAHCSnijders 0:1e216b50d323 44 int main()
MAHCSnijders 0:1e216b50d323 45 {
brass_phoenix 7:e4b475fceb86 46 pwmpin1.period_us(60.0); // 60 microseconds PWM period, 16.7 kHz, defines all PWM pins (only needs to be done once)
brass_phoenix 7:e4b475fceb86 47 motorTicker.attach(motorfunction,0.5);
MAHCSnijders 4:a4cd6f9c851d 48 while(true){} //Lege while loop zodat functie niet afloopt
MAHCSnijders 0:1e216b50d323 49 }