For IEEE Robotics
Dependencies: Adafruit-16-Ch-PWM-Servo-Driver HCSR04 PID PololuQik2 QEI Sharp mbed-rtos
main.cpp@13:529323807361, 2014-04-02 (annotated)
- Committer:
- tashworth
- Date:
- Wed Apr 02 03:30:48 2014 +0000
- Revision:
- 13:529323807361
- Parent:
- 12:284be46593ae
- Child:
- 14:784acd735b8c
4-1-14 10:31pm
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tashworth | 0:1b64a0cedc5d | 1 | #include "mbed.h" |
tashworth | 0:1b64a0cedc5d | 2 | #include "Adafruit_PWMServoDriver.h" |
tashworth | 0:1b64a0cedc5d | 3 | #include "ShapeDetect.h" |
tashworth | 8:77a57909aa15 | 4 | #include "rtos.h" |
tashworth | 8:77a57909aa15 | 5 | #include "PID.h" |
tashworth | 8:77a57909aa15 | 6 | #include "PololuQik2.h" |
tashworth | 8:77a57909aa15 | 7 | #include "QEI.h" |
tashworth | 8:77a57909aa15 | 8 | #include "HCSR04.h" |
tashworth | 8:77a57909aa15 | 9 | #include "stdio.h" |
tashworth | 8:77a57909aa15 | 10 | #include "LPC17xx.h" |
tashworth | 8:77a57909aa15 | 11 | #include "Sharp.h" |
tashworth | 0:1b64a0cedc5d | 12 | |
tashworth | 8:77a57909aa15 | 13 | /* Navigation Definitions */ |
tashworth | 8:77a57909aa15 | 14 | #define PIN_TRIGGERL (p12) |
tashworth | 8:77a57909aa15 | 15 | #define PIN_ECHOL (p11) |
tashworth | 8:77a57909aa15 | 16 | #define PIN_TRIGGERR (p29) |
tashworth | 8:77a57909aa15 | 17 | #define PIN_ECHOR (p30) |
tashworth | 8:77a57909aa15 | 18 | #define PULSE_PER_REV (1192) |
tashworth | 8:77a57909aa15 | 19 | #define WHEEL_CIRCUM (12.56637) |
tashworth | 8:77a57909aa15 | 20 | #define DIST_PER_PULSE (0.01054225722682) |
tashworth | 8:77a57909aa15 | 21 | #define MTRS_TO_INCH (39.3701) |
tashworth | 8:77a57909aa15 | 22 | #define MAX_SPEED (0.3*127) |
tashworth | 8:77a57909aa15 | 23 | #define PPR (4331/4) |
tashworth | 8:77a57909aa15 | 24 | #define LEFT (1) |
tashworth | 8:77a57909aa15 | 25 | #define RIGHT (0) |
tashworth | 12:284be46593ae | 26 | #define STRAIGHT (2) |
tashworth | 8:77a57909aa15 | 27 | #define FORWARD (1) |
tashworth | 8:77a57909aa15 | 28 | #define BACKWARD (0) |
tashworth | 8:77a57909aa15 | 29 | #define TOOLS (0) |
tashworth | 8:77a57909aa15 | 30 | #define MID (1) |
tashworth | 8:77a57909aa15 | 31 | #define RIGS (2) |
tashworth | 12:284be46593ae | 32 | #define MID2 (3) |
tashworth | 12:284be46593ae | 33 | #define RETURN (4) |
tashworth | 8:77a57909aa15 | 34 | #define FAR (1) |
tashworth | 6:75259c3306dd | 35 | |
tashworth | 6:75259c3306dd | 36 | //States |
tashworth | 6:75259c3306dd | 37 | #define START 0 |
tashworth | 6:75259c3306dd | 38 | #define OILRIG1_POS 1 |
tashworth | 6:75259c3306dd | 39 | #define OILRIG2_POS 2 |
tashworth | 12:284be46593ae | 40 | #define GOTO_TOOLS1 3 |
tashworth | 6:75259c3306dd | 41 | #define IDENTIFY_TOOLS 4 |
tashworth | 6:75259c3306dd | 42 | #define AQUIRE_TOOL1 5 |
tashworth | 6:75259c3306dd | 43 | #define AQUIRE_TOOL2 6 |
tashworth | 6:75259c3306dd | 44 | #define AQUIRE_TOOL3 7 |
tashworth | 6:75259c3306dd | 45 | #define NAVIGATE_WAVES_ROW1 8 |
tashworth | 6:75259c3306dd | 46 | #define NAVIGATE_WAVES_ROW2 9 |
tashworth | 6:75259c3306dd | 47 | #define NAVIGATE_WAVES_ROW3 10 |
tashworth | 6:75259c3306dd | 48 | #define NAVIGATE_TO_SQUARE_RIG 11 |
tashworth | 6:75259c3306dd | 49 | #define NAVIGATE_TO_TRIANGLE_RIG 12 |
tashworth | 6:75259c3306dd | 50 | #define NAVIGATE_TO_CIRCLE_RIG 13 |
tashworth | 6:75259c3306dd | 51 | #define RIG_ALIGN 14 |
tashworth | 6:75259c3306dd | 52 | #define INSERT_TOOL 15 |
tashworth | 6:75259c3306dd | 53 | #define END 16 |
tashworth | 12:284be46593ae | 54 | #define GOTO_TOOLS2 17 |
tashworth | 6:75259c3306dd | 55 | |
tashworth | 0:1b64a0cedc5d | 56 | |
tashworth | 8:77a57909aa15 | 57 | |
tashworth | 6:75259c3306dd | 58 | //Servo Static Positions |
tashworth | 6:75259c3306dd | 59 | #define STORE_POSITION 0 |
tashworth | 6:75259c3306dd | 60 | #define OIL_RIG1 1 |
tashworth | 6:75259c3306dd | 61 | #define OIL_RIG2 2 |
tashworth | 6:75259c3306dd | 62 | #define OIL_RIG3 3 |
tashworth | 6:75259c3306dd | 63 | #define DRIVE_POSITION_NOTOOL 4 |
tashworth | 6:75259c3306dd | 64 | #define TOOL_1 5 |
tashworth | 6:75259c3306dd | 65 | #define TOOL_2 6 |
tashworth | 6:75259c3306dd | 66 | #define TOOL_3 7 |
tashworth | 6:75259c3306dd | 67 | #define DRIVE_POSITION_TOOL 8 |
tashworth | 6:75259c3306dd | 68 | #define ORIENT_TOOL 9 |
tashworth | 11:8d2455e383ce | 69 | #define PU_TOOL_1 10 |
tashworth | 11:8d2455e383ce | 70 | #define PU_TOOL_2 11 |
tashworth | 11:8d2455e383ce | 71 | #define PU_TOOL_3 12 |
tashworth | 11:8d2455e383ce | 72 | #define PU_TOOL_1_STAB 13 |
tashworth | 11:8d2455e383ce | 73 | #define PU_TOOL_2_STAB 14 |
tashworth | 11:8d2455e383ce | 74 | #define PU_TOOL_3_STAB 15 |
tashworth | 2:4e082e4c255d | 75 | |
tashworth | 6:75259c3306dd | 76 | //Rig definitions |
tashworth | 6:75259c3306dd | 77 | #define SQUARE 1 |
tashworth | 6:75259c3306dd | 78 | #define TRIANGLE 2 |
tashworth | 6:75259c3306dd | 79 | #define CIRCLE 3 |
tashworth | 6:75259c3306dd | 80 | |
tashworth | 6:75259c3306dd | 81 | //*********************// |
tashworth | 6:75259c3306dd | 82 | //******* TODO ********// |
tashworth | 6:75259c3306dd | 83 | //*********************// |
tashworth | 6:75259c3306dd | 84 | //Oil Rig distance thresholds |
tashworth | 8:77a57909aa15 | 85 | #define OILRIG1_MAX 3000 |
tashworth | 8:77a57909aa15 | 86 | #define OILRIG1_MIN 1000 |
tashworth | 9:1b29cd9ed1ba | 87 | #define OILRIG2_MAX 5000 |
tashworth | 8:77a57909aa15 | 88 | #define OILRIG2_MIN 1000 |
tashworth | 9:1b29cd9ed1ba | 89 | #define OILRIG3_MAX 5000 |
tashworth | 8:77a57909aa15 | 90 | #define OILRIG3_MIN 1000 |
tashworth | 6:75259c3306dd | 91 | |
tashworth | 6:75259c3306dd | 92 | //for servo normalization |
tashworth | 3:b7b4780a7f6e | 93 | #define MIN_SERVO_PULSE 900 |
tashworth | 3:b7b4780a7f6e | 94 | #define MAX_SERVO_PULSE 2100 |
tashworth | 3:b7b4780a7f6e | 95 | #define SERVO_MAX_ANGLE 180 |
tashworth | 3:b7b4780a7f6e | 96 | |
tashworth | 8:77a57909aa15 | 97 | |
tashworth | 8:77a57909aa15 | 98 | DigitalOut myled1(LED1); |
tashworth | 8:77a57909aa15 | 99 | DigitalOut myled2(LED2); |
tashworth | 8:77a57909aa15 | 100 | DigitalOut myled3(LED3); |
tashworth | 8:77a57909aa15 | 101 | DigitalOut myled4(LED4); |
tashworth | 8:77a57909aa15 | 102 | |
tashworth | 8:77a57909aa15 | 103 | void errFunction(void); |
tashworth | 8:77a57909aa15 | 104 | bool cRc; |
tashworth | 8:77a57909aa15 | 105 | |
tashworth | 8:77a57909aa15 | 106 | Serial pc(USBTX,USBRX); //USB Comm |
tashworth | 8:77a57909aa15 | 107 | Adafruit_PWMServoDriver pwm(p28,p27); //pwm(SDA,SCL) - Servo Control PWM |
tashworth | 8:77a57909aa15 | 108 | DigitalOut ServoOutputDisable(p8); //Servo Control Output Enable/Disable |
tashworth | 8:77a57909aa15 | 109 | extern Serial lrf; //Laser Range Finder lrf(p13,p14) |
tashworth | 8:77a57909aa15 | 110 | //Hardware Initialization |
tashworth | 8:77a57909aa15 | 111 | //Serial bt(p13,p14); |
tashworth | 8:77a57909aa15 | 112 | HCSR04 rangeFinderLeft( PIN_TRIGGERL, PIN_ECHOL ); |
tashworth | 8:77a57909aa15 | 113 | HCSR04 rangeFinderRight( PIN_TRIGGERR, PIN_ECHOR ); |
tashworth | 8:77a57909aa15 | 114 | PID pid1(15.0,0.0,4.0,0.02); |
tashworth | 8:77a57909aa15 | 115 | PololuQik2 motors(p9, p10, p8, p15, errFunction, cRc); |
tashworth | 8:77a57909aa15 | 116 | QEI rightEncoder(p17,p18,NC,PPR,QEI::X4_ENCODING); |
tashworth | 8:77a57909aa15 | 117 | QEI leftEncoder(p16,p15,NC,PPR,QEI::X4_ENCODING); |
tashworth | 8:77a57909aa15 | 118 | Sharp IR(p20); |
tashworth | 8:77a57909aa15 | 119 | //InterruptIn encoder(p29); |
tashworth | 8:77a57909aa15 | 120 | |
tashworth | 8:77a57909aa15 | 121 | |
tashworth | 8:77a57909aa15 | 122 | |
tashworth | 8:77a57909aa15 | 123 | |
tashworth | 6:75259c3306dd | 124 | /*************** |
tashworth | 6:75259c3306dd | 125 | local servo functions |
tashworth | 6:75259c3306dd | 126 | ****************/ |
tashworth | 1:fe4a0b47ff25 | 127 | void servoBegin(void); |
tashworth | 0:1b64a0cedc5d | 128 | void initServoDriver(void); |
tashworth | 3:b7b4780a7f6e | 129 | void setServoPulse(uint8_t n, int angle); |
tashworth | 3:b7b4780a7f6e | 130 | void setServoPulseNo_delay(uint8_t n, int angle); |
tashworth | 0:1b64a0cedc5d | 131 | void servoPosition(int set); |
tashworth | 6:75259c3306dd | 132 | int fire_checker(int rig); |
tashworth | 6:75259c3306dd | 133 | |
tashworth | 8:77a57909aa15 | 134 | |
tashworth | 8:77a57909aa15 | 135 | //Navigation Functions |
tashworth | 12:284be46593ae | 136 | void wall_follow(int side, int direction, int section); |
tashworth | 12:284be46593ae | 137 | void wall_follow2(int side, int direction, int section, float location, int rig); |
tashworth | 8:77a57909aa15 | 138 | void leftTurn(void); |
tashworth | 8:77a57909aa15 | 139 | void slightleft(void); |
tashworth | 12:284be46593ae | 140 | void slightright(void); |
tashworth | 8:77a57909aa15 | 141 | void rightTurn(void); |
tashworth | 12:284be46593ae | 142 | void slightMove(int direction, float pulses); |
tashworth | 12:284be46593ae | 143 | void to_tools_section1(float* location, float ¤t); |
tashworth | 12:284be46593ae | 144 | void to_tools_section2(float* location, float ¤t); |
tashworth | 8:77a57909aa15 | 145 | void from_tools_section(float* location, float ¤t); |
tashworth | 8:77a57909aa15 | 146 | void mid_section(float* location, float ¤t, int* direction); |
tashworth | 8:77a57909aa15 | 147 | void mid_section2(float* location, float ¤t, int* direction); |
tashworth | 8:77a57909aa15 | 148 | void rig_section(float* location, float ¤t, int* direction, int rig); |
tashworth | 12:284be46593ae | 149 | void tools_section_return(float* location, float ¤t); |
tashworth | 12:284be46593ae | 150 | void mid_section_return(float* location, float ¤t, int* direction); |
tashworth | 12:284be46593ae | 151 | void mid_section2_return(float* location, float ¤t, int* direction); |
tashworth | 12:284be46593ae | 152 | void rig_section_return(float* location, float ¤t, int* direction); |
tashworth | 8:77a57909aa15 | 153 | void overBump(int section); |
tashworth | 8:77a57909aa15 | 154 | void alignWithWall(int section); |
tashworth | 12:284be46593ae | 155 | void UntilWall(int dir); |
tashworth | 8:77a57909aa15 | 156 | |
tashworth | 8:77a57909aa15 | 157 | |
tashworth | 6:75259c3306dd | 158 | /************ |
tashworth | 6:75259c3306dd | 159 | Main Variables |
tashworth | 6:75259c3306dd | 160 | *************/ |
tashworth | 6:75259c3306dd | 161 | int state = START; |
tashworth | 6:75259c3306dd | 162 | int fire = 0; |
tashworth | 6:75259c3306dd | 163 | int tool_needed = 0; |
tashworth | 6:75259c3306dd | 164 | int shape_detected = 0; |
tashworth | 8:77a57909aa15 | 165 | float range, range2, pid_return; |
tashworth | 8:77a57909aa15 | 166 | |
tashworth | 0:1b64a0cedc5d | 167 | |
tashworth | 0:1b64a0cedc5d | 168 | /************ |
tashworth | 0:1b64a0cedc5d | 169 | Variables for Servos |
tashworth | 0:1b64a0cedc5d | 170 | *************/ |
tashworth | 3:b7b4780a7f6e | 171 | int servoNum, servoAngle, outputDisabled, posNum, testVal; |
tashworth | 3:b7b4780a7f6e | 172 | int currentPosition[7]; |
tashworth | 0:1b64a0cedc5d | 173 | |
tashworth | 3:b7b4780a7f6e | 174 | typedef struct { |
tashworth | 6:75259c3306dd | 175 | int arm_position_name; //for organization only (STORE, OILRIG1, OILRIG2...) |
tashworth | 3:b7b4780a7f6e | 176 | int base_rotate; |
tashworth | 3:b7b4780a7f6e | 177 | int base_arm; |
tashworth | 3:b7b4780a7f6e | 178 | int big_arm; |
tashworth | 3:b7b4780a7f6e | 179 | int claw_arm; |
tashworth | 3:b7b4780a7f6e | 180 | int claw_rotate; |
tashworth | 3:b7b4780a7f6e | 181 | int claw_open; |
tashworth | 3:b7b4780a7f6e | 182 | } Coord; |
tashworth | 3:b7b4780a7f6e | 183 | |
tashworth | 6:75259c3306dd | 184 | /******************** |
tashworth | 6:75259c3306dd | 185 | Static Arm Positions |
tashworth | 6:75259c3306dd | 186 | *********************/ |
tashworth | 6:75259c3306dd | 187 | |
tashworth | 3:b7b4780a7f6e | 188 | Coord Arm_Table[] = { |
tashworth | 6:75259c3306dd | 189 | |
tashworth | 3:b7b4780a7f6e | 190 | // POSITION ODER: |
tashworth | 6:75259c3306dd | 191 | // base_rotate, base_arm, int big_arm, int claw_arm, int claw_rotate, int claw_open |
tashworth | 3:b7b4780a7f6e | 192 | |
tashworth | 8:77a57909aa15 | 193 | //increase in number 5 rotates gripper |
tashworth | 8:77a57909aa15 | 194 | |
tashworth | 11:8d2455e383ce | 195 | {STORE_POSITION, 85, 5, 0, 165, 175, 0}, // storing position |
tashworth | 12:284be46593ae | 196 | {OIL_RIG1, 160, 20, 60, 45, 175, 0}, // point laser at oilrig1 |
tashworth | 12:284be46593ae | 197 | {OIL_RIG2, 163, 20, 60, 45, 175, 0}, // point laser at oilrig2 |
tashworth | 12:284be46593ae | 198 | {OIL_RIG3, 130, 90, 90, 52, 175, 0}, // point laser at oilrig2 |
tashworth | 12:284be46593ae | 199 | {DRIVE_POSITION_NOTOOL, 85, 5, 0, 165, 175, 0}, // Drive through course |
tashworth | 12:284be46593ae | 200 | {TOOL_1, 94, 50, 80, 109, 175, 0}, // Look over first tool |
tashworth | 12:284be46593ae | 201 | {TOOL_2, 77, 50, 80, 110, 175, 0}, // Look over second tool |
tashworth | 12:284be46593ae | 202 | {TOOL_3, 57, 50, 80, 109, 175, 0}, // Look over third tool |
tashworth | 8:77a57909aa15 | 203 | {DRIVE_POSITION_TOOL, 55, 70, 102, 74, 0, 0}, // Drive with tool loaded |
tashworth | 6:75259c3306dd | 204 | {ORIENT_TOOL, 135, 60, 75, 60, 90, 90}, // position tool to be inserted |
tashworth | 11:8d2455e383ce | 205 | {PU_TOOL_1, 98, 50, 82, 118, 90, 0}, // STATIC Pickup tool POSITION |
tashworth | 11:8d2455e383ce | 206 | {PU_TOOL_2, 78, 50, 82, 108, 90, 0}, // STATIC Pickup tool POSITION |
tashworth | 11:8d2455e383ce | 207 | {PU_TOOL_3, 53, 50, 82, 118, 90, 0}, // STATIC Pickup tool POSITION |
tashworth | 11:8d2455e383ce | 208 | {PU_TOOL_1_STAB, 98, 50, 90, 118, 90, 0}, // STAB TOOL 1 |
tashworth | 11:8d2455e383ce | 209 | {PU_TOOL_2_STAB, 78, 50, 90, 108, 90, 0}, // STAB TOOL 2 |
tashworth | 11:8d2455e383ce | 210 | {PU_TOOL_3_STAB, 53, 50, 90, 118, 90, 0}, // STAB TOOL 3 |
tashworth | 3:b7b4780a7f6e | 211 | }; |
tashworth | 3:b7b4780a7f6e | 212 | |
tashworth | 3:b7b4780a7f6e | 213 | |
tashworth | 3:b7b4780a7f6e | 214 | /* Variables for imageprocessing and distance */ |
tashworth | 3:b7b4780a7f6e | 215 | int x_coord; |
tashworth | 3:b7b4780a7f6e | 216 | int y_coord; |
tashworth | 3:b7b4780a7f6e | 217 | int area; |
tashworth | 3:b7b4780a7f6e | 218 | int shape; |
tashworth | 13:529323807361 | 219 | int shape_alignX_done = 0; |
tashworth | 13:529323807361 | 220 | |
tashworth | 3:b7b4780a7f6e | 221 | |
tashworth | 6:75259c3306dd | 222 | /* Variables for distance sensor*/ |
tashworth | 8:77a57909aa15 | 223 | int distLaser; |
tashworth | 6:75259c3306dd | 224 | int fire_detected = 0; |
tashworth | 6:75259c3306dd | 225 | int fire_not_detected = 0; |
tashworth | 3:b7b4780a7f6e | 226 | |
tashworth | 0:1b64a0cedc5d | 227 | |
tashworth | 3:b7b4780a7f6e | 228 | int main() |
tashworth | 3:b7b4780a7f6e | 229 | { |
tashworth | 3:b7b4780a7f6e | 230 | |
tashworth | 3:b7b4780a7f6e | 231 | /***************** |
tashworth | 3:b7b4780a7f6e | 232 | INITIALIZATIONS |
tashworth | 3:b7b4780a7f6e | 233 | *******************/ |
tashworth | 12:284be46593ae | 234 | float location[3], current=4; |
tashworth | 8:77a57909aa15 | 235 | int direction[3]; |
tashworth | 8:77a57909aa15 | 236 | double distance; |
tashworth | 8:77a57909aa15 | 237 | |
tashworth | 9:1b29cd9ed1ba | 238 | |
tashworth | 3:b7b4780a7f6e | 239 | pc.baud(115200); |
tashworth | 6:75259c3306dd | 240 | //Laser Range Finder Initialization |
tashworth | 3:b7b4780a7f6e | 241 | lrf_baudCalibration(); |
tashworth | 8:77a57909aa15 | 242 | motors.begin(); |
tashworth | 3:b7b4780a7f6e | 243 | |
tashworth | 6:75259c3306dd | 244 | //Servo initialization |
tashworth | 3:b7b4780a7f6e | 245 | initServoDriver(); |
tashworth | 6:75259c3306dd | 246 | servoBegin(); //initiates servos to start position |
tashworth | 11:8d2455e383ce | 247 | //ServoOutputDisable = 0; |
tashworth | 11:8d2455e383ce | 248 | |
tashworth | 11:8d2455e383ce | 249 | |
tashworth | 6:75259c3306dd | 250 | /******************* |
tashworth | 6:75259c3306dd | 251 | WHILE LOOP FOR TESTING |
tashworth | 6:75259c3306dd | 252 | ********************/ |
tashworth | 12:284be46593ae | 253 | //while(1) { |
tashworth | 13:529323807361 | 254 | /* |
tashworth | 13:529323807361 | 255 | pc.scanf("%d %d", &servoNum, &servoAngle); |
tashworth | 13:529323807361 | 256 | if(servoAngle > 175) { |
tashworth | 13:529323807361 | 257 | servoAngle = 175; |
tashworth | 13:529323807361 | 258 | } |
tashworth | 13:529323807361 | 259 | if(servoNum > 5 ) { |
tashworth | 13:529323807361 | 260 | servoNum = 0; |
tashworth | 13:529323807361 | 261 | servoAngle = 90; |
tashworth | 13:529323807361 | 262 | } |
tashworth | 13:529323807361 | 263 | setServoPulse(servoNum, servoAngle); |
tashworth | 13:529323807361 | 264 | distLaser = getDistance(); |
tashworth | 13:529323807361 | 265 | pc.printf("Distance %d", distLaser); |
tashworth | 13:529323807361 | 266 | */ |
tashworth | 11:8d2455e383ce | 267 | |
tashworth | 13:529323807361 | 268 | /* |
tashworth | 13:529323807361 | 269 | int shape_alignX_done = 0; |
tashworth | 13:529323807361 | 270 | int shape_alignY_done = 0; |
tashworth | 8:77a57909aa15 | 271 | |
tashworth | 11:8d2455e383ce | 272 | |
tashworth | 13:529323807361 | 273 | //pc.scanf("%d", &posNum); |
tashworth | 11:8d2455e383ce | 274 | |
tashworth | 13:529323807361 | 275 | while(pc.getc() != 'g'); |
tashworth | 13:529323807361 | 276 | servoPosition(TOOL_1); |
tashworth | 13:529323807361 | 277 | while(pc.getc() != 'g'); |
tashworth | 13:529323807361 | 278 | //shape_detected = shapeDetection(); |
tashworth | 11:8d2455e383ce | 279 | |
tashworth | 13:529323807361 | 280 | ImageToArray(GREYSCALE); |
tashworth | 13:529323807361 | 281 | //clearBounds(); |
tashworth | 13:529323807361 | 282 | printImageToFile(GREYSCALE); |
tashworth | 13:529323807361 | 283 | while(1); |
tashworth | 11:8d2455e383ce | 284 | |
tashworth | 13:529323807361 | 285 | while(shape_alignY_done == 0) { |
tashworth | 13:529323807361 | 286 | shape_detected = shapeDetection(); |
tashworth | 13:529323807361 | 287 | pc.printf("\nY movement\n"); |
tashworth | 13:529323807361 | 288 | if(get_com_y() >= 80) { |
tashworth | 13:529323807361 | 289 | setServoPulse(1, Arm_Table[TOOL_1].base_arm-=2); |
tashworth | 13:529323807361 | 290 | setServoPulse(2, Arm_Table[TOOL_1].big_arm-=1); |
tashworth | 13:529323807361 | 291 | //setServoPulse(3, Arm_Table[TOOL_1].claw_arm+=2); |
tashworth | 13:529323807361 | 292 | } else if(get_com_y() > 70 && get_com_y() < 90) { |
tashworth | 13:529323807361 | 293 | setServoPulse(1, Arm_Table[TOOL_1].base_arm-=1); |
tashworth | 13:529323807361 | 294 | setServoPulse(2, Arm_Table[TOOL_1].big_arm-=1); |
tashworth | 13:529323807361 | 295 | //setServoPulse(3, Arm_Table[TOOL_1].claw_arm+=1); |
tashworth | 13:529323807361 | 296 | } else if(get_com_y() <= 35 ) { |
tashworth | 13:529323807361 | 297 | setServoPulse(1, Arm_Table[TOOL_1].base_arm+=2); |
tashworth | 13:529323807361 | 298 | setServoPulse(2, Arm_Table[TOOL_1].big_arm+=1); |
tashworth | 13:529323807361 | 299 | //setServoPulse(3, Arm_Table[TOOL_1].claw_arm-=2); |
tashworth | 13:529323807361 | 300 | } else if(get_com_y() < 50 && get_com_y() > 20) { |
tashworth | 13:529323807361 | 301 | setServoPulse(1, Arm_Table[TOOL_1].base_arm+=1); |
tashworth | 13:529323807361 | 302 | setServoPulse(2, Arm_Table[TOOL_1].big_arm+=1); |
tashworth | 13:529323807361 | 303 | //setServoPulse(3, Arm_Table[TOOL_1].claw_arm-=1); |
tashworth | 13:529323807361 | 304 | } else { |
tashworth | 13:529323807361 | 305 | shape_alignY_done = 1; |
tashworth | 13:529323807361 | 306 | } |
tashworth | 13:529323807361 | 307 | } |
tashworth | 11:8d2455e383ce | 308 | |
tashworth | 13:529323807361 | 309 | while( shape_alignX_done == 0){ |
tashworth | 13:529323807361 | 310 | shape_detected = shapeDetection(); |
tashworth | 13:529323807361 | 311 | pc.printf("\nX movement\n"); |
tashworth | 13:529323807361 | 312 | if(get_com_x() > 95) { |
tashworth | 13:529323807361 | 313 | Arm_Table[TOOL_1].base_rotate+=1; |
tashworth | 13:529323807361 | 314 | setServoPulse(0, Arm_Table[TOOL_1].base_rotate); |
tashworth | 13:529323807361 | 315 | setServoPulse(0, Arm_Table[TOOL_1].base_rotate-1); |
tashworth | 13:529323807361 | 316 | setServoPulse(0, Arm_Table[TOOL_1].base_rotate+1); |
tashworth | 11:8d2455e383ce | 317 | |
tashworth | 11:8d2455e383ce | 318 | |
tashworth | 13:529323807361 | 319 | } else if(get_com_x() < 65) { |
tashworth | 13:529323807361 | 320 | Arm_Table[TOOL_1].base_rotate-=1; |
tashworth | 13:529323807361 | 321 | setServoPulse(0, Arm_Table[TOOL_1].base_rotate); |
tashworth | 13:529323807361 | 322 | setServoPulse(0, Arm_Table[TOOL_1].base_rotate-1); |
tashworth | 13:529323807361 | 323 | setServoPulse(0, Arm_Table[TOOL_1].base_rotate+1); |
tashworth | 11:8d2455e383ce | 324 | |
tashworth | 13:529323807361 | 325 | } else { |
tashworth | 13:529323807361 | 326 | shape_alignX_done = 1; |
tashworth | 13:529323807361 | 327 | } |
tashworth | 11:8d2455e383ce | 328 | |
tashworth | 11:8d2455e383ce | 329 | |
tashworth | 13:529323807361 | 330 | } |
tashworth | 13:529323807361 | 331 | printImageToFile(BINARY);*/ |
tashworth | 9:1b29cd9ed1ba | 332 | |
tashworth | 13:529323807361 | 333 | // } |
tashworth | 3:b7b4780a7f6e | 334 | |
tashworth | 11:8d2455e383ce | 335 | |
tashworth | 11:8d2455e383ce | 336 | |
tashworth | 11:8d2455e383ce | 337 | |
tashworth | 11:8d2455e383ce | 338 | |
tashworth | 7:8fb4204f9600 | 339 | /******************************** |
tashworth | 7:8fb4204f9600 | 340 | MAIN WHILE LOOP FOR COMPETITION |
tashworth | 7:8fb4204f9600 | 341 | *********************************/ |
tashworth | 3:b7b4780a7f6e | 342 | while(1) { |
tashworth | 10:1a1d52207f59 | 343 | |
tashworth | 6:75259c3306dd | 344 | switch (state) { |
tashworth | 8:77a57909aa15 | 345 | |
tashworth | 7:8fb4204f9600 | 346 | /************************************************** |
tashworth | 7:8fb4204f9600 | 347 | * STAGE 0 |
tashworth | 7:8fb4204f9600 | 348 | * |
tashworth | 7:8fb4204f9600 | 349 | * - START OF THE COMETITION |
tashworth | 7:8fb4204f9600 | 350 | * |
tashworth | 7:8fb4204f9600 | 351 | **************************************************/ |
tashworth | 6:75259c3306dd | 352 | case START : |
tashworth | 6:75259c3306dd | 353 | myled1 = 1; |
tashworth | 13:529323807361 | 354 | while(pc.getc() != 'g'); //waits for the letter g before starting program |
tashworth | 6:75259c3306dd | 355 | myled1 = 0; |
tashworth | 6:75259c3306dd | 356 | state = OILRIG1_POS; |
tashworth | 3:b7b4780a7f6e | 357 | break; |
tashworth | 3:b7b4780a7f6e | 358 | |
tashworth | 13:529323807361 | 359 | |
tashworth | 6:75259c3306dd | 360 | /************************************************** |
tashworth | 6:75259c3306dd | 361 | * STAGE 1 |
tashworth | 6:75259c3306dd | 362 | * |
tashworth | 6:75259c3306dd | 363 | * - DETERMINE OIL RIG ON FIRE |
tashworth | 6:75259c3306dd | 364 | * |
tashworth | 6:75259c3306dd | 365 | **************************************************/ |
tashworth | 6:75259c3306dd | 366 | case OILRIG1_POS: //aims arm at square oil rig |
tashworth | 10:1a1d52207f59 | 367 | |
tashworth | 13:529323807361 | 368 | servoPosition(OIL_RIG1); |
tashworth | 13:529323807361 | 369 | wait(3); //wait for servo to settle before laser distance |
tashworth | 11:8d2455e383ce | 370 | |
tashworth | 6:75259c3306dd | 371 | fire = fire_checker(OIL_RIG1); //determines if oil rig is on fire |
tashworth | 12:284be46593ae | 372 | |
tashworth | 6:75259c3306dd | 373 | //determines what tool is needed |
tashworth | 6:75259c3306dd | 374 | if (fire == 1) { |
tashworth | 13:529323807361 | 375 | pc.printf("FIRE FOUND!!!!!!!!\n\r"); |
tashworth | 6:75259c3306dd | 376 | tool_needed = SQUARE; |
tashworth | 12:284be46593ae | 377 | state = GOTO_TOOLS1; |
tashworth | 6:75259c3306dd | 378 | } else { |
tashworth | 13:529323807361 | 379 | pc.printf("XXXXXX FIRE NOT FOUND XXXXXX"); |
tashworth | 6:75259c3306dd | 380 | state = OILRIG2_POS; |
tashworth | 6:75259c3306dd | 381 | } |
tashworth | 6:75259c3306dd | 382 | break; |
tashworth | 6:75259c3306dd | 383 | |
tashworth | 6:75259c3306dd | 384 | case OILRIG2_POS: |
tashworth | 12:284be46593ae | 385 | |
tashworth | 13:529323807361 | 386 | servoPosition(DRIVE_POSITION_NOTOOL); |
tashworth | 12:284be46593ae | 387 | wait(3); //wait for servos to settle |
tashworth | 12:284be46593ae | 388 | |
tashworth | 13:529323807361 | 389 | to_tools_section2(location, current); // moves to second rig |
tashworth | 12:284be46593ae | 390 | |
tashworth | 6:75259c3306dd | 391 | servoPosition(OIL_RIG2); //position arm to point at first oilrig |
tashworth | 13:529323807361 | 392 | wait(3); |
tashworth | 13:529323807361 | 393 | |
tashworth | 6:75259c3306dd | 394 | fire = fire_checker(OIL_RIG2); |
tashworth | 6:75259c3306dd | 395 | if (fire == 1) { |
tashworth | 13:529323807361 | 396 | pc.printf("FIRE FOUND!!!!!!!!"); |
tashworth | 6:75259c3306dd | 397 | tool_needed = TRIANGLE; |
tashworth | 12:284be46593ae | 398 | state = GOTO_TOOLS2; |
tashworth | 6:75259c3306dd | 399 | } else { |
tashworth | 13:529323807361 | 400 | pc.printf("XXXXXX FIRE NOT FOUND XXXXXX"); |
tashworth | 6:75259c3306dd | 401 | tool_needed = CIRCLE; |
tashworth | 12:284be46593ae | 402 | state = GOTO_TOOLS2; |
tashworth | 6:75259c3306dd | 403 | } |
tashworth | 6:75259c3306dd | 404 | break; |
tashworth | 6:75259c3306dd | 405 | |
tashworth | 6:75259c3306dd | 406 | /************************************************** |
tashworth | 6:75259c3306dd | 407 | * STAGE 2 |
tashworth | 6:75259c3306dd | 408 | * |
tashworth | 6:75259c3306dd | 409 | * - TRAVEL TO TOOLS |
tashworth | 6:75259c3306dd | 410 | * |
tashworth | 6:75259c3306dd | 411 | **************************************************/ |
tashworth | 12:284be46593ae | 412 | case GOTO_TOOLS1: |
tashworth | 12:284be46593ae | 413 | |
tashworth | 13:529323807361 | 414 | servoPosition(DRIVE_POSITION_NOTOOL); |
tashworth | 13:529323807361 | 415 | wait(3); //wait for servos to settle |
tashworth | 12:284be46593ae | 416 | to_tools_section1(location, current); |
tashworth | 12:284be46593ae | 417 | state = IDENTIFY_TOOLS; |
tashworth | 12:284be46593ae | 418 | break; |
tashworth | 12:284be46593ae | 419 | |
tashworth | 12:284be46593ae | 420 | case GOTO_TOOLS2: |
tashworth | 12:284be46593ae | 421 | |
tashworth | 13:529323807361 | 422 | servoPosition(DRIVE_POSITION_NOTOOL); |
tashworth | 13:529323807361 | 423 | wait(3); //wait for servos to settle |
tashworth | 8:77a57909aa15 | 424 | |
tashworth | 12:284be46593ae | 425 | slightMove(FORWARD,3100); |
tashworth | 12:284be46593ae | 426 | current+=(abs(leftEncoder.getPulses()*11.12/PPR) + abs(rightEncoder.getPulses()*11.12/PPR))/2; |
tashworth | 6:75259c3306dd | 427 | |
tashworth | 6:75259c3306dd | 428 | state = IDENTIFY_TOOLS; |
tashworth | 6:75259c3306dd | 429 | break; |
tashworth | 12:284be46593ae | 430 | |
tashworth | 13:529323807361 | 431 | |
tashworth | 13:529323807361 | 432 | |
tashworth | 6:75259c3306dd | 433 | /************************************************** |
tashworth | 6:75259c3306dd | 434 | * STAGE 3 |
tashworth | 6:75259c3306dd | 435 | * |
tashworth | 6:75259c3306dd | 436 | * - Determine order of tools |
tashworth | 6:75259c3306dd | 437 | * - Aquire appropriate tool |
tashworth | 6:75259c3306dd | 438 | * |
tashworth | 6:75259c3306dd | 439 | **************************************************/ |
tashworth | 6:75259c3306dd | 440 | case IDENTIFY_TOOLS: |
tashworth | 12:284be46593ae | 441 | |
tashworth | 12:284be46593ae | 442 | servoPosition(TOOL_2); //arm/camera looks over tool |
tashworth | 12:284be46593ae | 443 | wait(5); //wait for servos to settle |
tashworth | 13:529323807361 | 444 | |
tashworth | 13:529323807361 | 445 | //shape_detected = shapeDetection(); //determines the shape |
tashworth | 13:529323807361 | 446 | //clearBounds(); |
tashworth | 12:284be46593ae | 447 | //printImageToFile(BINARY); |
tashworth | 3:b7b4780a7f6e | 448 | |
tashworth | 13:529323807361 | 449 | while( shape_alignX_done == 0) { |
tashworth | 6:75259c3306dd | 450 | shape_detected = shapeDetection(); |
tashworth | 13:529323807361 | 451 | pc.printf("X - Adjust to center tool\n\r"); |
tashworth | 13:529323807361 | 452 | if(get_com_x() > 95) { |
tashworth | 13:529323807361 | 453 | Arm_Table[TOOL_1].base_rotate+=1; |
tashworth | 13:529323807361 | 454 | |
tashworth | 13:529323807361 | 455 | } else if(get_com_x() < 65) { |
tashworth | 13:529323807361 | 456 | Arm_Table[TOOL_1].base_rotate-=1; |
tashworth | 13:529323807361 | 457 | |
tashworth | 6:75259c3306dd | 458 | } else { |
tashworth | 13:529323807361 | 459 | shape_alignX_done = 1; |
tashworth | 6:75259c3306dd | 460 | } |
tashworth | 12:284be46593ae | 461 | |
tashworth | 12:284be46593ae | 462 | |
tashworth | 13:529323807361 | 463 | //either goes to aquire the tool or look at the next shape |
tashworth | 13:529323807361 | 464 | if(shape_detected == tool_needed) { |
tashworth | 13:529323807361 | 465 | state = AQUIRE_TOOL2; |
tashworth | 13:529323807361 | 466 | } else { |
tashworth | 13:529323807361 | 467 | servoPosition(TOOL_1); |
tashworth | 13:529323807361 | 468 | wait(3); //wait for servos to settle |
tashworth | 13:529323807361 | 469 | |
tashworth | 13:529323807361 | 470 | shape_alignX_done = 0; |
tashworth | 13:529323807361 | 471 | while( shape_alignX_done == 0) { |
tashworth | 13:529323807361 | 472 | |
tashworth | 13:529323807361 | 473 | shape_detected = shapeDetection(); |
tashworth | 13:529323807361 | 474 | pc.printf("X - Adjust to center tool\n\r"); |
tashworth | 13:529323807361 | 475 | if(get_com_x() > 95) { |
tashworth | 13:529323807361 | 476 | Arm_Table[TOOL_1].base_rotate+=1; |
tashworth | 13:529323807361 | 477 | |
tashworth | 13:529323807361 | 478 | } else if(get_com_x() < 65) { |
tashworth | 13:529323807361 | 479 | Arm_Table[TOOL_1].base_rotate-=1; |
tashworth | 13:529323807361 | 480 | |
tashworth | 13:529323807361 | 481 | } else { |
tashworth | 13:529323807361 | 482 | shape_alignX_done = 1; |
tashworth | 13:529323807361 | 483 | } |
tashworth | 13:529323807361 | 484 | |
tashworth | 13:529323807361 | 485 | if (shape_detected == tool_needed) { |
tashworth | 13:529323807361 | 486 | state = AQUIRE_TOOL; |
tashworth | 13:529323807361 | 487 | } else { |
tashworth | 13:529323807361 | 488 | servoPosition(TOOL_3); |
tashworth | 13:529323807361 | 489 | wait(3); //wait for servos to settle |
tashworth | 13:529323807361 | 490 | shape_detected = shapeDetection(); |
tashworth | 13:529323807361 | 491 | pc.printf("X - Adjust to center tool\n\r"); |
tashworth | 13:529323807361 | 492 | if(get_com_x() > 95) { |
tashworth | 13:529323807361 | 493 | Arm_Table[TOOL_1].base_rotate+=1; |
tashworth | 13:529323807361 | 494 | |
tashworth | 13:529323807361 | 495 | } else if(get_com_x() < 65) { |
tashworth | 13:529323807361 | 496 | Arm_Table[TOOL_1].base_rotate-=1; |
tashworth | 13:529323807361 | 497 | |
tashworth | 13:529323807361 | 498 | } else { |
tashworth | 13:529323807361 | 499 | shape_alignX_done = 1; |
tashworth | 13:529323807361 | 500 | } |
tashworth | 13:529323807361 | 501 | |
tashworth | 13:529323807361 | 502 | state = AQUIRE_TOOL3; |
tashworth | 13:529323807361 | 503 | } |
tashworth | 13:529323807361 | 504 | } |
tashworth | 13:529323807361 | 505 | |
tashworth | 13:529323807361 | 506 | while(1); |
tashworth | 13:529323807361 | 507 | break; |
tashworth | 13:529323807361 | 508 | |
tashworth | 13:529323807361 | 509 | case AQUIRE_TOOL1: |
tashworth | 13:529323807361 | 510 | |
tashworth | 13:529323807361 | 511 | servoPosition(PU_TOOL_1_STAB); |
tashworth | 13:529323807361 | 512 | wait(2); |
tashworth | 13:529323807361 | 513 | |
tashworth | 13:529323807361 | 514 | servoPosition(DRIVE_POSITION_TOOL); //arm position for driving with the tool |
tashworth | 13:529323807361 | 515 | state = NAVIGATE_WAVES_ROW1; |
tashworth | 13:529323807361 | 516 | break; |
tashworth | 13:529323807361 | 517 | |
tashworth | 13:529323807361 | 518 | case AQUIRE_TOOL2: |
tashworth | 13:529323807361 | 519 | servoPosition(PU_TOOL_2_STAB); |
tashworth | 13:529323807361 | 520 | wait(2); |
tashworth | 13:529323807361 | 521 | |
tashworth | 13:529323807361 | 522 | servoPosition(DRIVE_POSITION_TOOL); //arm position for driving with the tool |
tashworth | 13:529323807361 | 523 | state = NAVIGATE_WAVES_ROW1; |
tashworth | 13:529323807361 | 524 | break; |
tashworth | 13:529323807361 | 525 | |
tashworth | 13:529323807361 | 526 | case AQUIRE_TOOL3: |
tashworth | 13:529323807361 | 527 | servoPosition(PU_TOOL_3_STAB); |
tashworth | 13:529323807361 | 528 | wait(2); |
tashworth | 13:529323807361 | 529 | |
tashworth | 13:529323807361 | 530 | |
tashworth | 13:529323807361 | 531 | servoPosition(DRIVE_POSITION_TOOL); //arm position for driving with the tool |
tashworth | 13:529323807361 | 532 | state = NAVIGATE_WAVES_ROW1; |
tashworth | 13:529323807361 | 533 | break; |
tashworth | 3:b7b4780a7f6e | 534 | |
tashworth | 0:1b64a0cedc5d | 535 | |
tashworth | 13:529323807361 | 536 | /************************************************** |
tashworth | 13:529323807361 | 537 | * STAGE 4 |
tashworth | 13:529323807361 | 538 | * |
tashworth | 13:529323807361 | 539 | * - Navigate through the ocean |
tashworth | 13:529323807361 | 540 | * |
tashworth | 13:529323807361 | 541 | **************************************************/ |
tashworth | 6:75259c3306dd | 542 | |
tashworth | 13:529323807361 | 543 | case NAVIGATE_WAVES_ROW1: |
tashworth | 13:529323807361 | 544 | //*********************// |
tashworth | 13:529323807361 | 545 | //******* TODO ********// |
tashworth | 13:529323807361 | 546 | //*********************// |
tashworth | 13:529323807361 | 547 | // CODE TO NAVIGATE ROW1 |
tashworth | 13:529323807361 | 548 | state = NAVIGATE_WAVES_ROW2; |
tashworth | 13:529323807361 | 549 | break; |
tashworth | 6:75259c3306dd | 550 | |
tashworth | 13:529323807361 | 551 | case NAVIGATE_WAVES_ROW2: |
tashworth | 13:529323807361 | 552 | //*********************// |
tashworth | 13:529323807361 | 553 | //******* TODO ********// |
tashworth | 13:529323807361 | 554 | //*********************// |
tashworth | 13:529323807361 | 555 | // CODE TO NAVIGATE ROW2 |
tashworth | 13:529323807361 | 556 | state = NAVIGATE_WAVES_ROW3; |
tashworth | 13:529323807361 | 557 | break; |
tashworth | 3:b7b4780a7f6e | 558 | |
tashworth | 13:529323807361 | 559 | case NAVIGATE_WAVES_ROW3: |
tashworth | 13:529323807361 | 560 | //*********************// |
tashworth | 13:529323807361 | 561 | //******* TODO ********// |
tashworth | 13:529323807361 | 562 | //*********************// |
tashworth | 13:529323807361 | 563 | // CODE TO NAVIGATE ROW3 |
tashworth | 7:8fb4204f9600 | 564 | |
tashworth | 13:529323807361 | 565 | //goes to appropriate rig |
tashworth | 13:529323807361 | 566 | if(shape_detected == 1) { |
tashworth | 13:529323807361 | 567 | state = NAVIGATE_TO_SQUARE_RIG; |
tashworth | 13:529323807361 | 568 | } else if(shape_detected == 2) { |
tashworth | 13:529323807361 | 569 | state = NAVIGATE_TO_TRIANGLE_RIG; |
tashworth | 13:529323807361 | 570 | } else { |
tashworth | 13:529323807361 | 571 | state = NAVIGATE_TO_CIRCLE_RIG; |
tashworth | 13:529323807361 | 572 | } |
tashworth | 13:529323807361 | 573 | break; |
tashworth | 6:75259c3306dd | 574 | |
tashworth | 13:529323807361 | 575 | /************************************************** |
tashworth | 13:529323807361 | 576 | * STAGE 5 |
tashworth | 13:529323807361 | 577 | * |
tashworth | 13:529323807361 | 578 | * - Travel to appropriate rig |
tashworth | 13:529323807361 | 579 | * |
tashworth | 13:529323807361 | 580 | **************************************************/ |
tashworth | 13:529323807361 | 581 | case NAVIGATE_TO_SQUARE_RIG: |
tashworth | 13:529323807361 | 582 | //NAVIGATION CODE HERE |
tashworth | 13:529323807361 | 583 | state = RIG_ALIGN; |
tashworth | 13:529323807361 | 584 | break; |
tashworth | 13:529323807361 | 585 | case NAVIGATE_TO_TRIANGLE_RIG: |
tashworth | 13:529323807361 | 586 | //NAVIGATION CODE HERE |
tashworth | 13:529323807361 | 587 | state = RIG_ALIGN; |
tashworth | 13:529323807361 | 588 | break; |
tashworth | 13:529323807361 | 589 | case NAVIGATE_TO_CIRCLE_RIG: |
tashworth | 13:529323807361 | 590 | //NAVIGATION CODE HERE |
tashworth | 13:529323807361 | 591 | state = RIG_ALIGN; |
tashworth | 13:529323807361 | 592 | break; |
tashworth | 3:b7b4780a7f6e | 593 | |
tashworth | 13:529323807361 | 594 | /************************************************** |
tashworth | 13:529323807361 | 595 | * STAGE 6 |
tashworth | 13:529323807361 | 596 | * |
tashworth | 13:529323807361 | 597 | * - Align with appropriate rig |
tashworth | 13:529323807361 | 598 | * |
tashworth | 13:529323807361 | 599 | **************************************************/ |
tashworth | 13:529323807361 | 600 | case RIG_ALIGN: |
tashworth | 7:8fb4204f9600 | 601 | |
tashworth | 13:529323807361 | 602 | //*********************// |
tashworth | 13:529323807361 | 603 | //******* TODO ********// |
tashworth | 13:529323807361 | 604 | //*********************// |
tashworth | 13:529323807361 | 605 | // CODE TO ALIGN ROBOT WITH RIG |
tashworth | 7:8fb4204f9600 | 606 | |
tashworth | 13:529323807361 | 607 | servoPosition(ORIENT_TOOL); |
tashworth | 13:529323807361 | 608 | wait(1); //wait for servos to settle |
tashworth | 13:529323807361 | 609 | state = INSERT_TOOL; |
tashworth | 13:529323807361 | 610 | break; |
tashworth | 3:b7b4780a7f6e | 611 | |
tashworth | 13:529323807361 | 612 | /************************************************** |
tashworth | 13:529323807361 | 613 | * STAGE 7 |
tashworth | 13:529323807361 | 614 | * |
tashworth | 13:529323807361 | 615 | * - Insert Tool |
tashworth | 13:529323807361 | 616 | * - Extenguish fire |
tashworth | 13:529323807361 | 617 | * - win contest |
tashworth | 13:529323807361 | 618 | * |
tashworth | 13:529323807361 | 619 | **************************************************/ |
tashworth | 7:8fb4204f9600 | 620 | |
tashworth | 13:529323807361 | 621 | case INSERT_TOOL: |
tashworth | 13:529323807361 | 622 | //*********************// |
tashworth | 13:529323807361 | 623 | //******* TODO ********// |
tashworth | 13:529323807361 | 624 | //*********************// |
tashworth | 13:529323807361 | 625 | // CODE TO INSERT TOOL |
tashworth | 13:529323807361 | 626 | break; |
tashworth | 3:b7b4780a7f6e | 627 | |
tashworth | 13:529323807361 | 628 | /************************************************** |
tashworth | 13:529323807361 | 629 | * STAGE 8 |
tashworth | 13:529323807361 | 630 | * |
tashworth | 13:529323807361 | 631 | * - END COMPETITION |
tashworth | 13:529323807361 | 632 | * |
tashworth | 13:529323807361 | 633 | **************************************************/ |
tashworth | 13:529323807361 | 634 | case END: |
tashworth | 13:529323807361 | 635 | servoPosition(STORE_POSITION); |
tashworth | 13:529323807361 | 636 | myled1 = 1; |
tashworth | 13:529323807361 | 637 | wait(.2); |
tashworth | 13:529323807361 | 638 | myled2 = 1; |
tashworth | 13:529323807361 | 639 | wait(.2); |
tashworth | 13:529323807361 | 640 | myled3 = 1; |
tashworth | 13:529323807361 | 641 | wait(.2); |
tashworth | 13:529323807361 | 642 | myled4 = 1; |
tashworth | 13:529323807361 | 643 | wait(.2); |
tashworth | 13:529323807361 | 644 | break; |
tashworth | 13:529323807361 | 645 | default: |
tashworth | 3:b7b4780a7f6e | 646 | |
tashworth | 13:529323807361 | 647 | break; |
tashworth | 13:529323807361 | 648 | } |
tashworth | 13:529323807361 | 649 | } |
tashworth | 6:75259c3306dd | 650 | |
tashworth | 0:1b64a0cedc5d | 651 | |
tashworth | 13:529323807361 | 652 | } |
tashworth | 0:1b64a0cedc5d | 653 | |
tashworth | 0:1b64a0cedc5d | 654 | |
tashworth | 0:1b64a0cedc5d | 655 | |
tashworth | 13:529323807361 | 656 | /************ |
tashworth | 0:1b64a0cedc5d | 657 | |
tashworth | 13:529323807361 | 658 | Servo Functions |
tashworth | 3:b7b4780a7f6e | 659 | |
tashworth | 13:529323807361 | 660 | **************/ |
tashworth | 3:b7b4780a7f6e | 661 | |
tashworth | 13:529323807361 | 662 | void setServoPulse(uint8_t n, int angle) { |
tashworth | 13:529323807361 | 663 | int pulse; |
tashworth | 13:529323807361 | 664 | pulse = MIN_SERVO_PULSE + (( angle * (MAX_SERVO_PULSE - MIN_SERVO_PULSE)) / SERVO_MAX_ANGLE); |
tashworth | 13:529323807361 | 665 | float pulselength = 20000; // 20,000 us per second |
tashworth | 13:529323807361 | 666 | int i = currentPosition[n]; |
tashworth | 13:529323807361 | 667 | pc.printf("ServoNumber: %d Begining Pulse: %d\n\r",n,currentPosition[n]); |
tashworth | 13:529323807361 | 668 | int pulse2; |
tashworth | 13:529323807361 | 669 | if(currentPosition[n] < pulse) { |
tashworth | 13:529323807361 | 670 | for(i; i < pulse; i++) { |
tashworth | 13:529323807361 | 671 | pulse2 = 4094 * i / pulselength; |
tashworth | 13:529323807361 | 672 | pwm.setPWM(n, 0, pulse2); |
tashworth | 13:529323807361 | 673 | wait_ms(3); |
tashworth | 6:75259c3306dd | 674 | } |
tashworth | 13:529323807361 | 675 | } else if (currentPosition[n] > pulse) { |
tashworth | 13:529323807361 | 676 | for(i; i > pulse; i--) { |
tashworth | 13:529323807361 | 677 | pulse2 = 4094 * i / pulselength; |
tashworth | 13:529323807361 | 678 | pwm.setPWM(n, 0, pulse2); |
tashworth | 13:529323807361 | 679 | wait_ms(3); |
tashworth | 6:75259c3306dd | 680 | } |
tashworth | 6:75259c3306dd | 681 | } |
tashworth | 13:529323807361 | 682 | currentPosition[n] = i; |
tashworth | 13:529323807361 | 683 | pc.printf("END: pulse: %d, angle: %d\n\r", i, angle); |
tashworth | 13:529323807361 | 684 | } |
tashworth | 6:75259c3306dd | 685 | |
tashworth | 13:529323807361 | 686 | void initServoDriver(void) { |
tashworth | 13:529323807361 | 687 | pwm.begin(); |
tashworth | 13:529323807361 | 688 | //pwm.setPWMFreq(100); //This dosen't work well because of uncertain clock speed. Use setPrescale(). |
tashworth | 13:529323807361 | 689 | pwm.setPrescale(140); //This value is decided for 20ms interval. |
tashworth | 13:529323807361 | 690 | pwm.setI2Cfreq(400000); //400kHz |
tashworth | 13:529323807361 | 691 | |
tashworth | 13:529323807361 | 692 | } |
tashworth | 0:1b64a0cedc5d | 693 | |
tashworth | 13:529323807361 | 694 | void servoBegin(void) { |
tashworth | 13:529323807361 | 695 | pc.printf("Setting Initial Servo Position\n\r"); |
tashworth | 13:529323807361 | 696 | servoPosition(STORE_POSITION); |
tashworth | 13:529323807361 | 697 | } |
tashworth | 8:77a57909aa15 | 698 | |
tashworth | 13:529323807361 | 699 | void setServoPulseNo_delay(uint8_t n, int angle) { |
tashworth | 13:529323807361 | 700 | int pulse = MIN_SERVO_PULSE + (( angle * (MAX_SERVO_PULSE - MIN_SERVO_PULSE)) / SERVO_MAX_ANGLE); |
tashworth | 13:529323807361 | 701 | float pulselength = 20000; // 20,000 us per second |
tashworth | 13:529323807361 | 702 | currentPosition[n] = pulse; |
tashworth | 13:529323807361 | 703 | //pc.printf("ServoNumber: %d Pulsewidth: %d Angle: %d \n\r",n,pulse, angle); |
tashworth | 13:529323807361 | 704 | pulse = 4094 * pulse / pulselength; |
tashworth | 13:529323807361 | 705 | pwm.setPWM(n, 0, pulse); |
tashworth | 13:529323807361 | 706 | |
tashworth | 13:529323807361 | 707 | } |
tashworth | 13:529323807361 | 708 | |
tashworth | 13:529323807361 | 709 | |
tashworth | 8:77a57909aa15 | 710 | |
tashworth | 13:529323807361 | 711 | void servoPosition(int set) { |
tashworth | 13:529323807361 | 712 | //moves to current position |
tashworth | 13:529323807361 | 713 | setServoPulse(3, Arm_Table[set].claw_arm); |
tashworth | 13:529323807361 | 714 | setServoPulse(2, Arm_Table[set].big_arm); |
tashworth | 13:529323807361 | 715 | setServoPulse(1, Arm_Table[set].base_arm); |
tashworth | 13:529323807361 | 716 | setServoPulse(0, Arm_Table[set].base_rotate); |
tashworth | 13:529323807361 | 717 | setServoPulse(4, Arm_Table[set].claw_rotate); |
tashworth | 13:529323807361 | 718 | setServoPulse(5, Arm_Table[set].claw_open); |
tashworth | 13:529323807361 | 719 | } |
tashworth | 8:77a57909aa15 | 720 | |
tashworth | 8:77a57909aa15 | 721 | |
tashworth | 13:529323807361 | 722 | int fire_checker(int rig) { |
tashworth | 13:529323807361 | 723 | switch (rig) { |
tashworth | 8:77a57909aa15 | 724 | |
tashworth | 13:529323807361 | 725 | case 1: |
tashworth | 13:529323807361 | 726 | for (int i = 0; i<10; i++) { |
tashworth | 13:529323807361 | 727 | distLaser = getDistance(); |
tashworth | 13:529323807361 | 728 | pc.printf("L DISTANCE: %d \n\r", distLaser); |
tashworth | 13:529323807361 | 729 | if ((distLaser < OILRIG1_MAX) |
tashworth | 13:529323807361 | 730 | && (distLaser > OILRIG1_MIN)) { |
tashworth | 13:529323807361 | 731 | fire_detected++; |
tashworth | 13:529323807361 | 732 | } else { |
tashworth | 13:529323807361 | 733 | fire_not_detected++; |
tashworth | 13:529323807361 | 734 | } |
tashworth | 13:529323807361 | 735 | } |
tashworth | 13:529323807361 | 736 | break; |
tashworth | 13:529323807361 | 737 | case 2: |
tashworth | 13:529323807361 | 738 | for (int i = 0; i<10; i++) { |
tashworth | 13:529323807361 | 739 | distLaser = getDistance(); |
tashworth | 13:529323807361 | 740 | pc.printf("L DISTANCE: %d \n\r", distLaser); |
tashworth | 13:529323807361 | 741 | if ((distLaser < OILRIG2_MAX) |
tashworth | 13:529323807361 | 742 | && (distLaser > OILRIG2_MIN)) { |
tashworth | 13:529323807361 | 743 | fire_detected++; |
tashworth | 13:529323807361 | 744 | } else { |
tashworth | 13:529323807361 | 745 | fire_not_detected++; |
tashworth | 13:529323807361 | 746 | } |
tashworth | 13:529323807361 | 747 | } |
tashworth | 13:529323807361 | 748 | break; |
tashworth | 13:529323807361 | 749 | |
tashworth | 13:529323807361 | 750 | } |
tashworth | 13:529323807361 | 751 | |
tashworth | 13:529323807361 | 752 | if (fire_detected > 0) { |
tashworth | 13:529323807361 | 753 | return 1; |
tashworth | 13:529323807361 | 754 | } else { |
tashworth | 13:529323807361 | 755 | return 0; |
tashworth | 13:529323807361 | 756 | } |
tashworth | 8:77a57909aa15 | 757 | } |
tashworth | 8:77a57909aa15 | 758 | |
tashworth | 13:529323807361 | 759 | void errFunction(void) { |
tashworth | 13:529323807361 | 760 | //Nothing |
tashworth | 13:529323807361 | 761 | } |
tashworth | 13:529323807361 | 762 | void wall_follow(int side, int direction, int section) { |
tashworth | 13:529323807361 | 763 | float location, set=6; |
tashworth | 13:529323807361 | 764 | int dir=1; |
tashworth | 8:77a57909aa15 | 765 | |
tashworth | 13:529323807361 | 766 | pid1.reset(); |
tashworth | 13:529323807361 | 767 | |
tashworth | 13:529323807361 | 768 | if(direction == BACKWARD) dir=-1; |
tashworth | 13:529323807361 | 769 | if(section == TOOLS)set= 10; |
tashworth | 8:77a57909aa15 | 770 | |
tashworth | 13:529323807361 | 771 | leftEncoder.reset(); |
tashworth | 13:529323807361 | 772 | rightEncoder.reset(); |
tashworth | 13:529323807361 | 773 | |
tashworth | 13:529323807361 | 774 | location=(abs(leftEncoder.getPulses()*11.12/PPR) + abs(rightEncoder.getPulses()*11.12/PPR))/2; |
tashworth | 13:529323807361 | 775 | |
tashworth | 13:529323807361 | 776 | while(location< 66.5) { |
tashworth | 13:529323807361 | 777 | location=(abs(leftEncoder.getPulses()*11.12/PPR) + abs(rightEncoder.getPulses()*11.12/PPR))/2; |
tashworth | 13:529323807361 | 778 | |
tashworth | 13:529323807361 | 779 | pid1.setInputLimits(0, set); |
tashworth | 13:529323807361 | 780 | pid1.setOutputLimits( -MAX_SPEED, MAX_SPEED); |
tashworth | 13:529323807361 | 781 | pid1.setSetPoint(set); |
tashworth | 8:77a57909aa15 | 782 | if(side) { |
tashworth | 13:529323807361 | 783 | rangeFinderLeft.startMeas(); |
tashworth | 13:529323807361 | 784 | wait_ms(20); |
tashworth | 13:529323807361 | 785 | rangeFinderLeft.getMeas(range); |
tashworth | 8:77a57909aa15 | 786 | } else { |
tashworth | 13:529323807361 | 787 | rangeFinderRight.startMeas(); |
tashworth | 13:529323807361 | 788 | wait_ms(20); |
tashworth | 13:529323807361 | 789 | rangeFinderRight.getMeas(range); |
tashworth | 13:529323807361 | 790 | pc.printf("%d\r\n",range); |
tashworth | 8:77a57909aa15 | 791 | } |
tashworth | 8:77a57909aa15 | 792 | |
tashworth | 13:529323807361 | 793 | if(range > 15) { |
tashworth | 13:529323807361 | 794 | //pc.printf("wavegap %f\r\n",wavegap); |
tashworth | 13:529323807361 | 795 | // AT WAVE OPENING!!!! |
tashworth | 13:529323807361 | 796 | motors.setMotor1Speed(dir*0.25*127);//left |
tashworth | 13:529323807361 | 797 | motors.setMotor0Speed(dir*0.25*127);//right |
tashworth | 13:529323807361 | 798 | } else { |
tashworth | 8:77a57909aa15 | 799 | |
tashworth | 13:529323807361 | 800 | pid1.setProcessValue(range); |
tashworth | 13:529323807361 | 801 | pid_return = pid1.compute(); |
tashworth | 8:77a57909aa15 | 802 | |
tashworth | 13:529323807361 | 803 | if(pid_return > 0) { |
tashworth | 13:529323807361 | 804 | if(side) { |
tashworth | 13:529323807361 | 805 | motors.setMotor0Speed(dir*MAX_SPEED - dir*pid_return);//right |
tashworth | 13:529323807361 | 806 | motors.setMotor1Speed(dir*MAX_SPEED);//left |
tashworth | 13:529323807361 | 807 | } else { |
tashworth | 13:529323807361 | 808 | motors.setMotor1Speed(dir*MAX_SPEED - dir*pid_return);//left |
tashworth | 13:529323807361 | 809 | motors.setMotor0Speed(dir*MAX_SPEED);//right |
tashworth | 13:529323807361 | 810 | } |
tashworth | 13:529323807361 | 811 | } else if(pid_return < 0) { |
tashworth | 13:529323807361 | 812 | if(side) { |
tashworth | 13:529323807361 | 813 | motors.setMotor0Speed(dir*MAX_SPEED);//right |
tashworth | 13:529323807361 | 814 | motors.setMotor1Speed(dir*MAX_SPEED + dir*pid_return);//left |
tashworth | 13:529323807361 | 815 | } else { |
tashworth | 13:529323807361 | 816 | motors.setMotor1Speed(dir*MAX_SPEED);//left |
tashworth | 13:529323807361 | 817 | motors.setMotor0Speed(dir*MAX_SPEED + dir*pid_return);//right |
tashworth | 13:529323807361 | 818 | } |
tashworth | 13:529323807361 | 819 | } else { |
tashworth | 13:529323807361 | 820 | motors.setMotor0Speed(dir*MAX_SPEED);//right |
tashworth | 13:529323807361 | 821 | motors.setMotor1Speed(dir*MAX_SPEED);//left |
tashworth | 13:529323807361 | 822 | } |
tashworth | 13:529323807361 | 823 | } |
tashworth | 13:529323807361 | 824 | } |
tashworth | 8:77a57909aa15 | 825 | |
tashworth | 13:529323807361 | 826 | //STOP |
tashworth | 13:529323807361 | 827 | motors.setMotor0Speed(dir*-0.3*127); //right |
tashworth | 13:529323807361 | 828 | motors.setMotor1Speed(dir*-0.3*127); //left |
tashworth | 13:529323807361 | 829 | wait_ms(10); |
tashworth | 13:529323807361 | 830 | motors.stopBothMotors(0); |
tashworth | 8:77a57909aa15 | 831 | } |
tashworth | 8:77a57909aa15 | 832 | |
tashworth | 13:529323807361 | 833 | /* MODIFIED WALL_FOLLOW FOR NAVIGATION */ |
tashworth | 13:529323807361 | 834 | |
tashworth | 13:529323807361 | 835 | void wall_follow2(int side, int direction, int section, float location, int rig) { |
tashworth | 13:529323807361 | 836 | int dir=1, limit=86, lowlim=5; |
tashworth | 13:529323807361 | 837 | float set=6, loc=0, Rigloc=0; |
tashworth | 13:529323807361 | 838 | bool SeeWaveGap = false; |
tashworth | 13:529323807361 | 839 | |
tashworth | 13:529323807361 | 840 | if(rig == 1) Rigloc= 16; |
tashworth | 13:529323807361 | 841 | else if(rig == 2) Rigloc= 45; |
tashworth | 13:529323807361 | 842 | else if(rig== 3) Rigloc = 70; |
tashworth | 13:529323807361 | 843 | |
tashworth | 13:529323807361 | 844 | pid1.reset(); |
tashworth | 13:529323807361 | 845 | |
tashworth | 13:529323807361 | 846 | if(direction == BACKWARD) { |
tashworth | 13:529323807361 | 847 | dir=-1; |
tashworth | 13:529323807361 | 848 | limit = 100; |
tashworth | 13:529323807361 | 849 | } |
tashworth | 13:529323807361 | 850 | if(section == TOOLS) { |
tashworth | 13:529323807361 | 851 | set= 6; |
tashworth | 13:529323807361 | 852 | limit = 86; |
tashworth | 13:529323807361 | 853 | } |
tashworth | 13:529323807361 | 854 | if(section == RETURN) lowlim=15; |
tashworth | 13:529323807361 | 855 | |
tashworth | 13:529323807361 | 856 | leftEncoder.reset(); |
tashworth | 13:529323807361 | 857 | rightEncoder.reset(); |
tashworth | 13:529323807361 | 858 | |
tashworth | 13:529323807361 | 859 | //pc.printf("before %f\r\n", location); |
tashworth | 13:529323807361 | 860 | |
tashworth | 13:529323807361 | 861 | pc.printf("dir*loc+location %f\r\n",dir*loc + location ); |
tashworth | 13:529323807361 | 862 | pc.printf("limit %d \r\n", limit); |
tashworth | 13:529323807361 | 863 | |
tashworth | 13:529323807361 | 864 | while((dir*loc + location <= limit) && (dir*loc + location >= lowlim)) { |
tashworth | 13:529323807361 | 865 | |
tashworth | 13:529323807361 | 866 | loc=(abs(leftEncoder.getPulses()*11.12/PPR) + abs(rightEncoder.getPulses()*11.12/PPR))/2; |
tashworth | 13:529323807361 | 867 | pc.printf("loc %f \r\n", loc); |
tashworth | 13:529323807361 | 868 | |
tashworth | 13:529323807361 | 869 | pid1.setInputLimits(0.0, set); |
tashworth | 13:529323807361 | 870 | pid1.setOutputLimits( -MAX_SPEED, MAX_SPEED); |
tashworth | 13:529323807361 | 871 | pid1.setSetPoint(set); |
tashworth | 13:529323807361 | 872 | |
tashworth | 13:529323807361 | 873 | if(side) { |
tashworth | 13:529323807361 | 874 | rangeFinderLeft.startMeas(); |
tashworth | 13:529323807361 | 875 | wait_ms(20); |
tashworth | 13:529323807361 | 876 | rangeFinderLeft.getMeas(range); |
tashworth | 13:529323807361 | 877 | } else { |
tashworth | 13:529323807361 | 878 | rangeFinderRight.startMeas(); |
tashworth | 13:529323807361 | 879 | wait_ms(20); |
tashworth | 13:529323807361 | 880 | rangeFinderRight.getMeas(range); |
tashworth | 13:529323807361 | 881 | } |
tashworth | 13:529323807361 | 882 | |
tashworth | 13:529323807361 | 883 | if(section == RIGS) { |
tashworth | 13:529323807361 | 884 | rangeFinderLeft.startMeas(); |
tashworth | 13:529323807361 | 885 | wait_ms(20); |
tashworth | 13:529323807361 | 886 | rangeFinderLeft.getMeas(range2); |
tashworth | 8:77a57909aa15 | 887 | |
tashworth | 13:529323807361 | 888 | if(range2< 20) { |
tashworth | 13:529323807361 | 889 | if( abs(dir*loc + location - Rigloc) < 10) { |
tashworth | 13:529323807361 | 890 | //STOP |
tashworth | 13:529323807361 | 891 | motors.setMotor0Speed(dir*-0.25*127); //right |
tashworth | 13:529323807361 | 892 | motors.setMotor1Speed(dir*-0.25*127); //left |
tashworth | 13:529323807361 | 893 | wait_ms(5); |
tashworth | 13:529323807361 | 894 | motors.stopBothMotors(0); |
tashworth | 13:529323807361 | 895 | break; |
tashworth | 13:529323807361 | 896 | } |
tashworth | 13:529323807361 | 897 | } |
tashworth | 13:529323807361 | 898 | } |
tashworth | 13:529323807361 | 899 | |
tashworth | 13:529323807361 | 900 | |
tashworth | 13:529323807361 | 901 | //pc.printf("wall follow 2 range %f\r\n",range); |
tashworth | 13:529323807361 | 902 | //pc.printf("loc+location = %f\r\n", loc+location); |
tashworth | 13:529323807361 | 903 | if(range > 20 ) { |
tashworth | 13:529323807361 | 904 | if(section == RIGS || section == RETURN) { |
tashworth | 13:529323807361 | 905 | motors.setMotor0Speed(dir*0.25*127); //right |
tashworth | 13:529323807361 | 906 | motors.setMotor1Speed(dir*0.25*127); //left |
tashworth | 13:529323807361 | 907 | } else { |
tashworth | 13:529323807361 | 908 | if(!SeeWaveGap) { |
tashworth | 13:529323807361 | 909 | SeeWaveGap=true; |
tashworth | 13:529323807361 | 910 | } else { |
tashworth | 13:529323807361 | 911 | //STOP |
tashworth | 13:529323807361 | 912 | motors.setMotor0Speed(dir*-0.25*127); //right |
tashworth | 13:529323807361 | 913 | motors.setMotor1Speed(dir*-0.25*127); //left |
tashworth | 13:529323807361 | 914 | wait_ms(5); |
tashworth | 13:529323807361 | 915 | motors.stopBothMotors(0); |
tashworth | 13:529323807361 | 916 | |
tashworth | 13:529323807361 | 917 | pc.printf("wavegap\r\n"); |
tashworth | 13:529323807361 | 918 | // AT WAVE OPENING!!!! |
tashworth | 13:529323807361 | 919 | break; |
tashworth | 13:529323807361 | 920 | } |
tashworth | 13:529323807361 | 921 | } |
tashworth | 13:529323807361 | 922 | } else { |
tashworth | 13:529323807361 | 923 | SeeWaveGap = false; |
tashworth | 13:529323807361 | 924 | pid1.setProcessValue(range); |
tashworth | 13:529323807361 | 925 | pid_return = pid1.compute(); |
tashworth | 13:529323807361 | 926 | //pc.printf("Range: %f\n PID: %f\r\n", range, pid_return); |
tashworth | 13:529323807361 | 927 | |
tashworth | 13:529323807361 | 928 | if(pid_return > 0) { |
tashworth | 13:529323807361 | 929 | if(side) { |
tashworth | 13:529323807361 | 930 | motors.setMotor0Speed(dir*MAX_SPEED - dir*pid_return);//right |
tashworth | 13:529323807361 | 931 | motors.setMotor1Speed(dir*MAX_SPEED);//left |
tashworth | 13:529323807361 | 932 | } else { |
tashworth | 13:529323807361 | 933 | motors.setMotor1Speed(dir*MAX_SPEED - dir*pid_return);//left |
tashworth | 13:529323807361 | 934 | motors.setMotor0Speed(dir*MAX_SPEED);//right |
tashworth | 13:529323807361 | 935 | } |
tashworth | 13:529323807361 | 936 | } else if(pid_return < 0) { |
tashworth | 13:529323807361 | 937 | if(side) { |
tashworth | 13:529323807361 | 938 | motors.setMotor0Speed(dir*MAX_SPEED);//right |
tashworth | 13:529323807361 | 939 | motors.setMotor1Speed(dir*MAX_SPEED + dir*pid_return);//left |
tashworth | 13:529323807361 | 940 | } else { |
tashworth | 13:529323807361 | 941 | motors.setMotor1Speed(dir*MAX_SPEED);//left |
tashworth | 13:529323807361 | 942 | motors.setMotor0Speed(dir*MAX_SPEED + dir*pid_return);//right |
tashworth | 13:529323807361 | 943 | } |
tashworth | 13:529323807361 | 944 | } else { |
tashworth | 13:529323807361 | 945 | motors.setMotor0Speed(dir*MAX_SPEED); |
tashworth | 13:529323807361 | 946 | motors.setMotor1Speed(dir*MAX_SPEED); |
tashworth | 13:529323807361 | 947 | } |
tashworth | 12:284be46593ae | 948 | } |
tashworth | 12:284be46593ae | 949 | } |
tashworth | 13:529323807361 | 950 | |
tashworth | 13:529323807361 | 951 | //STOP |
tashworth | 13:529323807361 | 952 | motors.setMotor0Speed(dir*-0.3*127); //right |
tashworth | 13:529323807361 | 953 | motors.setMotor1Speed(dir*-0.3*127); //left |
tashworth | 13:529323807361 | 954 | wait_ms(5); |
tashworth | 13:529323807361 | 955 | motors.stopBothMotors(0); |
tashworth | 12:284be46593ae | 956 | } |
tashworth | 12:284be46593ae | 957 | |
tashworth | 8:77a57909aa15 | 958 | |
tashworth | 13:529323807361 | 959 | void alignWithWall(int section) { |
tashworth | 13:529323807361 | 960 | float usValue = 0; |
tashworth | 13:529323807361 | 961 | |
tashworth | 13:529323807361 | 962 | if(section == TOOLS) { |
tashworth | 13:529323807361 | 963 | pc.printf("tools section align\r\n"); |
tashworth | 13:529323807361 | 964 | // turn at an angle |
tashworth | 13:529323807361 | 965 | leftEncoder.reset(); |
tashworth | 13:529323807361 | 966 | rightEncoder.reset(); |
tashworth | 13:529323807361 | 967 | motors.setMotor0Speed(-1.2*MAX_SPEED); //right |
tashworth | 13:529323807361 | 968 | motors.setMotor1Speed(0.4*MAX_SPEED); //left |
tashworth | 13:529323807361 | 969 | while(rightEncoder.getPulses()>-1000); |
tashworth | 13:529323807361 | 970 | motors.stopBothMotors(0); |
tashworth | 13:529323807361 | 971 | |
tashworth | 13:529323807361 | 972 | //go backwards toward wall |
tashworth | 13:529323807361 | 973 | leftEncoder.reset(); |
tashworth | 13:529323807361 | 974 | rightEncoder.reset(); |
tashworth | 13:529323807361 | 975 | motors.setMotor0Speed(-0.25*127); //right |
tashworth | 13:529323807361 | 976 | motors.setMotor1Speed(-0.25*127); //left |
tashworth | 13:529323807361 | 977 | while(abs(leftEncoder.getPulses()) < 300 || abs(rightEncoder.getPulses()) < 300); |
tashworth | 13:529323807361 | 978 | motors.stopBothMotors(0); |
tashworth | 13:529323807361 | 979 | |
tashworth | 13:529323807361 | 980 | // turn left towards wall |
tashworth | 13:529323807361 | 981 | leftEncoder.reset(); |
tashworth | 13:529323807361 | 982 | rightEncoder.reset(); |
tashworth | 13:529323807361 | 983 | motors.setMotor0Speed(MAX_SPEED); //right |
tashworth | 13:529323807361 | 984 | motors.setMotor1Speed(-MAX_SPEED); //left |
tashworth | 13:529323807361 | 985 | while(rightEncoder.getPulses() < 20 || abs(leftEncoder.getPulses()) < 20); |
tashworth | 13:529323807361 | 986 | |
tashworth | 13:529323807361 | 987 | motors.stopBothMotors(0); |
tashworth | 13:529323807361 | 988 | |
tashworth | 13:529323807361 | 989 | // turning left |
tashworth | 13:529323807361 | 990 | motors.setMotor0Speed(0.9*MAX_SPEED); //right |
tashworth | 13:529323807361 | 991 | motors.setMotor1Speed(-0.9*MAX_SPEED); //left |
tashworth | 13:529323807361 | 992 | |
tashworth | 13:529323807361 | 993 | } else if( section == RIGS) { |
tashworth | 13:529323807361 | 994 | // check distance to wall |
tashworth | 13:529323807361 | 995 | rangeFinderRight.startMeas(); |
tashworth | 13:529323807361 | 996 | wait_ms(20); |
tashworth | 13:529323807361 | 997 | rangeFinderRight.getMeas(range); |
tashworth | 13:529323807361 | 998 | |
tashworth | 13:529323807361 | 999 | if(range < 4 || range > 20) return; |
tashworth | 13:529323807361 | 1000 | |
tashworth | 13:529323807361 | 1001 | // turn at an angle |
tashworth | 13:529323807361 | 1002 | leftEncoder.reset(); |
tashworth | 13:529323807361 | 1003 | rightEncoder.reset(); |
tashworth | 13:529323807361 | 1004 | motors.setMotor1Speed(-1.2*MAX_SPEED); //left |
tashworth | 13:529323807361 | 1005 | motors.setMotor0Speed(0.4*MAX_SPEED); //right |
tashworth | 13:529323807361 | 1006 | while(abs(leftEncoder.getPulses())<1000); |
tashworth | 13:529323807361 | 1007 | motors.stopBothMotors(0); |
tashworth | 13:529323807361 | 1008 | |
tashworth | 13:529323807361 | 1009 | //go backwards toward wall |
tashworth | 13:529323807361 | 1010 | leftEncoder.reset(); |
tashworth | 13:529323807361 | 1011 | rightEncoder.reset(); |
tashworth | 13:529323807361 | 1012 | motors.setMotor0Speed(-0.25*127); //right |
tashworth | 13:529323807361 | 1013 | motors.setMotor1Speed(-0.25*127); //left |
tashworth | 13:529323807361 | 1014 | while(abs(leftEncoder.getPulses()) < 150 || abs(rightEncoder.getPulses()) < 150); |
tashworth | 13:529323807361 | 1015 | motors.stopBothMotors(0); |
tashworth | 13:529323807361 | 1016 | |
tashworth | 13:529323807361 | 1017 | // turn left towards wall |
tashworth | 13:529323807361 | 1018 | leftEncoder.reset(); |
tashworth | 13:529323807361 | 1019 | rightEncoder.reset(); |
tashworth | 13:529323807361 | 1020 | motors.setMotor0Speed(-MAX_SPEED); //right |
tashworth | 13:529323807361 | 1021 | motors.setMotor1Speed(MAX_SPEED); //left |
tashworth | 13:529323807361 | 1022 | while(abs(rightEncoder.getPulses()) < 20 || abs(leftEncoder.getPulses()) < 20); |
tashworth | 13:529323807361 | 1023 | |
tashworth | 13:529323807361 | 1024 | motors.stopBothMotors(0); |
tashworth | 13:529323807361 | 1025 | |
tashworth | 13:529323807361 | 1026 | // turning left |
tashworth | 13:529323807361 | 1027 | motors.setMotor0Speed(-0.9*MAX_SPEED); //right |
tashworth | 13:529323807361 | 1028 | motors.setMotor1Speed(0.9*MAX_SPEED); //left |
tashworth | 12:284be46593ae | 1029 | } else { |
tashworth | 13:529323807361 | 1030 | pc.printf("in mid section align\r\n"); |
tashworth | 13:529323807361 | 1031 | // turn right towards wall |
tashworth | 13:529323807361 | 1032 | rightTurn(); |
tashworth | 13:529323807361 | 1033 | // turning left towards wall |
tashworth | 13:529323807361 | 1034 | motors.setMotor0Speed(0.9*MAX_SPEED); //right |
tashworth | 13:529323807361 | 1035 | motors.setMotor1Speed(-0.9*MAX_SPEED); //left |
tashworth | 13:529323807361 | 1036 | } |
tashworth | 13:529323807361 | 1037 | |
tashworth | 13:529323807361 | 1038 | usValue = 0; |
tashworth | 13:529323807361 | 1039 | while(1) { |
tashworth | 13:529323807361 | 1040 | if(section == RIGS) { |
tashworth | 13:529323807361 | 1041 | rangeFinderRight.startMeas(); |
tashworth | 13:529323807361 | 1042 | wait_ms(20); |
tashworth | 13:529323807361 | 1043 | rangeFinderRight.getMeas(range); |
tashworth | 12:284be46593ae | 1044 | } else { |
tashworth | 13:529323807361 | 1045 | rangeFinderLeft.startMeas(); |
tashworth | 13:529323807361 | 1046 | wait_ms(20); |
tashworth | 13:529323807361 | 1047 | rangeFinderLeft.getMeas(range); |
tashworth | 13:529323807361 | 1048 | } |
tashworth | 13:529323807361 | 1049 | pc.printf("Range %f \t OldValue %f\n\r",range, usValue); |
tashworth | 13:529323807361 | 1050 | if(range > usValue && usValue != 0 && range < 25) { |
tashworth | 12:284be46593ae | 1051 | break; |
tashworth | 13:529323807361 | 1052 | } else { |
tashworth | 13:529323807361 | 1053 | usValue = range; |
tashworth | 12:284be46593ae | 1054 | } |
tashworth | 8:77a57909aa15 | 1055 | } |
tashworth | 13:529323807361 | 1056 | motors.stopBothMotors(0); |
tashworth | 13:529323807361 | 1057 | } |
tashworth | 13:529323807361 | 1058 | |
tashworth | 13:529323807361 | 1059 | void rightTurn(void) { |
tashworth | 13:529323807361 | 1060 | motors.begin(); |
tashworth | 13:529323807361 | 1061 | leftEncoder.reset(); |
tashworth | 13:529323807361 | 1062 | rightEncoder.reset(); |
tashworth | 13:529323807361 | 1063 | motors.setMotor0Speed(-0.5*127);//right |
tashworth | 13:529323807361 | 1064 | motors.setMotor1Speed(0.5*127);//left |
tashworth | 13:529323807361 | 1065 | while(abs(leftEncoder.getPulses())<950 || abs(rightEncoder.getPulses())<950); |
tashworth | 13:529323807361 | 1066 | motors.stopBothMotors(0); |
tashworth | 13:529323807361 | 1067 | } |
tashworth | 13:529323807361 | 1068 | |
tashworth | 13:529323807361 | 1069 | void leftTurn(void) { |
tashworth | 13:529323807361 | 1070 | motors.begin(); |
tashworth | 13:529323807361 | 1071 | leftEncoder.reset(); |
tashworth | 13:529323807361 | 1072 | rightEncoder.reset(); |
tashworth | 13:529323807361 | 1073 | motors.setMotor0Speed(0.5*127);// right |
tashworth | 13:529323807361 | 1074 | motors.setMotor1Speed(-0.5*127);// left |
tashworth | 13:529323807361 | 1075 | while(abs(leftEncoder.getPulses())<1100 || rightEncoder.getPulses()<1100); |
tashworth | 13:529323807361 | 1076 | motors.stopBothMotors(0); |
tashworth | 13:529323807361 | 1077 | } |
tashworth | 13:529323807361 | 1078 | |
tashworth | 13:529323807361 | 1079 | void slightleft(void) { |
tashworth | 13:529323807361 | 1080 | |
tashworth | 13:529323807361 | 1081 | leftEncoder.reset(); |
tashworth | 13:529323807361 | 1082 | rightEncoder.reset(); |
tashworth | 13:529323807361 | 1083 | motors.setMotor0Speed(0.5*127);// right |
tashworth | 13:529323807361 | 1084 | motors.setMotor1Speed(-0.5*127);// left |
tashworth | 13:529323807361 | 1085 | while(abs(leftEncoder.getPulses())<90 || rightEncoder.getPulses()<90); |
tashworth | 13:529323807361 | 1086 | motors.stopBothMotors(0); |
tashworth | 13:529323807361 | 1087 | } |
tashworth | 13:529323807361 | 1088 | |
tashworth | 13:529323807361 | 1089 | void slightright(void) { |
tashworth | 13:529323807361 | 1090 | |
tashworth | 13:529323807361 | 1091 | leftEncoder.reset(); |
tashworth | 13:529323807361 | 1092 | rightEncoder.reset(); |
tashworth | 13:529323807361 | 1093 | motors.setMotor0Speed(-0.4*127);// right |
tashworth | 13:529323807361 | 1094 | motors.setMotor1Speed(0.4*127);// left |
tashworth | 13:529323807361 | 1095 | while(abs(leftEncoder.getPulses())<90 || abs(rightEncoder.getPulses())<90); |
tashworth | 13:529323807361 | 1096 | motors.stopBothMotors(0); |
tashworth | 13:529323807361 | 1097 | } |
tashworth | 13:529323807361 | 1098 | |
tashworth | 13:529323807361 | 1099 | void slightMove(int direction, float pulses) { |
tashworth | 13:529323807361 | 1100 | int dir=1; |
tashworth | 13:529323807361 | 1101 | |
tashworth | 13:529323807361 | 1102 | if(direction == BACKWARD) dir= -1; |
tashworth | 13:529323807361 | 1103 | |
tashworth | 13:529323807361 | 1104 | leftEncoder.reset(); |
tashworth | 13:529323807361 | 1105 | rightEncoder.reset(); |
tashworth | 13:529323807361 | 1106 | motors.setMotor0Speed(dir*0.25*127); //right |
tashworth | 13:529323807361 | 1107 | motors.setMotor1Speed(dir*0.25*127); //left |
tashworth | 13:529323807361 | 1108 | while(abs(leftEncoder.getPulses()) < pulses || abs(rightEncoder.getPulses()) < pulses); |
tashworth | 13:529323807361 | 1109 | |
tashworth | 13:529323807361 | 1110 | motors.setMotor0Speed(dir*-0.25*127); //right |
tashworth | 13:529323807361 | 1111 | motors.setMotor1Speed(dir*-0.25*127); //left |
tashworth | 13:529323807361 | 1112 | wait_ms(10); |
tashworth | 13:529323807361 | 1113 | motors.stopBothMotors(127); |
tashworth | 13:529323807361 | 1114 | } |
tashworth | 13:529323807361 | 1115 | |
tashworth | 13:529323807361 | 1116 | void UntilWall(int dir) { |
tashworth | 13:529323807361 | 1117 | |
tashworth | 13:529323807361 | 1118 | if(dir == BACKWARD) dir=-1; |
tashworth | 13:529323807361 | 1119 | |
tashworth | 13:529323807361 | 1120 | leftEncoder.reset(); |
tashworth | 13:529323807361 | 1121 | rightEncoder.reset(); |
tashworth | 13:529323807361 | 1122 | motors.setMotor0Speed(dir*0.2*127); //right |
tashworth | 13:529323807361 | 1123 | motors.setMotor1Speed(dir*0.2*127); //left |
tashworth | 13:529323807361 | 1124 | |
tashworth | 13:529323807361 | 1125 | range = 30; |
tashworth | 13:529323807361 | 1126 | |
tashworth | 13:529323807361 | 1127 | while(range > 20) { |
tashworth | 13:529323807361 | 1128 | rangeFinderRight.startMeas(); |
tashworth | 13:529323807361 | 1129 | wait_ms(20); |
tashworth | 13:529323807361 | 1130 | rangeFinderRight.getMeas(range); |
tashworth | 13:529323807361 | 1131 | } |
tashworth | 12:284be46593ae | 1132 | |
tashworth | 13:529323807361 | 1133 | motors.setMotor0Speed(dir*-0.2*127); //right |
tashworth | 13:529323807361 | 1134 | motors.setMotor1Speed(dir*-0.2*127); //left |
tashworth | 13:529323807361 | 1135 | wait_ms(5); |
tashworth | 13:529323807361 | 1136 | motors.stopBothMotors(0); |
tashworth | 13:529323807361 | 1137 | } |
tashworth | 13:529323807361 | 1138 | |
tashworth | 13:529323807361 | 1139 | void overBump(int section) { |
tashworth | 13:529323807361 | 1140 | int preLeft=5000, preRight=5000, out=0; |
tashworth | 13:529323807361 | 1141 | |
tashworth | 13:529323807361 | 1142 | motors.begin(); |
tashworth | 13:529323807361 | 1143 | // slight backwards |
tashworth | 13:529323807361 | 1144 | leftEncoder.reset(); |
tashworth | 13:529323807361 | 1145 | rightEncoder.reset(); |
tashworth | 13:529323807361 | 1146 | motors.setMotor0Speed(-0.25*127); //right |
tashworth | 13:529323807361 | 1147 | motors.setMotor1Speed(-0.25*127); //left |
tashworth | 13:529323807361 | 1148 | while(abs(leftEncoder.getPulses()) < 50 || abs(rightEncoder.getPulses()) < 50); |
tashworth | 13:529323807361 | 1149 | motors.stopBothMotors(0); |
tashworth | 13:529323807361 | 1150 | |
tashworth | 13:529323807361 | 1151 | pc.printf("slight backwards\r\n"); |
tashworth | 13:529323807361 | 1152 | wait_ms(200); |
tashworth | 13:529323807361 | 1153 | |
tashworth | 13:529323807361 | 1154 | leftEncoder.reset(); |
tashworth | 13:529323807361 | 1155 | rightEncoder.reset(); |
tashworth | 13:529323807361 | 1156 | motors.setMotor0Speed(0.3*127); //right |
tashworth | 13:529323807361 | 1157 | motors.setMotor1Speed(0.3*127); //left |
tashworth | 13:529323807361 | 1158 | while((abs(leftEncoder.getPulses()) < 800 || abs(rightEncoder.getPulses()) < 800) /*&& preLeft!=0*/ && IR.getIRDistance() >15 ) { |
tashworth | 13:529323807361 | 1159 | /*preLeft=leftEncoder.getPulses(); |
tashworth | 13:529323807361 | 1160 | preRight=rightEncoder.getPulses(); |
tashworth | 13:529323807361 | 1161 | wait_ms(200); |
tashworth | 13:529323807361 | 1162 | if(leftEncoder.getPulses() == preLeft || rightEncoder.getPulses()== preRight) preLeft=preRight=0;*/ |
tashworth | 13:529323807361 | 1163 | } |
tashworth | 13:529323807361 | 1164 | |
tashworth | 13:529323807361 | 1165 | pc.printf("forward \r\n"); |
tashworth | 13:529323807361 | 1166 | wait_ms(200); |
tashworth | 13:529323807361 | 1167 | /* |
tashworth | 13:529323807361 | 1168 | motors.stopBothMotors(0); |
tashworth | 13:529323807361 | 1169 | motors.begin(); |
tashworth | 13:529323807361 | 1170 | |
tashworth | 13:529323807361 | 1171 | leftEncoder.reset(); |
tashworth | 13:529323807361 | 1172 | rightEncoder.reset(); |
tashworth | 13:529323807361 | 1173 | motors.setMotor0Speed(0.3*127); //right |
tashworth | 13:529323807361 | 1174 | motors.setMotor1Speed(0.3*127); //left |
tashworth | 13:529323807361 | 1175 | |
tashworth | 13:529323807361 | 1176 | while(!out) { |
tashworth | 13:529323807361 | 1177 | preLeft=leftEncoder.getPulses(); |
tashworth | 13:529323807361 | 1178 | preRight=rightEncoder.getPulses(); |
tashworth | 13:529323807361 | 1179 | |
tashworth | 13:529323807361 | 1180 | rangeFinderLeft.startMeas(); |
tashworth | 13:529323807361 | 1181 | rangeFinderRight.startMeas(); |
tashworth | 13:529323807361 | 1182 | wait_ms(20); |
tashworth | 13:529323807361 | 1183 | rangeFinderLeft.getMeas(range); |
tashworth | 13:529323807361 | 1184 | rangeFinderRight.getMeas(range2); |
tashworth | 13:529323807361 | 1185 | if(range < 10 || range2 < 10) out=1; |
tashworth | 13:529323807361 | 1186 | |
tashworth | 13:529323807361 | 1187 | if(leftEncoder.getPulses() == preLeft || rightEncoder.getPulses()== preRight) { |
tashworth | 13:529323807361 | 1188 | motors.setMotor0Speed(0.4*127); //right |
tashworth | 13:529323807361 | 1189 | motors.setMotor1Speed(0.4*127); //left |
tashworth | 13:529323807361 | 1190 | wait_ms(50); |
tashworth | 13:529323807361 | 1191 | out=1; |
tashworth | 13:529323807361 | 1192 | } |
tashworth | 13:529323807361 | 1193 | if(abs(leftEncoder.getPulses()) >1000 || abs(leftEncoder.getPulses())>1000) out=1; |
tashworth | 13:529323807361 | 1194 | } |
tashworth | 13:529323807361 | 1195 | */ |
tashworth | 13:529323807361 | 1196 | |
tashworth | 13:529323807361 | 1197 | motors.stopBothMotors(0); |
tashworth | 13:529323807361 | 1198 | motors.begin(); |
tashworth | 13:529323807361 | 1199 | |
tashworth | 13:529323807361 | 1200 | preLeft=preRight=5000 ; |
tashworth | 13:529323807361 | 1201 | leftEncoder.reset(); |
tashworth | 13:529323807361 | 1202 | rightEncoder.reset(); |
tashworth | 13:529323807361 | 1203 | motors.setMotor0Speed(.25*127); //right |
tashworth | 13:529323807361 | 1204 | motors.setMotor1Speed(.25*127); //left |
tashworth | 13:529323807361 | 1205 | |
tashworth | 13:529323807361 | 1206 | if(section == TOOLS) { |
tashworth | 13:529323807361 | 1207 | while(IR.getIRDistance() > 10 && (abs(leftEncoder.getPulses()) < 300 || abs(rightEncoder.getPulses()) < 300) && (leftEncoder.getPulses() != preLeft || rightEncoder.getPulses() != preRight)) { |
tashworth | 13:529323807361 | 1208 | |
tashworth | 13:529323807361 | 1209 | if(IR.getIRDistance() > 38) break; |
tashworth | 13:529323807361 | 1210 | |
tashworth | 13:529323807361 | 1211 | preLeft=leftEncoder.getPulses(); |
tashworth | 13:529323807361 | 1212 | preRight=rightEncoder.getPulses(); |
tashworth | 13:529323807361 | 1213 | wait_ms(200); |
tashworth | 12:284be46593ae | 1214 | } |
tashworth | 13:529323807361 | 1215 | } else if(section == MID || section == MID2) { |
tashworth | 13:529323807361 | 1216 | if(section == MID2) while(IR.getIRDistance() > 15 && (abs(leftEncoder.getPulses()) < 400 || abs(rightEncoder.getPulses()) < 400)); |
tashworth | 13:529323807361 | 1217 | while(IR.getIRDistance() > 15 && (abs(leftEncoder.getPulses()) < 400 || abs(rightEncoder.getPulses()) < 400) && (leftEncoder.getPulses() != preLeft || rightEncoder.getPulses() != preRight)) { |
tashworth | 13:529323807361 | 1218 | |
tashworth | 13:529323807361 | 1219 | if(IR.getIRDistance() > 38) break; |
tashworth | 13:529323807361 | 1220 | |
tashworth | 13:529323807361 | 1221 | preLeft=leftEncoder.getPulses(); |
tashworth | 13:529323807361 | 1222 | preRight=rightEncoder.getPulses(); |
tashworth | 13:529323807361 | 1223 | wait_ms(200); |
tashworth | 12:284be46593ae | 1224 | } |
tashworth | 13:529323807361 | 1225 | |
tashworth | 12:284be46593ae | 1226 | } else { |
tashworth | 13:529323807361 | 1227 | while(abs(leftEncoder.getPulses()) < 100 || abs(rightEncoder.getPulses()) < 100); |
tashworth | 13:529323807361 | 1228 | |
tashworth | 13:529323807361 | 1229 | leftEncoder.reset(); |
tashworth | 13:529323807361 | 1230 | rightEncoder.reset(); |
tashworth | 13:529323807361 | 1231 | |
tashworth | 13:529323807361 | 1232 | motors.setMotor0Speed(-.15*127); //right |
tashworth | 13:529323807361 | 1233 | motors.setMotor1Speed(-.15*127); //left |
tashworth | 13:529323807361 | 1234 | while((abs(leftEncoder.getPulses()) < 100 || abs(rightEncoder.getPulses()) < 100) && preLeft!=0 ) { |
tashworth | 13:529323807361 | 1235 | preLeft = leftEncoder.getPulses(); |
tashworth | 13:529323807361 | 1236 | preRight = rightEncoder.getPulses(); |
tashworth | 13:529323807361 | 1237 | wait_ms(200); |
tashworth | 13:529323807361 | 1238 | if(leftEncoder.getPulses() == preLeft || rightEncoder.getPulses()== preRight) preLeft=preRight=0; |
tashworth | 13:529323807361 | 1239 | } |
tashworth | 13:529323807361 | 1240 | |
tashworth | 13:529323807361 | 1241 | leftEncoder.reset(); |
tashworth | 13:529323807361 | 1242 | rightEncoder.reset(); |
tashworth | 13:529323807361 | 1243 | |
tashworth | 13:529323807361 | 1244 | motors.setMotor0Speed(0.25*127); //right |
tashworth | 13:529323807361 | 1245 | motors.setMotor1Speed(0.25*127); //left |
tashworth | 13:529323807361 | 1246 | while((abs(leftEncoder.getPulses()) < 10 || abs(rightEncoder.getPulses()) < 10)); |
tashworth | 13:529323807361 | 1247 | |
tashworth | 13:529323807361 | 1248 | motors.stopBothMotors(0); |
tashworth | 13:529323807361 | 1249 | |
tashworth | 13:529323807361 | 1250 | return; |
tashworth | 12:284be46593ae | 1251 | } |
tashworth | 12:284be46593ae | 1252 | |
tashworth | 13:529323807361 | 1253 | leftEncoder.reset(); |
tashworth | 13:529323807361 | 1254 | rightEncoder.reset(); |
tashworth | 13:529323807361 | 1255 | |
tashworth | 13:529323807361 | 1256 | motors.setMotor0Speed(-.25*127); //right |
tashworth | 13:529323807361 | 1257 | motors.setMotor1Speed(-.25*127); //left |
tashworth | 13:529323807361 | 1258 | while((abs(leftEncoder.getPulses()) < 10 || abs(rightEncoder.getPulses()) < 10)); |
tashworth | 13:529323807361 | 1259 | |
tashworth | 13:529323807361 | 1260 | motors.stopBothMotors(0); |
tashworth | 13:529323807361 | 1261 | wait_ms(20); |
tashworth | 13:529323807361 | 1262 | motors.begin(); |
tashworth | 13:529323807361 | 1263 | |
tashworth | 13:529323807361 | 1264 | } |
tashworth | 13:529323807361 | 1265 | |
tashworth | 13:529323807361 | 1266 | void to_tools_section1(float* location, float ¤t) { |
tashworth | 13:529323807361 | 1267 | slightMove(FORWARD,6500); |
tashworth | 13:529323807361 | 1268 | current+=(abs(leftEncoder.getPulses()*11.12/PPR) + abs(rightEncoder.getPulses()*11.12/PPR))/2; |
tashworth | 13:529323807361 | 1269 | |
tashworth | 13:529323807361 | 1270 | } |
tashworth | 13:529323807361 | 1271 | |
tashworth | 13:529323807361 | 1272 | void to_tools_section2(float* location, float ¤t) { |
tashworth | 13:529323807361 | 1273 | slightMove(FORWARD,3200); |
tashworth | 13:529323807361 | 1274 | current+=(abs(leftEncoder.getPulses()*11.12/PPR) + abs(rightEncoder.getPulses()*11.12/PPR))/2; |
tashworth | 13:529323807361 | 1275 | |
tashworth | 13:529323807361 | 1276 | } |
tashworth | 13:529323807361 | 1277 | |
tashworth | 13:529323807361 | 1278 | void from_tools_section(float* location, float ¤t) { |
tashworth | 0:1b64a0cedc5d | 1279 | |
tashworth | 0:1b64a0cedc5d | 1280 | |
tashworth | 13:529323807361 | 1281 | alignWithWall(TOOLS); |
tashworth | 13:529323807361 | 1282 | pc.printf("align\r\n"); |
tashworth | 13:529323807361 | 1283 | wait_ms(100); |
tashworth | 12:284be46593ae | 1284 | |
tashworth | 13:529323807361 | 1285 | //wall_follow2(LEFT,FORWARD,MID, current); |
tashworth | 13:529323807361 | 1286 | //current+=((abs(leftEncoder.getPulses()*11.12/PPR) + abs(rightEncoder.getPulses()*11.12/PPR))/2); |
tashworth | 12:284be46593ae | 1287 | |
tashworth | 12:284be46593ae | 1288 | rangeFinderLeft.startMeas(); |
tashworth | 12:284be46593ae | 1289 | wait_ms(20); |
tashworth | 12:284be46593ae | 1290 | rangeFinderLeft.getMeas(range); |
tashworth | 12:284be46593ae | 1291 | |
tashworth | 13:529323807361 | 1292 | if(range < 20) { |
tashworth | 13:529323807361 | 1293 | wall_follow2(LEFT,BACKWARD,TOOLS, current,0); |
tashworth | 13:529323807361 | 1294 | pc.printf("wall follow\r\n"); |
tashworth | 13:529323807361 | 1295 | location[0]= current - ((abs(leftEncoder.getPulses()*11.12/PPR) + abs(rightEncoder.getPulses()*11.12/PPR))/2); |
tashworth | 13:529323807361 | 1296 | current= location[0]; |
tashworth | 13:529323807361 | 1297 | pc.printf("current %f \r\n",current); |
tashworth | 13:529323807361 | 1298 | // go backwards |
tashworth | 13:529323807361 | 1299 | leftEncoder.reset(); |
tashworth | 13:529323807361 | 1300 | rightEncoder.reset(); |
tashworth | 13:529323807361 | 1301 | motors.setMotor0Speed(-MAX_SPEED); //right |
tashworth | 13:529323807361 | 1302 | motors.setMotor1Speed(-MAX_SPEED); //left |
tashworth | 13:529323807361 | 1303 | while(abs(leftEncoder.getPulses()) < 120 || abs(rightEncoder.getPulses())< 120); |
tashworth | 13:529323807361 | 1304 | // hard stop |
tashworth | 13:529323807361 | 1305 | leftEncoder.reset(); |
tashworth | 13:529323807361 | 1306 | rightEncoder.reset(); |
tashworth | 13:529323807361 | 1307 | motors.setMotor0Speed(MAX_SPEED); //right |
tashworth | 13:529323807361 | 1308 | motors.setMotor1Speed(MAX_SPEED); //left |
tashworth | 13:529323807361 | 1309 | while(abs(leftEncoder.getPulses()) < 10 || abs(rightEncoder.getPulses())< 10); |
tashworth | 13:529323807361 | 1310 | motors.stopBothMotors(0); |
tashworth | 12:284be46593ae | 1311 | |
tashworth | 13:529323807361 | 1312 | wait_ms(100); |
tashworth | 13:529323807361 | 1313 | leftTurn(); |
tashworth | 13:529323807361 | 1314 | overBump(TOOLS); |
tashworth | 13:529323807361 | 1315 | } else { |
tashworth | 13:529323807361 | 1316 | pc.printf("else greater than 20\r\n"); |
tashworth | 13:529323807361 | 1317 | location[0]= current; |
tashworth | 13:529323807361 | 1318 | leftTurn(); |
tashworth | 13:529323807361 | 1319 | overBump(TOOLS); |
tashworth | 13:529323807361 | 1320 | } |
tashworth | 8:77a57909aa15 | 1321 | |
tashworth | 13:529323807361 | 1322 | pc.printf("First Wavegap = %f\r\n",location[0]); |
tashworth | 12:284be46593ae | 1323 | |
tashworth | 8:77a57909aa15 | 1324 | } |
tashworth | 12:284be46593ae | 1325 | |
tashworth | 13:529323807361 | 1326 | void mid_section(float* location, float ¤t, int* direction) { |
tashworth | 13:529323807361 | 1327 | motors.begin(); |
tashworth | 12:284be46593ae | 1328 | |
tashworth | 13:529323807361 | 1329 | if(IR.getIRDistance() > 38) { |
tashworth | 13:529323807361 | 1330 | direction[0]= STRAIGHT; |
tashworth | 13:529323807361 | 1331 | overBump(MID); |
tashworth | 13:529323807361 | 1332 | return; |
tashworth | 13:529323807361 | 1333 | } |
tashworth | 13:529323807361 | 1334 | pc.printf("before align with wall \r\n"); |
tashworth | 13:529323807361 | 1335 | alignWithWall(MID); |
tashworth | 13:529323807361 | 1336 | wait_ms(100); |
tashworth | 13:529323807361 | 1337 | |
tashworth | 13:529323807361 | 1338 | pc.printf("mid section current = %f\r\n",current); |
tashworth | 13:529323807361 | 1339 | wall_follow2(LEFT,FORWARD,MID, current,0); |
tashworth | 13:529323807361 | 1340 | current+=((abs(leftEncoder.getPulses()*11.12/PPR) + abs(rightEncoder.getPulses()*11.12/PPR))/2); |
tashworth | 13:529323807361 | 1341 | pc.printf("after wf2 current = %f\r\n",current); |
tashworth | 13:529323807361 | 1342 | |
tashworth | 13:529323807361 | 1343 | wait_ms(500); |
tashworth | 13:529323807361 | 1344 | rangeFinderLeft.startMeas(); |
tashworth | 13:529323807361 | 1345 | wait_ms(20); |
tashworth | 13:529323807361 | 1346 | rangeFinderLeft.getMeas(range); |
tashworth | 12:284be46593ae | 1347 | |
tashworth | 13:529323807361 | 1348 | if(range > 20 ) { |
tashworth | 13:529323807361 | 1349 | direction[0]= RIGHT; |
tashworth | 13:529323807361 | 1350 | location[1]= current; |
tashworth | 13:529323807361 | 1351 | slightMove(FORWARD,75); |
tashworth | 13:529323807361 | 1352 | //current+=((abs(leftEncoder.getPulses()*11.12/PPR) + abs(rightEncoder.getPulses()*11.12/PPR))/2); |
tashworth | 13:529323807361 | 1353 | } else { |
tashworth | 13:529323807361 | 1354 | direction[0]= LEFT; |
tashworth | 13:529323807361 | 1355 | wall_follow2(LEFT,BACKWARD,MID,current,0); |
tashworth | 13:529323807361 | 1356 | location[1]= current- ((abs(leftEncoder.getPulses()*11.12/PPR) + abs(rightEncoder.getPulses()*11.12/PPR))/2); |
tashworth | 13:529323807361 | 1357 | current= location[1]; |
tashworth | 13:529323807361 | 1358 | |
tashworth | 13:529323807361 | 1359 | if(location[1] < 18) { |
tashworth | 13:529323807361 | 1360 | slightMove(FORWARD, 50); |
tashworth | 13:529323807361 | 1361 | current+=((abs(leftEncoder.getPulses()*11.12/PPR) + abs(rightEncoder.getPulses()*11.12/PPR))/2); |
tashworth | 13:529323807361 | 1362 | } |
tashworth | 13:529323807361 | 1363 | |
tashworth | 13:529323807361 | 1364 | } |
tashworth | 13:529323807361 | 1365 | |
tashworth | 13:529323807361 | 1366 | pc.printf("wavegap2 = %f\r\n",location[1]); |
tashworth | 13:529323807361 | 1367 | leftTurn(); |
tashworth | 13:529323807361 | 1368 | |
tashworth | 13:529323807361 | 1369 | wait_ms(100); |
tashworth | 13:529323807361 | 1370 | |
tashworth | 13:529323807361 | 1371 | overBump(MID); |
tashworth | 13:529323807361 | 1372 | |
tashworth | 12:284be46593ae | 1373 | } |
tashworth | 12:284be46593ae | 1374 | |
tashworth | 13:529323807361 | 1375 | void mid_section2(float* location, float ¤t, int* direction) { |
tashworth | 13:529323807361 | 1376 | motors.begin(); |
tashworth | 12:284be46593ae | 1377 | |
tashworth | 13:529323807361 | 1378 | pc.printf("mid section 2\r\n"); |
tashworth | 8:77a57909aa15 | 1379 | |
tashworth | 13:529323807361 | 1380 | if(IR.getIRDistance() > 38) { |
tashworth | 13:529323807361 | 1381 | direction[1]= STRAIGHT; |
tashworth | 13:529323807361 | 1382 | overBump(RIGS); |
tashworth | 13:529323807361 | 1383 | return; |
tashworth | 13:529323807361 | 1384 | } |
tashworth | 8:77a57909aa15 | 1385 | |
tashworth | 13:529323807361 | 1386 | alignWithWall(MID); |
tashworth | 13:529323807361 | 1387 | pc.printf("midsection 2 alignt with wall mid \r\n"); |
tashworth | 12:284be46593ae | 1388 | |
tashworth | 13:529323807361 | 1389 | wall_follow2(LEFT,FORWARD,MID, current,0); |
tashworth | 13:529323807361 | 1390 | current+=((abs(leftEncoder.getPulses()*11.12/PPR) + abs(rightEncoder.getPulses()*11.12/PPR))/2); |
tashworth | 8:77a57909aa15 | 1391 | |
tashworth | 13:529323807361 | 1392 | wait_ms(500); |
tashworth | 8:77a57909aa15 | 1393 | |
tashworth | 13:529323807361 | 1394 | pc.printf("midseection 2 after wf2 %f",current); |
tashworth | 13:529323807361 | 1395 | rangeFinderLeft.startMeas(); |
tashworth | 13:529323807361 | 1396 | wait_ms(20); |
tashworth | 13:529323807361 | 1397 | rangeFinderLeft.getMeas(range); |
tashworth | 12:284be46593ae | 1398 | |
tashworth | 13:529323807361 | 1399 | if(range > 20 ) { |
tashworth | 13:529323807361 | 1400 | direction[1]= RIGHT; |
tashworth | 13:529323807361 | 1401 | location[2]= current; |
tashworth | 13:529323807361 | 1402 | slightMove(FORWARD,75); |
tashworth | 13:529323807361 | 1403 | //current+=((abs(leftEncoder.getPulses()*11.12/PPR) + abs(rightEncoder.getPulses()*11.12/PPR))/2); |
tashworth | 13:529323807361 | 1404 | } else { |
tashworth | 13:529323807361 | 1405 | direction[1]= LEFT; |
tashworth | 13:529323807361 | 1406 | wall_follow2(LEFT,BACKWARD,MID,current,0); |
tashworth | 13:529323807361 | 1407 | location[2]= current- ((abs(leftEncoder.getPulses()*11.12/PPR) + abs(rightEncoder.getPulses()*11.12/PPR))/2); |
tashworth | 13:529323807361 | 1408 | current=location[2]; |
tashworth | 13:529323807361 | 1409 | //slightMove(FORWARD,500); |
tashworth | 13:529323807361 | 1410 | } |
tashworth | 8:77a57909aa15 | 1411 | |
tashworth | 13:529323807361 | 1412 | leftTurn(); |
tashworth | 13:529323807361 | 1413 | overBump(RIGS); |
tashworth | 13:529323807361 | 1414 | pc.printf("overbump rigs\r\n"); |
tashworth | 12:284be46593ae | 1415 | } |
tashworth | 12:284be46593ae | 1416 | |
tashworth | 13:529323807361 | 1417 | void rig_section(float* location, float ¤t, int* direction, int rig) { |
tashworth | 13:529323807361 | 1418 | float loc; |
tashworth | 8:77a57909aa15 | 1419 | |
tashworth | 13:529323807361 | 1420 | if(rig == 1) loc= 15; |
tashworth | 13:529323807361 | 1421 | else if(rig == 2) loc= 45; |
tashworth | 13:529323807361 | 1422 | else loc = 75; |
tashworth | 8:77a57909aa15 | 1423 | |
tashworth | 13:529323807361 | 1424 | rightTurn(); |
tashworth | 13:529323807361 | 1425 | slightright(); |
tashworth | 8:77a57909aa15 | 1426 | |
tashworth | 13:529323807361 | 1427 | if(current > loc) { |
tashworth | 13:529323807361 | 1428 | pc.printf("RIG section %f\r\n",current); |
tashworth | 13:529323807361 | 1429 | wall_follow2(RIGHT, BACKWARD, RIGS, current, rig); |
tashworth | 13:529323807361 | 1430 | current-=((abs(leftEncoder.getPulses()*11.12/PPR) + abs(rightEncoder.getPulses()*11.12/PPR))/2); |
tashworth | 13:529323807361 | 1431 | } else { |
tashworth | 13:529323807361 | 1432 | pc.printf("RIG section %f\r\n",current); |
tashworth | 13:529323807361 | 1433 | wall_follow2(RIGHT, FORWARD, RIGS, current, rig); |
tashworth | 13:529323807361 | 1434 | current+=((abs(leftEncoder.getPulses()*11.12/PPR) + abs(rightEncoder.getPulses()*11.12/PPR))/2); |
tashworth | 13:529323807361 | 1435 | } |
tashworth | 13:529323807361 | 1436 | } |
tashworth | 12:284be46593ae | 1437 | |
tashworth | 13:529323807361 | 1438 | void tools_section_return(float* location, float ¤t) { |
tashworth | 13:529323807361 | 1439 | if(location[0] > 16) { |
tashworth | 13:529323807361 | 1440 | leftTurn(); |
tashworth | 13:529323807361 | 1441 | wall_follow2(LEFT, BACKWARD, RETURN, location[0], 0); |
tashworth | 13:529323807361 | 1442 | } |
tashworth | 13:529323807361 | 1443 | motors.stopBothMotors(0); |
tashworth | 8:77a57909aa15 | 1444 | |
tashworth | 13:529323807361 | 1445 | } |
tashworth | 12:284be46593ae | 1446 | |
tashworth | 13:529323807361 | 1447 | void mid_section_return(float* location, float ¤t, int* direction) { |
tashworth | 13:529323807361 | 1448 | if(direction[0] == RIGHT) { |
tashworth | 13:529323807361 | 1449 | leftTurn(); |
tashworth | 13:529323807361 | 1450 | alignWithWall(MID); |
tashworth | 13:529323807361 | 1451 | wall_follow2(LEFT, BACKWARD, MID, current,0); |
tashworth | 13:529323807361 | 1452 | current-=((abs(leftEncoder.getPulses()*11.12/PPR) + abs(rightEncoder.getPulses()*11.12/PPR))/2); |
tashworth | 13:529323807361 | 1453 | rightTurn(); |
tashworth | 13:529323807361 | 1454 | } else if(direction[0] == LEFT) { |
tashworth | 13:529323807361 | 1455 | leftTurn(); |
tashworth | 13:529323807361 | 1456 | wall_follow2(RIGHT, FORWARD, MID, current,0); |
tashworth | 13:529323807361 | 1457 | current+=((abs(leftEncoder.getPulses()*11.12/PPR) + abs(rightEncoder.getPulses()*11.12/PPR))/2); |
tashworth | 13:529323807361 | 1458 | rightTurn(); |
tashworth | 13:529323807361 | 1459 | } |
tashworth | 13:529323807361 | 1460 | //ELSE and GO FORWARD |
tashworth | 13:529323807361 | 1461 | overBump(RIGS); |
tashworth | 13:529323807361 | 1462 | } |
tashworth | 8:77a57909aa15 | 1463 | |
tashworth | 13:529323807361 | 1464 | void mid_section2_return(float* location, float ¤t, int* direction) { |
tashworth | 13:529323807361 | 1465 | if(direction[1] == RIGHT) { |
tashworth | 13:529323807361 | 1466 | leftTurn(); |
tashworth | 13:529323807361 | 1467 | wall_follow2(LEFT, BACKWARD, MID, current,0); |
tashworth | 13:529323807361 | 1468 | current-=((abs(leftEncoder.getPulses()*11.12/PPR) + abs(rightEncoder.getPulses()*11.12/PPR))/2); |
tashworth | 13:529323807361 | 1469 | rightTurn(); |
tashworth | 13:529323807361 | 1470 | } else if(direction[1] == LEFT) { |
tashworth | 13:529323807361 | 1471 | leftTurn(); |
tashworth | 13:529323807361 | 1472 | wall_follow2(RIGHT, FORWARD, MID, current,0); |
tashworth | 13:529323807361 | 1473 | current+=((abs(leftEncoder.getPulses()*11.12/PPR) + abs(rightEncoder.getPulses()*11.12/PPR))/2); |
tashworth | 13:529323807361 | 1474 | rightTurn(); |
tashworth | 13:529323807361 | 1475 | } |
tashworth | 13:529323807361 | 1476 | //ELSE and GO FORWARD |
tashworth | 13:529323807361 | 1477 | overBump(MID); |
tashworth | 13:529323807361 | 1478 | } |
tashworth | 12:284be46593ae | 1479 | |
tashworth | 13:529323807361 | 1480 | void rig_section_return(float* location, float ¤t, int* direction) { |
tashworth | 13:529323807361 | 1481 | alignWithWall(RIGS); |
tashworth | 13:529323807361 | 1482 | if(location[2] > current) { |
tashworth | 13:529323807361 | 1483 | wall_follow2(RIGHT, FORWARD, MID, current,0); |
tashworth | 13:529323807361 | 1484 | current+=((abs(leftEncoder.getPulses()*11.12/PPR) + abs(rightEncoder.getPulses()*11.12/PPR))/2); |
tashworth | 13:529323807361 | 1485 | } else { |
tashworth | 13:529323807361 | 1486 | wall_follow2(RIGHT, BACKWARD, MID, current,0); |
tashworth | 13:529323807361 | 1487 | current-=((abs(leftEncoder.getPulses()*11.12/PPR) + abs(rightEncoder.getPulses()*11.12/PPR))/2); |
tashworth | 13:529323807361 | 1488 | } |
tashworth | 13:529323807361 | 1489 | rightTurn(); |
tashworth | 13:529323807361 | 1490 | overBump(MID2); |
tashworth | 13:529323807361 | 1491 | } |
tashworth | 8:77a57909aa15 | 1492 | |
tashworth | 8:77a57909aa15 | 1493 | |
tashworth | 8:77a57909aa15 | 1494 | |
tashworth | 12:284be46593ae | 1495 | |
tashworth | 12:284be46593ae | 1496 |