Julesnaps / Mbed 2 deprecated Linefollowproject

Dependencies:   m3pi mbed

Committer:
uld
Date:
Tue Oct 04 11:58:03 2022 +0000
Revision:
5:dbd32cb3650a
Parent:
4:803dc29393cc
Child:
6:6865930c1135
Moving variabels same part of program. Adding comments

Who changed what in which revision?

UserRevisionLine numberNew contents of line
magnusmland 0:f562e4f9c29f 1 #include "mbed.h"
magnusmland 0:f562e4f9c29f 2 #include "m3pi.h"
magnusmland 0:f562e4f9c29f 3
mikkelbredholt 3:88a6763a7e44 4 //Mikkel har været her
magnusmland 0:f562e4f9c29f 5 m3pi m3pi;
magnusmland 0:f562e4f9c29f 6
magnusmland 0:f562e4f9c29f 7 // Minimum and maximum motor speeds
magnusmland 0:f562e4f9c29f 8 #define MAX 1.0
magnusmland 0:f562e4f9c29f 9 #define MIN 0
magnusmland 0:f562e4f9c29f 10
magnusmland 0:f562e4f9c29f 11 // PID terms
magnusmland 0:f562e4f9c29f 12 #define P_TERM 1
magnusmland 0:f562e4f9c29f 13 #define I_TERM 0
magnusmland 0:f562e4f9c29f 14 #define D_TERM 20
magnusmland 0:f562e4f9c29f 15
uld 5:dbd32cb3650a 16 // Tresholds
uld 5:dbd32cb3650a 17 #define BATVOLTRESHOLD 3.0
uld 5:dbd32cb3650a 18
magnusmland 0:f562e4f9c29f 19 int main() {
uld 1:5431d59ee324 20
uld 1:5431d59ee324 21
magnusmland 0:f562e4f9c29f 22 m3pi.sensor_auto_calibrate();
uld 5:dbd32cb3650a 23
uld 5:dbd32cb3650a 24 /*Variable initiation*/
magnusmland 0:f562e4f9c29f 25 float right;
magnusmland 0:f562e4f9c29f 26 float left;
magnusmland 0:f562e4f9c29f 27 float current_pos_of_line = 0.0;
magnusmland 0:f562e4f9c29f 28 float previous_pos_of_line = 0.0;
magnusmland 0:f562e4f9c29f 29 float derivative,proportional,integral = 0;
magnusmland 0:f562e4f9c29f 30 float power;
magnusmland 0:f562e4f9c29f 31 float speed = MAX;
magnusmland 0:f562e4f9c29f 32
uld 5:dbd32cb3650a 33 float batVol = m3pi.battery(); //Storing battery voltage in variable
uld 5:dbd32cb3650a 34 float potVol = m3pi.pot_voltage(); //Storing pot voltage
uld 5:dbd32cb3650a 35
uld 5:dbd32cb3650a 36 /*Printing test*/
uld 5:dbd32cb3650a 37 m3pi.cls();
uld 5:dbd32cb3650a 38 m3pi.locate(0,0);
uld 5:dbd32cb3650a 39
uld 5:dbd32cb3650a 40 m3pi.printf("%f.3 ",batVol);
uld 5:dbd32cb3650a 41 m3pi.locate(0,1);
uld 5:dbd32cb3650a 42 m3pi.printf("%f.3 ",b);
uld 5:dbd32cb3650a 43 wait(2.0);
uld 5:dbd32cb3650a 44
magnusmland 0:f562e4f9c29f 45 while (1) {
magnusmland 0:f562e4f9c29f 46
magnusmland 0:f562e4f9c29f 47 // Get the position of the line.
magnusmland 0:f562e4f9c29f 48 current_pos_of_line = m3pi.line_position();
magnusmland 0:f562e4f9c29f 49 proportional = current_pos_of_line;
magnusmland 0:f562e4f9c29f 50
magnusmland 0:f562e4f9c29f 51 // Compute the derivative
magnusmland 0:f562e4f9c29f 52 derivative = current_pos_of_line - previous_pos_of_line;
magnusmland 0:f562e4f9c29f 53
magnusmland 0:f562e4f9c29f 54 // Compute the integral
magnusmland 0:f562e4f9c29f 55 integral += proportional;
magnusmland 0:f562e4f9c29f 56
magnusmland 0:f562e4f9c29f 57 // Remember the last position.
magnusmland 0:f562e4f9c29f 58 previous_pos_of_line = current_pos_of_line;
magnusmland 0:f562e4f9c29f 59
magnusmland 0:f562e4f9c29f 60 // Compute the power
magnusmland 0:f562e4f9c29f 61 power = (proportional * (P_TERM) ) + (integral*(I_TERM)) + (derivative*(D_TERM)) ;
magnusmland 0:f562e4f9c29f 62
magnusmland 0:f562e4f9c29f 63 // Compute new speeds
magnusmland 0:f562e4f9c29f 64 right = speed+power;
magnusmland 0:f562e4f9c29f 65 left = speed-power;
magnusmland 0:f562e4f9c29f 66
magnusmland 0:f562e4f9c29f 67 // limit checks
magnusmland 0:f562e4f9c29f 68 if (right < MIN)
magnusmland 0:f562e4f9c29f 69 right = MIN;
magnusmland 0:f562e4f9c29f 70 else if (right > MAX)
magnusmland 0:f562e4f9c29f 71 right = MAX;
magnusmland 0:f562e4f9c29f 72
magnusmland 0:f562e4f9c29f 73 if (left < MIN)
magnusmland 0:f562e4f9c29f 74 left = MIN;
magnusmland 0:f562e4f9c29f 75 else if (left > MAX)
magnusmland 0:f562e4f9c29f 76 left = MAX;
magnusmland 0:f562e4f9c29f 77
magnusmland 0:f562e4f9c29f 78 // set speed
magnusmland 0:f562e4f9c29f 79 m3pi.left_motor(left);
magnusmland 0:f562e4f9c29f 80 m3pi.right_motor(right);
magnusmland 0:f562e4f9c29f 81
magnusmland 0:f562e4f9c29f 82 }
magnusmland 0:f562e4f9c29f 83 }