00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "pic24_all.h"
00030 #include <stdio.h>
00031
00037 extern uint8 irFifoRead(void);
00038 extern void configTimer2(void);
00039 extern void configInputCapture1(void);
00040 extern void configOutputCapture1(void);
00041 extern uint8 irFifoDataRdy(void);
00042
00043 #define WHEEL_PWM_STEPS 10 //number of PWM steps
00044 #define WHEEL_PWM_STEPSIZE 500 //in microseconds
00045 #define MAN_DRIVE 0
00046 #define AUTO_DRIVE 1
00047
00048 #define BLOCKAGE_PRESENT 560 //1.8 V
00049 #define BLOCKAGE_CLEAR 434 //1.4 V
00050
00051
00052 uint8 u8_leftDC, u8_rightDC;
00053 uint8 u8_leftPeriod,u8_rightPeriod ;
00054 uint16 u16_adcVal;
00055
00056
00057 typedef struct tagFLAGBITS {
00058 unsigned u1_mode:
00059 1;
00060 unsigned u1_obstacle:
00061 1;
00062 unsigned u1_irCmdFlag:
00063 1;
00064 }FLAGBITS;
00065 static volatile FLAGBITS flags;
00066
00067
00068 #define WHEEL_LEFT_1A _LATB1
00069 #define WHEEL_LEFT_2A _LATB3
00070 #define WHEEL_LEFT_EN _LATB2
00071 #define WHEEL_RIGHT_1A _LATB14
00072 #define WHEEL_RIGHT_2A _LATB12
00073 #define WHEEL_RIGHT_EN _LATB13
00074
00075
00076 #define WHEEL_LEFT_FORWARD() { WHEEL_LEFT_1A = 1; WHEEL_LEFT_2A = 0; }
00077 #define WHEEL_LEFT_REVERSE() { WHEEL_LEFT_1A = 0; WHEEL_LEFT_2A = 1; }
00078 #define WHEEL_LEFT_STOP() { WHEEL_LEFT_1A = 0; WHEEL_LEFT_2A = 0; }
00079 #define WHEEL_RIGHT_FORWARD() { WHEEL_RIGHT_1A = 1; WHEEL_RIGHT_2A = 0; }
00080 #define WHEEL_RIGHT_REVERSE() { WHEEL_RIGHT_1A = 0; WHEEL_RIGHT_2A = 1; }
00081 #define WHEEL_RIGHT_STOP() { WHEEL_RIGHT_1A = 0; WHEEL_RIGHT_2A = 0; }
00082
00083 static inline void configLeftWheel(void) {
00084 CONFIG_RB1_AS_DIG_OUTPUT();
00085 CONFIG_RB2_AS_DIG_OUTPUT();
00086 CONFIG_RB3_AS_DIG_OUTPUT();
00087 }
00088
00089 static inline void configRightWheel(void) {
00090 CONFIG_RB12_AS_DIG_OUTPUT();
00091 CONFIG_RB13_AS_DIG_OUTPUT();
00092 CONFIG_RB14_AS_DIG_OUTPUT();
00093 }
00094
00095
00096 void _ISRFAST _T3Interrupt (void) {
00097 uint16 u16_temp;
00098 _T3IF = 0;
00099 if (u8_rightDC) {
00100 if ( u8_rightPeriod == WHEEL_PWM_STEPS || !u8_rightPeriod) {
00101 u8_rightPeriod = 0;
00102 WHEEL_RIGHT_EN = 1;
00103 } else if (u8_rightPeriod >= u8_rightDC) WHEEL_RIGHT_EN = 0;
00104 u8_rightPeriod++;
00105 } else {
00106 WHEEL_RIGHT_EN = 0;
00107 u8_rightPeriod = 0;
00108 }
00109 if (u8_leftDC) {
00110 if ( u8_leftPeriod == WHEEL_PWM_STEPS || !u8_leftPeriod) {
00111 u8_leftPeriod = 0;
00112 WHEEL_LEFT_EN = 1;
00113 } else if (u8_leftPeriod >= u8_leftDC) WHEEL_LEFT_EN = 0;
00114 u8_leftPeriod++;
00115 } else {
00116 WHEEL_LEFT_EN = 0;
00117 u8_rightPeriod = 0;
00118 }
00119 u16_temp = ADC1BUF0;
00120 if (u16_temp > BLOCKAGE_PRESENT ) flags.u1_obstacle = 1;
00121 else if (u16_temp < BLOCKAGE_CLEAR) flags.u1_obstacle = 0;
00122 AD1CON1bits.SAMP = 1;
00123 }
00124
00125 void configTimer3(void) {
00126
00127 T2CONbits.T32 = 0;
00128 T3CON = T3_OFF |T3_IDLE_CON | T3_GATE_OFF
00129 | T3_SOURCE_INT
00130 | T3_PS_1_8 ;
00131 PR3 = usToU16Ticks (WHEEL_PWM_STEPSIZE, getTimerPrescale(T3CONbits)) - 1;
00132 TMR3 = 0;
00133 _T3IF = 0;
00134 _T3IP = 1;
00135 _T3IE = 1;
00136 T3CONbits.TON = 1;
00137 }
00138
00139
00140
00141
00142
00143
00144 void configWheels(void) {
00145 u8_leftDC = 0;
00146 u8_rightDC = 0;
00147 u8_leftPeriod = 0;
00148 u8_rightPeriod = 0;
00149 configLeftWheel();
00150 configRightWheel();
00151 WHEEL_LEFT_FORWARD();
00152 WHEEL_RIGHT_FORWARD();
00153 configTimer3();
00154 }
00155
00156 #define CMD_STOP 0x36 //VCR stop button
00157 #define CMD_SPEEDUP 0x29 //VCR pause button
00158 #define CMD_SLOWDOWN 0x37 //VCR record button
00159 #define CMD_FORWARD 0x35 //VCR play button
00160 #define CMD_TURN_LEFT 0x32 //VCR rewind button
00161 #define CMD_TURN_RIGHT 0x34 //VCR fast forward button
00162 #define CMD_REVERSE 0x05 //VCR numeral 5
00163 #define CMD_SPIN 0x09 //VCR numeral 9
00164 #define CMD_MODESWAP 0x01 //VCR numeral 1
00165 #define CMD_NOP 0x00
00166
00167 #define DEFAULT_SPEED 4
00168 #define DEFAULT_SPEED 4
00169 #define BACK_SPEED 6
00170
00171 void doSpeedUp(uint8 u8_speed) {
00172 u8_leftDC = 0;
00173 u8_rightDC = 0;
00174 do {
00175 u8_leftDC++;
00176 u8_rightDC++;
00177 DELAY_MS(100);
00178 } while (u8_leftDC < u8_speed);
00179 }
00180
00181 void allStop(void) {
00182
00183 while (u8_leftDC || u8_rightDC) {
00184 if (u8_leftDC) u8_leftDC--;
00185 if (u8_rightDC) u8_rightDC--;
00186 DELAY_MS(100);
00187 }
00188 }
00189
00190 void goForward(uint8 u8_speed) {
00191 WHEEL_LEFT_FORWARD();
00192 WHEEL_RIGHT_FORWARD();
00193 doSpeedUp(u8_speed);
00194 }
00195
00196 void goReverse(uint8 u8_speed) {
00197 WHEEL_LEFT_REVERSE();
00198 WHEEL_RIGHT_REVERSE();
00199 doSpeedUp(u8_speed);
00200 }
00201
00202 void processCmd(uint8 u8_cmd) {
00203 switch (u8_cmd) {
00204 case CMD_SPEEDUP:
00205 if (u8_leftDC < WHEEL_PWM_STEPS) u8_leftDC += 1;
00206 if (u8_rightDC < WHEEL_PWM_STEPS) u8_rightDC += 1;
00207 break;
00208 case CMD_SLOWDOWN:
00209 if (u8_leftDC) u8_leftDC -= 1;
00210 if (u8_rightDC) u8_rightDC -= 1;
00211 break;
00212 case CMD_STOP:
00213 allStop();
00214 break;
00215 case CMD_FORWARD:
00216 allStop();
00217 goForward(DEFAULT_SPEED);
00218 break;
00219 case CMD_REVERSE:
00220 allStop();
00221 goReverse(DEFAULT_SPEED);
00222 break;
00223 case CMD_TURN_LEFT:
00224 if (u8_leftDC) u8_leftDC -= 1;
00225 break;
00226 case CMD_TURN_RIGHT:
00227 if (u8_rightDC) u8_rightDC -= 1;
00228 break;
00229 case CMD_SPIN:
00230 allStop();
00231 WHEEL_LEFT_FORWARD();
00232 WHEEL_RIGHT_REVERSE();
00233 doSpeedUp(DEFAULT_SPEED);
00234 break;
00235 case CMD_MODESWAP:
00236 allStop();
00237 flags.u1_mode = !flags.u1_mode;
00238 break;
00239 default:
00240 break;
00241 }
00242 }
00243
00244 uint8 getIRCMD(void) {
00245 uint8 u8_x, u8_cmd;
00246 u8_x = irFifoRead();
00247 u8_cmd = irFifoRead();
00248 if (u8_x & 0x20) outString("Toggle = 1, ");
00249 else outString("Toggle = 0, ");
00250 outString("Addr: ");
00251 outUint8(u8_x & 0x1F);
00252 outString(",Cmd: ");
00253 outUint8(u8_cmd);
00254 outString("\n");
00255 if (flags.u1_irCmdFlag) flags.u1_irCmdFlag = 0;
00256 else flags.u1_irCmdFlag = 1;
00257 if (flags.u1_irCmdFlag) return u8_cmd;
00258 else return CMD_NOP;
00259 }
00260
00261 void checkIR(void) {
00262 uint8 u8_cmd;
00263 if (irFifoDataRdy()) {
00264 u8_cmd = getIRCMD();
00265 processCmd(u8_cmd);
00266 }
00267 }
00268
00269 void doTurn(uint8 u8_ltarg, uint8 u8_rtarg) {
00270 u8_leftDC = 0;
00271 u8_rightDC = 0;
00272 while ((u8_leftDC != u8_ltarg) ||
00273 (u8_rightDC != u8_rtarg) ) {
00274 if (u8_leftDC != u8_ltarg) u8_leftDC++;
00275 if (u8_rightDC != u8_rtarg) u8_rightDC++;
00276 DELAY_MS(80);
00277 }
00278 }
00279
00280
00281 void autoDrive(void) {
00282 if (flags.u1_obstacle) {
00283 allStop();
00284
00285 WHEEL_LEFT_REVERSE();
00286 WHEEL_RIGHT_REVERSE();
00287 doSpeedUp(BACK_SPEED);
00288 DELAY_MS(300);
00289 u8_leftDC = 2;
00290 DELAY_MS(800);
00291 allStop();
00292 DELAY_MS(200);
00293 WHEEL_LEFT_FORWARD();
00294 WHEEL_RIGHT_FORWARD();
00295 doTurn(BACK_SPEED,2);
00296 DELAY_MS(600);
00297 doSpeedUp(DEFAULT_SPEED);
00298 } else if (u8_leftDC != DEFAULT_SPEED ||
00299 u8_rightDC != DEFAULT_SPEED ) {
00300 WHEEL_LEFT_FORWARD();
00301 WHEEL_RIGHT_FORWARD();
00302 doSpeedUp(DEFAULT_SPEED);
00303 }
00304 }
00305
00306 int main (void) {
00307 configBasic(HELLO_MSG);
00308 CONFIG_AN1_AS_ANALOG();
00309 configADC1_ManualCH0( ADC_CH0_POS_SAMPLEA_AN1, 31, 1 );
00310 SET_SAMP_BIT_ADC1();
00311 configTimer2();
00312 configInputCapture1();
00313 configWheels();
00314 while (1) {
00315 checkIR();
00316 if (flags.u1_mode == AUTO_DRIVE) autoDrive();
00317 doHeartbeat();
00318 }
00319 }