![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Tuk workshop
Dependencies: mbed RC_Servo VMA306 PID CNY70 CMPS03 GP2A Pixy
main.cpp@16:c0b098c83233, 2019-10-31 (annotated)
- Committer:
- tuk4
- Date:
- Thu Oct 31 14:46:56 2019 +0000
- Revision:
- 16:c0b098c83233
- Parent:
- 15:415c38c93b89
- Child:
- 17:961a91037658
Committed on 31/10/2019
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
haarkon | 0:4902b3f17137 | 1 | #include "mbed.h" |
haarkon | 0:4902b3f17137 | 2 | #include "CMPS03.h" |
haarkon | 0:4902b3f17137 | 3 | #include "CNY70.h" |
haarkon | 0:4902b3f17137 | 4 | #include "GP2A.h" |
haarkon | 0:4902b3f17137 | 5 | #include "VMA306.h" |
haarkon | 3:0221daeeaa86 | 6 | #include "Pixy.h" |
haarkon | 4:5038b4cd1088 | 7 | #include "PID.h" |
tuk4 | 10:6c3653c53eca | 8 | #include "motor_state.h" |
haarkon | 4:5038b4cd1088 | 9 | |
haarkon | 4:5038b4cd1088 | 10 | #define PI 3.1415926535898 |
haarkon | 0:4902b3f17137 | 11 | |
haarkon | 8:2d4887624f94 | 12 | Serial pc (PA_2, PA_3, 115200); |
tuk4 | 11:31564089b41c | 13 | |
haarkon | 7:6d58adc26b78 | 14 | PID motor (TIM4, TIM3, PA_9, PA_8, PC_9, PC_8, PC_6, PC_5); |
haarkon | 0:4902b3f17137 | 15 | |
haarkon | 3:0221daeeaa86 | 16 | CMPS03 boussole (PC_4); |
haarkon | 0:4902b3f17137 | 17 | |
haarkon | 3:0221daeeaa86 | 18 | CNY70 ligneD (PC_3); |
haarkon | 3:0221daeeaa86 | 19 | CNY70 ligneG (PC_2); |
haarkon | 3:0221daeeaa86 | 20 | CNY70 exterior (PA_7); |
haarkon | 0:4902b3f17137 | 21 | |
haarkon | 8:2d4887624f94 | 22 | VMA306 ultraSon (PB_15, PA_6, PB_14, PC_7, PB_13, PB_2); |
haarkon | 0:4902b3f17137 | 23 | |
tuk4 | 11:31564089b41c | 24 | PIXY pixy (PA_0, PA_1, 50000); |
haarkon | 0:4902b3f17137 | 25 | |
tuk4 | 10:6c3653c53eca | 26 | DigitalIn bp (PC_13); |
haarkon | 4:5038b4cd1088 | 27 | |
haarkon | 0:4902b3f17137 | 28 | DigitalOut led1 (PA_5); |
haarkon | 0:4902b3f17137 | 29 | DigitalOut led2 (PD_2); |
haarkon | 8:2d4887624f94 | 30 | DigitalOut unused1 (PB_10); |
haarkon | 8:2d4887624f94 | 31 | DigitalOut unused2 (PA_15); |
haarkon | 8:2d4887624f94 | 32 | DigitalOut unused3 (PA_12); |
haarkon | 8:2d4887624f94 | 33 | |
haarkon | 8:2d4887624f94 | 34 | DigitalIn unused4 (PA_4, PullUp); |
haarkon | 8:2d4887624f94 | 35 | DigitalIn unused5 (PB_0, PullUp); |
haarkon | 8:2d4887624f94 | 36 | DigitalIn unused6 (PC_1, PullUp); |
haarkon | 8:2d4887624f94 | 37 | DigitalIn unused7 (PC_0, PullUp); |
haarkon | 8:2d4887624f94 | 38 | |
haarkon | 8:2d4887624f94 | 39 | Timer temps; |
haarkon | 0:4902b3f17137 | 40 | |
tuk4 | 10:6c3653c53eca | 41 | motor_state current_state = IDLE; |
tuk4 | 10:6c3653c53eca | 42 | |
tuk4 | 9:4053b5217339 | 43 | int main () |
haarkon | 3:0221daeeaa86 | 44 | { |
tuk4 | 10:6c3653c53eca | 45 | int numberOfObjects=0, dummy=0; |
tuk4 | 15:415c38c93b89 | 46 | T_pixyNMBloc file; |
tuk4 | 9:4053b5217339 | 47 | motor.resetPosition(); |
tuk4 | 10:6c3653c53eca | 48 | double x=0, y=0, theta = 0; |
tuk4 | 10:6c3653c53eca | 49 | motor.setProportionnalValue(2.0); |
tuk4 | 10:6c3653c53eca | 50 | motor.setintegralValue(0.4); |
tuk4 | 10:6c3653c53eca | 51 | motor.setDerivativeValue(1.0); |
tuk4 | 14:899c6d29c0a8 | 52 | double speed_L = -200, speed_R = -200; |
haarkon | 3:0221daeeaa86 | 53 | while (1) { |
tuk4 | 15:415c38c93b89 | 54 | |
tuk4 | 12:548cdc49cdba | 55 | motor.getPosition(&x, &y, &theta); |
tuk4 | 15:415c38c93b89 | 56 | pc.printf("\r state is %d x is %.2lf, y is %.2lf, theta is %.2lf",current_state,x,y,theta); |
tuk4 | 15:415c38c93b89 | 57 | switch (current_state) { |
tuk4 | 15:415c38c93b89 | 58 | case IDLE : |
tuk4 | 15:415c38c93b89 | 59 | motor.setSpeed(0,0); |
tuk4 | 15:415c38c93b89 | 60 | if (bp == 0) current_state = TURN_RIGHT; |
tuk4 | 15:415c38c93b89 | 61 | break; |
tuk4 | 15:415c38c93b89 | 62 | |
tuk4 | 15:415c38c93b89 | 63 | case TURN_RIGHT : |
tuk4 | 15:415c38c93b89 | 64 | motor.setSpeed(-speed_L/10.0,speed_R/10.0);// turn right |
tuk4 | 15:415c38c93b89 | 65 | |
tuk4 | 15:415c38c93b89 | 66 | if (pixy.checkNewImage()) { |
tuk4 | 15:415c38c93b89 | 67 | pixy.detectedObject(&numberOfObjects,&dummy); |
tuk4 | 15:415c38c93b89 | 68 | if (numberOfObjects==1) { |
tuk4 | 15:415c38c93b89 | 69 | current_state = LOOK_FOR_FILE; |
tuk4 | 15:415c38c93b89 | 70 | } |
tuk4 | 12:548cdc49cdba | 71 | } |
tuk4 | 15:415c38c93b89 | 72 | break; |
tuk4 | 15:415c38c93b89 | 73 | |
tuk4 | 15:415c38c93b89 | 74 | case SAFEMODE : |
tuk4 | 15:415c38c93b89 | 75 | motor.setSpeed(0,0); |
tuk4 | 15:415c38c93b89 | 76 | if (ultraSon.readUSB() >= 20) current_state = FORWARD; |
tuk4 | 15:415c38c93b89 | 77 | if (ultraSon.readUSB() <= 15) current_state = BACKWARD; |
tuk4 | 15:415c38c93b89 | 78 | break; |
tuk4 | 15:415c38c93b89 | 79 | |
tuk4 | 15:415c38c93b89 | 80 | case FORWARD : |
tuk4 | 12:548cdc49cdba | 81 | motor.setSpeed(speed_L,speed_R); |
tuk4 | 12:548cdc49cdba | 82 | if (ultraSon.readUSB() <= 20) { |
tuk4 | 12:548cdc49cdba | 83 | current_state = SAFEMODE; |
tuk4 | 12:548cdc49cdba | 84 | } |
tuk4 | 16:c0b098c83233 | 85 | if (x<=-2000) current_state = STUPID1; |
tuk4 | 15:415c38c93b89 | 86 | break; |
tuk4 | 15:415c38c93b89 | 87 | |
tuk4 | 15:415c38c93b89 | 88 | case BACKWARD : |
tuk4 | 12:548cdc49cdba | 89 | motor.setSpeed(-speed_L,-speed_R); |
tuk4 | 12:548cdc49cdba | 90 | if (ultraSon.readUSB() >= 15) current_state = SAFEMODE; |
tuk4 | 15:415c38c93b89 | 91 | break; |
tuk4 | 15:415c38c93b89 | 92 | |
tuk4 | 15:415c38c93b89 | 93 | case LOOK_FOR_FILE : |
tuk4 | 15:415c38c93b89 | 94 | if (pixy.checkNewImage()) { |
tuk4 | 15:415c38c93b89 | 95 | pixy.detectedObject(&numberOfObjects,&dummy); |
tuk4 | 15:415c38c93b89 | 96 | if (numberOfObjects==1) { |
tuk4 | 15:415c38c93b89 | 97 | file=pixy.getNMBloc(); |
tuk4 | 15:415c38c93b89 | 98 | pc.printf("\r x = %d", file.x); |
tuk4 | 15:415c38c93b89 | 99 | if (file.x >165) { |
tuk4 | 15:415c38c93b89 | 100 | motor.setSpeed(-speed_L/10.0,speed_R/10.0);// turn right |
tuk4 | 15:415c38c93b89 | 101 | pc.printf(" - Turning right"); |
tuk4 | 15:415c38c93b89 | 102 | } else if (file.x <155) { |
tuk4 | 15:415c38c93b89 | 103 | motor.setSpeed(speed_L/10.0,-speed_R/10.0);// turn left |
tuk4 | 15:415c38c93b89 | 104 | pc.printf(" - Turning left"); |
tuk4 | 15:415c38c93b89 | 105 | } else { |
tuk4 | 15:415c38c93b89 | 106 | motor.setSpeed(0,0); |
tuk4 | 15:415c38c93b89 | 107 | motor.resetPosition(); |
tuk4 | 15:415c38c93b89 | 108 | current_state = FORWARD; |
tuk4 | 15:415c38c93b89 | 109 | } |
tuk4 | 15:415c38c93b89 | 110 | } else { |
tuk4 | 15:415c38c93b89 | 111 | //motor.setSpeed(-speed_L,speed_R);// turn right |
tuk4 | 15:415c38c93b89 | 112 | pc.printf(" - No object"); |
tuk4 | 15:415c38c93b89 | 113 | } |
tuk4 | 11:31564089b41c | 114 | } |
tuk4 | 15:415c38c93b89 | 115 | break; |
tuk4 | 15:415c38c93b89 | 116 | |
tuk4 | 15:415c38c93b89 | 117 | case STUPID1 : |
tuk4 | 15:415c38c93b89 | 118 | wait(0.2); |
tuk4 | 15:415c38c93b89 | 119 | current_state = HARVEST1; |
tuk4 | 15:415c38c93b89 | 120 | break; |
tuk4 | 15:415c38c93b89 | 121 | |
tuk4 | 15:415c38c93b89 | 122 | case STUPID2 : |
tuk4 | 15:415c38c93b89 | 123 | wait(0.2); |
tuk4 | 15:415c38c93b89 | 124 | current_state = HARVEST2; |
tuk4 | 15:415c38c93b89 | 125 | break; |
tuk4 | 15:415c38c93b89 | 126 | |
tuk4 | 15:415c38c93b89 | 127 | case STUPID3 : |
tuk4 | 15:415c38c93b89 | 128 | wait(0.2); |
tuk4 | 15:415c38c93b89 | 129 | current_state = HARVEST3; |
tuk4 | 15:415c38c93b89 | 130 | break; |
tuk4 | 15:415c38c93b89 | 131 | |
tuk4 | 15:415c38c93b89 | 132 | |
tuk4 | 15:415c38c93b89 | 133 | |
tuk4 | 15:415c38c93b89 | 134 | case HARVEST1 : |
tuk4 | 15:415c38c93b89 | 135 | motor.setSpeed(-speed_L,0.0); |
tuk4 | 15:415c38c93b89 | 136 | if (theta < (-2*PI)) current_state = STUPID2; |
tuk4 | 15:415c38c93b89 | 137 | break; |
tuk4 | 15:415c38c93b89 | 138 | |
tuk4 | 15:415c38c93b89 | 139 | case HARVEST2 : |
tuk4 | 15:415c38c93b89 | 140 | motor.setSpeed(0.0,-speed_R); |
tuk4 | 15:415c38c93b89 | 141 | if (theta > 0) current_state = STUPID3; |
tuk4 | 15:415c38c93b89 | 142 | break; |
tuk4 | 15:415c38c93b89 | 143 | |
tuk4 | 15:415c38c93b89 | 144 | case HARVEST3 : |
tuk4 | 15:415c38c93b89 | 145 | motor.setSpeed(speed_L,-speed_R); |
tuk4 | 15:415c38c93b89 | 146 | if (theta > (2*PI)) current_state = IDLE; |
tuk4 | 15:415c38c93b89 | 147 | break; |
tuk4 | 15:415c38c93b89 | 148 | |
tuk4 | 15:415c38c93b89 | 149 | default : |
tuk4 | 15:415c38c93b89 | 150 | //do something |
tuk4 | 15:415c38c93b89 | 151 | break; |
tuk4 | 10:6c3653c53eca | 152 | } |
haarkon | 3:0221daeeaa86 | 153 | } |
tuk4 | 15:415c38c93b89 | 154 | |
tuk4 | 9:4053b5217339 | 155 | return 0; |
haarkon | 0:4902b3f17137 | 156 | } |