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@14:408d9d087d0f, 2018-11-29 (annotated)
- Committer:
- Blunsdon
- Date:
- Thu Nov 29 12:35:48 2018 +0000
- Revision:
- 14:408d9d087d0f
- Parent:
- 13:6a8689f601cd
- Child:
- 15:fd6886ab896a
vers 35
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 | 13:6a8689f601cd | 7 | #define MAX 0.3 |
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 | 13:6a8689f601cd | 31 | |
Blunsdon | 13:6a8689f601cd | 32 | int maze (void) |
Blunsdon | 13:6a8689f601cd | 33 | { |
Blunsdon | 13:6a8689f601cd | 34 | |
Blunsdon | 13:6a8689f601cd | 35 | return 1; |
Blunsdon | 14:408d9d087d0f | 36 | } |
Blunsdon | 14:408d9d087d0f | 37 | |
Blunsdon | 14:408d9d087d0f | 38 | void turn_left (void) |
Blunsdon | 14:408d9d087d0f | 39 | { |
Blunsdon | 14:408d9d087d0f | 40 | m3pi.left(0.3); |
Blunsdon | 14:408d9d087d0f | 41 | wait (0.22); |
Blunsdon | 14:408d9d087d0f | 42 | } |
Blunsdon | 14:408d9d087d0f | 43 | |
Blunsdon | 13:6a8689f601cd | 44 | void turn_right (void) |
Blunsdon | 13:6a8689f601cd | 45 | { |
Blunsdon | 13:6a8689f601cd | 46 | m3pi.right (0.3); |
Blunsdon | 14:408d9d087d0f | 47 | wait (0.22); |
Blunsdon | 13:6a8689f601cd | 48 | } |
chris | 0:78f9794620a3 | 49 | |
chris | 0:78f9794620a3 | 50 | int main() { |
janusboandersen | 2:d22dcc2bfcc1 | 51 | |
chris | 0:78f9794620a3 | 52 | m3pi.locate(0,1); |
chris | 0:78f9794620a3 | 53 | m3pi.printf("Line PID"); |
chris | 0:78f9794620a3 | 54 | |
Blunsdon | 1:47ea51d9e25d | 55 | wait(1.0); |
chris | 0:78f9794620a3 | 56 | |
chris | 0:78f9794620a3 | 57 | m3pi.sensor_auto_calibrate(); |
chris | 0:78f9794620a3 | 58 | |
chris | 0:78f9794620a3 | 59 | float right; |
chris | 0:78f9794620a3 | 60 | float left; |
chris | 0:78f9794620a3 | 61 | float current_pos_of_line = 0.0; |
chris | 0:78f9794620a3 | 62 | float previous_pos_of_line = 0.0; |
chris | 0:78f9794620a3 | 63 | float derivative,proportional,integral = 0; |
chris | 0:78f9794620a3 | 64 | float power; |
chris | 0:78f9794620a3 | 65 | float speed = MAX; |
Blunsdon | 12:dfc717775297 | 66 | int sensor_val[5]; |
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 | 12:dfc717775297 | 105 | sensor_all(sensor_val); // sensor_value gets fkt value |
Blunsdon | 13:6a8689f601cd | 106 | if (sensor_val[0] > 350 && sensor_val[4] > 350) |
Blunsdon | 12:dfc717775297 | 107 | { |
Blunsdon | 13:6a8689f601cd | 108 | |
Blunsdon | 13:6a8689f601cd | 109 | m3pi.stop(); |
Blunsdon | 13:6a8689f601cd | 110 | turn_right(); |
Blunsdon | 13:6a8689f601cd | 111 | m3pi.stop(); |
Blunsdon | 13:6a8689f601cd | 112 | m3pi.forward(0.2); |
Blunsdon | 14:408d9d087d0f | 113 | wait (2); |
Blunsdon | 14:408d9d087d0f | 114 | m3pi.stop(); |
Blunsdon | 14:408d9d087d0f | 115 | m3pi.backward(0.2); |
Blunsdon | 14:408d9d087d0f | 116 | wait (2); |
Blunsdon | 14:408d9d087d0f | 117 | m3pi.stop(); |
Blunsdon | 14:408d9d087d0f | 118 | turn_left(); |
Blunsdon | 13:6a8689f601cd | 119 | m3pi.stop(); |
Blunsdon | 12:dfc717775297 | 120 | } |
janusboandersen | 5:527c046c7b8a | 121 | /* |
janusboandersen | 2:d22dcc2bfcc1 | 122 | sensor_value4 = sensor_all(4); // sensor_value gets fkt value |
janusboandersen | 6:3be33adda32d | 123 | */ |
janusboandersen | 7:30c63d6460f6 | 124 | |
janusboandersen | 6:3be33adda32d | 125 | /* |
janusboandersen | 2:d22dcc2bfcc1 | 126 | m3pi.printf("%d", sensor_value0); // prints one of sidesensors value |
janusboandersen | 2:d22dcc2bfcc1 | 127 | m3pi.locate(0,1); |
janusboandersen | 2:d22dcc2bfcc1 | 128 | m3pi.printf("%d", sensor_value4); // prints the other sidesensor value |
janusboandersen | 3:47968be0df37 | 129 | */ |
chris | 0:78f9794620a3 | 130 | |
chris | 0:78f9794620a3 | 131 | } |
chris | 0:78f9794620a3 | 132 | } |