Janus Bo Andersen / Mbed 2 deprecated T2PRO1_master

Dependencies:   mbed m3pi

Committer:
Blunsdon
Date:
Thu Nov 22 12:37:09 2018 +0000
Revision:
12:dfc717775297
Parent:
11:394ab193971f
Child:
13:6a8689f601cd
v 5 marc;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
chris 0:78f9794620a3 1 #include "mbed.h"
chris 0:78f9794620a3 2 #include "m3pi.h"
chris 0:78f9794620a3 3
chris 0:78f9794620a3 4 m3pi m3pi;
chris 0:78f9794620a3 5
chris 0:78f9794620a3 6 // Minimum and maximum motor speeds
Blunsdon 1:47ea51d9e25d 7 #define MAX 0.5
chris 0:78f9794620a3 8 #define MIN 0
chris 0:78f9794620a3 9
chris 0:78f9794620a3 10 // PID terms
chris 0:78f9794620a3 11 #define P_TERM 1
chris 0:78f9794620a3 12 #define I_TERM 0
Blunsdon 1:47ea51d9e25d 13 #define D_TERM 8
Blunsdon 1:47ea51d9e25d 14
Blunsdon 12:dfc717775297 15 void sensor_all(int arr[]) //lav en funktion? ved ikke hvad jeg skal her
Blunsdon 1:47ea51d9e25d 16 {
Blunsdon 12:dfc717775297 17 m3pi.putc(0x87); // send noget sensor data
Blunsdon 1:47ea51d9e25d 18
janusboandersen 4:5939f9ad88ca 19 for(int n=0; n < 5; n++) // forloop så sensor for data
Blunsdon 1:47ea51d9e25d 20 {
Blunsdon 1:47ea51d9e25d 21 char lowbyte = m3pi.getc();
Blunsdon 1:47ea51d9e25d 22 char hibyte = m3pi.getc();
Blunsdon 12:dfc717775297 23 arr[n] = ((lowbyte + (hibyte << 8))); // sensor value
Blunsdon 1:47ea51d9e25d 24 }
Blunsdon 9:fa745d3afcac 25 m3pi.cls();
Blunsdon 9:fa745d3afcac 26 m3pi.locate(0,1);
Blunsdon 12:dfc717775297 27 m3pi.printf("%d", arr[0]);
Blunsdon 12:dfc717775297 28 m3pi.locate(0,0);
Blunsdon 12:dfc717775297 29 m3pi.printf("%d", arr[4]);
Blunsdon 11:394ab193971f 30 }
Blunsdon 11:394ab193971f 31
chris 0:78f9794620a3 32
chris 0:78f9794620a3 33 int main() {
janusboandersen 2:d22dcc2bfcc1 34
chris 0:78f9794620a3 35 m3pi.locate(0,1);
chris 0:78f9794620a3 36 m3pi.printf("Line PID");
chris 0:78f9794620a3 37
Blunsdon 1:47ea51d9e25d 38 wait(1.0);
chris 0:78f9794620a3 39
chris 0:78f9794620a3 40 m3pi.sensor_auto_calibrate();
chris 0:78f9794620a3 41
chris 0:78f9794620a3 42 float right;
chris 0:78f9794620a3 43 float left;
chris 0:78f9794620a3 44 float current_pos_of_line = 0.0;
chris 0:78f9794620a3 45 float previous_pos_of_line = 0.0;
chris 0:78f9794620a3 46 float derivative,proportional,integral = 0;
chris 0:78f9794620a3 47 float power;
chris 0:78f9794620a3 48 float speed = MAX;
Blunsdon 12:dfc717775297 49 int sensor_val[5];
chris 0:78f9794620a3 50
chris 0:78f9794620a3 51 while (1) {
Blunsdon 11:394ab193971f 52 // Get the position of the line.
chris 0:78f9794620a3 53 current_pos_of_line = m3pi.line_position();
chris 0:78f9794620a3 54 proportional = current_pos_of_line;
chris 0:78f9794620a3 55
chris 0:78f9794620a3 56 // Compute the derivative
chris 0:78f9794620a3 57 derivative = current_pos_of_line - previous_pos_of_line;
chris 0:78f9794620a3 58
chris 0:78f9794620a3 59 // Compute the integral
chris 0:78f9794620a3 60 integral += proportional;
chris 0:78f9794620a3 61
chris 0:78f9794620a3 62 // Remember the last position.
chris 0:78f9794620a3 63 previous_pos_of_line = current_pos_of_line;
chris 0:78f9794620a3 64
chris 0:78f9794620a3 65 // Compute the power
chris 0:78f9794620a3 66 power = (proportional * (P_TERM) ) + (integral*(I_TERM)) + (derivative*(D_TERM)) ;
chris 0:78f9794620a3 67
chris 0:78f9794620a3 68 // Compute new speeds
chris 0:78f9794620a3 69 right = speed+power;
chris 0:78f9794620a3 70 left = speed-power;
chris 0:78f9794620a3 71
chris 0:78f9794620a3 72 // limit checks
chris 0:78f9794620a3 73 if (right < MIN)
chris 0:78f9794620a3 74 right = MIN;
chris 0:78f9794620a3 75 else if (right > MAX)
chris 0:78f9794620a3 76 right = MAX;
chris 0:78f9794620a3 77
chris 0:78f9794620a3 78 if (left < MIN)
chris 0:78f9794620a3 79 left = MIN;
chris 0:78f9794620a3 80 else if (left > MAX)
chris 0:78f9794620a3 81 left = MAX;
chris 0:78f9794620a3 82
chris 0:78f9794620a3 83 // set speed
chris 0:78f9794620a3 84 m3pi.left_motor(left);
chris 0:78f9794620a3 85 m3pi.right_motor(right);
Blunsdon 11:394ab193971f 86
janusboandersen 2:d22dcc2bfcc1 87 //Marc's sensor test
Blunsdon 12:dfc717775297 88 sensor_all(sensor_val); // sensor_value gets fkt value
Blunsdon 12:dfc717775297 89 if ((sensor_val[0] > 800) && (sensor_val[4] > 800))
Blunsdon 12:dfc717775297 90 {
Blunsdon 11:394ab193971f 91 speed = 0.2;
Blunsdon 11:394ab193971f 92 }
Blunsdon 12:dfc717775297 93 if ((sensor_val[0] > 800) && (sensor_val[4] > 150))
Blunsdon 12:dfc717775297 94 {
Blunsdon 12:dfc717775297 95 speed = 0.8;
Blunsdon 12:dfc717775297 96 }
janusboandersen 5:527c046c7b8a 97 /*
janusboandersen 2:d22dcc2bfcc1 98 sensor_value4 = sensor_all(4); // sensor_value gets fkt value
janusboandersen 6:3be33adda32d 99 */
janusboandersen 7:30c63d6460f6 100
janusboandersen 6:3be33adda32d 101 /*
janusboandersen 2:d22dcc2bfcc1 102 m3pi.printf("%d", sensor_value0); // prints one of sidesensors value
janusboandersen 2:d22dcc2bfcc1 103 m3pi.locate(0,1);
janusboandersen 2:d22dcc2bfcc1 104 m3pi.printf("%d", sensor_value4); // prints the other sidesensor value
janusboandersen 3:47968be0df37 105 */
chris 0:78f9794620a3 106
chris 0:78f9794620a3 107 }
chris 0:78f9794620a3 108 }