Julesnaps / Mbed 2 deprecated Linefollowproject

Dependencies:   m3pi mbed

Committer:
uld
Date:
Tue Oct 04 11:48:01 2022 +0000
Revision:
2:5369f34d061e
Parent:
1:5431d59ee324
Child:
4:803dc29393cc
ny og bedre kommentar

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
magnusmland 0:f562e4f9c29f 4 m3pi m3pi;
magnusmland 0:f562e4f9c29f 5
magnusmland 0:f562e4f9c29f 6 // Minimum and maximum motor speeds
magnusmland 0:f562e4f9c29f 7 #define MAX 1.0
magnusmland 0:f562e4f9c29f 8 #define MIN 0
magnusmland 0:f562e4f9c29f 9
magnusmland 0:f562e4f9c29f 10 // PID terms
magnusmland 0:f562e4f9c29f 11 #define P_TERM 1
magnusmland 0:f562e4f9c29f 12 #define I_TERM 0
magnusmland 0:f562e4f9c29f 13 #define D_TERM 20
magnusmland 0:f562e4f9c29f 14
magnusmland 0:f562e4f9c29f 15 int main() {
magnusmland 0:f562e4f9c29f 16 float a=m3pi.battery();
magnusmland 0:f562e4f9c29f 17 float b=m3pi.pot_voltage();
magnusmland 0:f562e4f9c29f 18 m3pi.locate(0,0);
magnusmland 0:f562e4f9c29f 19
magnusmland 0:f562e4f9c29f 20
magnusmland 0:f562e4f9c29f 21 m3pi.cls();
magnusmland 0:f562e4f9c29f 22 m3pi.printf("%f.3 ",a);
magnusmland 0:f562e4f9c29f 23 m3pi.locate(0,1);
magnusmland 0:f562e4f9c29f 24 m3pi.printf("%f.3 ",b);
uld 1:5431d59ee324 25
uld 1:5431d59ee324 26
uld 1:5431d59ee324 27 // dette er en mega god kommentar 13:44
uld 2:5369f34d061e 28 // denen er endnu bedre
magnusmland 0:f562e4f9c29f 29
magnusmland 0:f562e4f9c29f 30 wait(2.0);
magnusmland 0:f562e4f9c29f 31
magnusmland 0:f562e4f9c29f 32 m3pi.sensor_auto_calibrate();
magnusmland 0:f562e4f9c29f 33
magnusmland 0:f562e4f9c29f 34 float right;
magnusmland 0:f562e4f9c29f 35 float left;
magnusmland 0:f562e4f9c29f 36 float current_pos_of_line = 0.0;
magnusmland 0:f562e4f9c29f 37 float previous_pos_of_line = 0.0;
magnusmland 0:f562e4f9c29f 38 float derivative,proportional,integral = 0;
magnusmland 0:f562e4f9c29f 39 float power;
magnusmland 0:f562e4f9c29f 40 float speed = MAX;
magnusmland 0:f562e4f9c29f 41
magnusmland 0:f562e4f9c29f 42 while (1) {
magnusmland 0:f562e4f9c29f 43
magnusmland 0:f562e4f9c29f 44 // Get the position of the line.
magnusmland 0:f562e4f9c29f 45 current_pos_of_line = m3pi.line_position();
magnusmland 0:f562e4f9c29f 46 proportional = current_pos_of_line;
magnusmland 0:f562e4f9c29f 47
magnusmland 0:f562e4f9c29f 48 // Compute the derivative
magnusmland 0:f562e4f9c29f 49 derivative = current_pos_of_line - previous_pos_of_line;
magnusmland 0:f562e4f9c29f 50
magnusmland 0:f562e4f9c29f 51 // Compute the integral
magnusmland 0:f562e4f9c29f 52 integral += proportional;
magnusmland 0:f562e4f9c29f 53
magnusmland 0:f562e4f9c29f 54 // Remember the last position.
magnusmland 0:f562e4f9c29f 55 previous_pos_of_line = current_pos_of_line;
magnusmland 0:f562e4f9c29f 56
magnusmland 0:f562e4f9c29f 57 // Compute the power
magnusmland 0:f562e4f9c29f 58 power = (proportional * (P_TERM) ) + (integral*(I_TERM)) + (derivative*(D_TERM)) ;
magnusmland 0:f562e4f9c29f 59
magnusmland 0:f562e4f9c29f 60 // Compute new speeds
magnusmland 0:f562e4f9c29f 61 right = speed+power;
magnusmland 0:f562e4f9c29f 62 left = speed-power;
magnusmland 0:f562e4f9c29f 63
magnusmland 0:f562e4f9c29f 64 // limit checks
magnusmland 0:f562e4f9c29f 65 if (right < MIN)
magnusmland 0:f562e4f9c29f 66 right = MIN;
magnusmland 0:f562e4f9c29f 67 else if (right > MAX)
magnusmland 0:f562e4f9c29f 68 right = MAX;
magnusmland 0:f562e4f9c29f 69
magnusmland 0:f562e4f9c29f 70 if (left < MIN)
magnusmland 0:f562e4f9c29f 71 left = MIN;
magnusmland 0:f562e4f9c29f 72 else if (left > MAX)
magnusmland 0:f562e4f9c29f 73 left = MAX;
magnusmland 0:f562e4f9c29f 74
magnusmland 0:f562e4f9c29f 75 // set speed
magnusmland 0:f562e4f9c29f 76 m3pi.left_motor(left);
magnusmland 0:f562e4f9c29f 77 m3pi.right_motor(right);
magnusmland 0:f562e4f9c29f 78
magnusmland 0:f562e4f9c29f 79 }
magnusmland 0:f562e4f9c29f 80 }