Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@11:394ab193971f, 2018-11-21 (annotated)
- Committer:
- Blunsdon
- Date:
- Wed Nov 21 13:02:46 2018 +0000
- Revision:
- 11:394ab193971f
- Parent:
- 10:d18768e771ac
- Child:
- 12:dfc717775297
v 3 marc;
Who changed what in which revision?
User | Revision | Line number | New 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 | 11:394ab193971f | 15 | int sensor_all(void) //lav en funktion? ved ikke hvad jeg skal her |
Blunsdon | 1:47ea51d9e25d | 16 | { |
Blunsdon | 1:47ea51d9e25d | 17 | int sensor_value[5]; // sensor int |
Blunsdon | 1:47ea51d9e25d | 18 | m3pi.putc(0x86); // send noget sensor data |
Blunsdon | 1:47ea51d9e25d | 19 | |
janusboandersen | 4:5939f9ad88ca | 20 | for(int n=0; n < 5; n++) // forloop så sensor for data |
Blunsdon | 1:47ea51d9e25d | 21 | { |
Blunsdon | 1:47ea51d9e25d | 22 | char lowbyte = m3pi.getc(); |
Blunsdon | 1:47ea51d9e25d | 23 | char hibyte = m3pi.getc(); |
Blunsdon | 1:47ea51d9e25d | 24 | sensor_value[n] = ((lowbyte + (hibyte << 8))); // sensor value |
Blunsdon | 1:47ea51d9e25d | 25 | } |
Blunsdon | 9:fa745d3afcac | 26 | m3pi.cls(); |
Blunsdon | 9:fa745d3afcac | 27 | m3pi.locate(0,1); |
Blunsdon | 9:fa745d3afcac | 28 | m3pi.printf("%d", sensor_value[0]); |
Blunsdon | 11:394ab193971f | 29 | return sensor_value[0]; |
Blunsdon | 11:394ab193971f | 30 | } |
Blunsdon | 11:394ab193971f | 31 | |
Blunsdon | 11:394ab193971f | 32 | int sensor_all2(void) //lav en funktion? ved ikke hvad jeg skal her |
Blunsdon | 11:394ab193971f | 33 | { |
Blunsdon | 11:394ab193971f | 34 | int sensor_value[5]; // sensor int |
Blunsdon | 11:394ab193971f | 35 | m3pi.putc(0x86); // send noget sensor data |
Blunsdon | 11:394ab193971f | 36 | |
Blunsdon | 11:394ab193971f | 37 | for(int n=0; n < 5; n++) // forloop så sensor for data |
Blunsdon | 11:394ab193971f | 38 | { |
Blunsdon | 11:394ab193971f | 39 | char lowbyte = m3pi.getc(); |
Blunsdon | 11:394ab193971f | 40 | char hibyte = m3pi.getc(); |
Blunsdon | 11:394ab193971f | 41 | sensor_value[n] = ((lowbyte + (hibyte << 8))); // sensor value |
Blunsdon | 11:394ab193971f | 42 | } |
Blunsdon | 11:394ab193971f | 43 | m3pi.cls(); |
Blunsdon | 11:394ab193971f | 44 | m3pi.locate(0,0); |
Blunsdon | 11:394ab193971f | 45 | m3pi.printf("%d", sensor_value[4]); |
Blunsdon | 11:394ab193971f | 46 | return sensor_value[4]; |
Blunsdon | 11:394ab193971f | 47 | } |
chris | 0:78f9794620a3 | 48 | |
chris | 0:78f9794620a3 | 49 | int main() { |
janusboandersen | 2:d22dcc2bfcc1 | 50 | |
chris | 0:78f9794620a3 | 51 | m3pi.locate(0,1); |
chris | 0:78f9794620a3 | 52 | m3pi.printf("Line PID"); |
chris | 0:78f9794620a3 | 53 | |
Blunsdon | 1:47ea51d9e25d | 54 | wait(1.0); |
chris | 0:78f9794620a3 | 55 | |
chris | 0:78f9794620a3 | 56 | m3pi.sensor_auto_calibrate(); |
chris | 0:78f9794620a3 | 57 | |
chris | 0:78f9794620a3 | 58 | float right; |
chris | 0:78f9794620a3 | 59 | float left; |
chris | 0:78f9794620a3 | 60 | float current_pos_of_line = 0.0; |
chris | 0:78f9794620a3 | 61 | float previous_pos_of_line = 0.0; |
chris | 0:78f9794620a3 | 62 | float derivative,proportional,integral = 0; |
chris | 0:78f9794620a3 | 63 | float power; |
chris | 0:78f9794620a3 | 64 | float speed = MAX; |
Blunsdon | 11:394ab193971f | 65 | int sensor_val = 0; |
Blunsdon | 11:394ab193971f | 66 | int sensor_val2 = 0; |
chris | 0:78f9794620a3 | 67 | |
chris | 0:78f9794620a3 | 68 | while (1) { |
Blunsdon | 11:394ab193971f | 69 | // Get the position of the line. |
chris | 0:78f9794620a3 | 70 | current_pos_of_line = m3pi.line_position(); |
chris | 0:78f9794620a3 | 71 | proportional = current_pos_of_line; |
chris | 0:78f9794620a3 | 72 | |
chris | 0:78f9794620a3 | 73 | // Compute the derivative |
chris | 0:78f9794620a3 | 74 | derivative = current_pos_of_line - previous_pos_of_line; |
chris | 0:78f9794620a3 | 75 | |
chris | 0:78f9794620a3 | 76 | // Compute the integral |
chris | 0:78f9794620a3 | 77 | integral += proportional; |
chris | 0:78f9794620a3 | 78 | |
chris | 0:78f9794620a3 | 79 | // Remember the last position. |
chris | 0:78f9794620a3 | 80 | previous_pos_of_line = current_pos_of_line; |
chris | 0:78f9794620a3 | 81 | |
chris | 0:78f9794620a3 | 82 | // Compute the power |
chris | 0:78f9794620a3 | 83 | power = (proportional * (P_TERM) ) + (integral*(I_TERM)) + (derivative*(D_TERM)) ; |
chris | 0:78f9794620a3 | 84 | |
chris | 0:78f9794620a3 | 85 | // Compute new speeds |
chris | 0:78f9794620a3 | 86 | right = speed+power; |
chris | 0:78f9794620a3 | 87 | left = speed-power; |
chris | 0:78f9794620a3 | 88 | |
chris | 0:78f9794620a3 | 89 | // limit checks |
chris | 0:78f9794620a3 | 90 | if (right < MIN) |
chris | 0:78f9794620a3 | 91 | right = MIN; |
chris | 0:78f9794620a3 | 92 | else if (right > MAX) |
chris | 0:78f9794620a3 | 93 | right = MAX; |
chris | 0:78f9794620a3 | 94 | |
chris | 0:78f9794620a3 | 95 | if (left < MIN) |
chris | 0:78f9794620a3 | 96 | left = MIN; |
chris | 0:78f9794620a3 | 97 | else if (left > MAX) |
chris | 0:78f9794620a3 | 98 | left = MAX; |
chris | 0:78f9794620a3 | 99 | |
chris | 0:78f9794620a3 | 100 | // set speed |
chris | 0:78f9794620a3 | 101 | m3pi.left_motor(left); |
chris | 0:78f9794620a3 | 102 | m3pi.right_motor(right); |
Blunsdon | 11:394ab193971f | 103 | |
janusboandersen | 2:d22dcc2bfcc1 | 104 | //Marc's sensor test |
Blunsdon | 11:394ab193971f | 105 | sensor_val = sensor_all(); // sensor_value gets fkt value |
Blunsdon | 11:394ab193971f | 106 | sensor_val2 = sensor_all(); // sensor_value gets fkt value |
Blunsdon | 11:394ab193971f | 107 | if ((sensor_val > 1500) && (sensor_val2 > 1500)) |
Blunsdon | 11:394ab193971f | 108 | { |
Blunsdon | 11:394ab193971f | 109 | speed = 0.2; |
Blunsdon | 11:394ab193971f | 110 | } |
janusboandersen | 5:527c046c7b8a | 111 | /* |
janusboandersen | 2:d22dcc2bfcc1 | 112 | sensor_value4 = sensor_all(4); // sensor_value gets fkt value |
janusboandersen | 6:3be33adda32d | 113 | */ |
janusboandersen | 7:30c63d6460f6 | 114 | |
janusboandersen | 6:3be33adda32d | 115 | /* |
janusboandersen | 2:d22dcc2bfcc1 | 116 | m3pi.printf("%d", sensor_value0); // prints one of sidesensors value |
janusboandersen | 2:d22dcc2bfcc1 | 117 | m3pi.locate(0,1); |
janusboandersen | 2:d22dcc2bfcc1 | 118 | m3pi.printf("%d", sensor_value4); // prints the other sidesensor value |
janusboandersen | 3:47968be0df37 | 119 | */ |
chris | 0:78f9794620a3 | 120 | |
chris | 0:78f9794620a3 | 121 | } |
chris | 0:78f9794620a3 | 122 | } |