00001 #ifndef _ADK_SHIELD_
00002 #define _ADK_SHIELD_
00003
00004 #include <WProgram.h>
00005 #include <Wire.h>
00006 #include <Servo.h>
00007 #include <CapSense.h>
00008
00009
00010 #define LED3_RED 2
00011 #define LED3_GREEN 4
00012 #define LED3_BLUE 3
00013
00014 #define LED2_RED 5
00015 #define LED2_GREEN 7
00016 #define LED2_BLUE 6
00017
00018 #define LED1_RED 8
00019 #define LED1_GREEN 10
00020 #define LED1_BLUE 9
00021
00022 #define SERVO1 11
00023 #define SERVO2 12
00024 #define SERVO3 13
00025
00026 #define TOUCH_RECV 14
00027 #define TOUCH_SEND 15
00028
00029 #define RELAY1 A0
00030 #define RELAY2 A1
00031
00032 #define LIGHT_SENSOR A2
00033 #define TEMP_SENSOR A3
00034
00035 #define BUTTON1 A6
00036 #define BUTTON2 A7
00037 #define BUTTON3 A8
00038
00039 #define JOY_SWITCH A9 // pulls line down when pressed
00040 #define JOY_nINT A10 // active low interrupt input
00041 #define JOY_nRESET A11 // active low reset output
00042
00043
00044 CapSense touch_robot = CapSense(TOUCH_SEND, TOUCH_RECV);
00045 bool prior_robot_state;
00046
00047
00048 void init_buttons()
00049 {
00050 pinMode(BUTTON1, INPUT);
00051 pinMode(BUTTON2, INPUT);
00052 pinMode(BUTTON3, INPUT);
00053 pinMode(JOY_SWITCH, INPUT);
00054
00055
00056 digitalWrite(BUTTON1, HIGH);
00057 digitalWrite(BUTTON2, HIGH);
00058 digitalWrite(BUTTON3, HIGH);
00059 digitalWrite(JOY_SWITCH, HIGH);
00060 }
00061
00062
00063
00064 void init_relays()
00065 {
00066 pinMode(RELAY1, OUTPUT);
00067 pinMode(RELAY2, OUTPUT);
00068 }
00069
00070
00071 void init_leds()
00072 {
00073 digitalWrite(LED1_RED, 1);
00074 digitalWrite(LED1_GREEN, 1);
00075 digitalWrite(LED1_BLUE, 1);
00076
00077 pinMode(LED1_RED, OUTPUT);
00078 pinMode(LED1_GREEN, OUTPUT);
00079 pinMode(LED1_BLUE, OUTPUT);
00080
00081 digitalWrite(LED2_RED, 1);
00082 digitalWrite(LED2_GREEN, 1);
00083 digitalWrite(LED2_BLUE, 1);
00084
00085 pinMode(LED2_RED, OUTPUT);
00086 pinMode(LED2_GREEN, OUTPUT);
00087 pinMode(LED2_BLUE, OUTPUT);
00088
00089 digitalWrite(LED3_RED, 1);
00090 digitalWrite(LED3_GREEN, 1);
00091 digitalWrite(LED3_BLUE, 1);
00092
00093 pinMode(LED3_RED, OUTPUT);
00094 pinMode(LED3_GREEN, OUTPUT);
00095 pinMode(LED3_BLUE, OUTPUT);
00096 }
00097
00098
00099
00100
00101 #define JOY_I2C_ADDR 0x40
00102
00103 char read_joy_reg(char reg_addr)
00104 {
00105 char c;
00106
00107 Wire.beginTransmission(JOY_I2C_ADDR);
00108 Wire.send(reg_addr);
00109 Wire.endTransmission();
00110
00111 Wire.requestFrom(JOY_I2C_ADDR, 1);
00112
00113 while(Wire.available())
00114 c = Wire.receive();
00115
00116 return c;
00117 }
00118
00119 void write_joy_reg(char reg_addr, char val)
00120 {
00121 Wire.beginTransmission(JOY_I2C_ADDR);
00122 Wire.send(reg_addr);
00123 Wire.send(val);
00124 Wire.endTransmission();
00125 }
00126
00127 char joystick_interrupt()
00128 {
00129 return digitalRead(JOY_nINT) == 0;
00130 }
00131
00132
00133 int offset_X, offset_Y;
00134
00135 void calibrate_joystick(int dz)
00136 {
00137 char iii;
00138 int x_cal = 0;
00139 int y_cal = 0;
00140
00141
00142
00143
00144
00145 write_joy_reg(0x0f, 0x00);
00146 delay(1);
00147
00148
00149 read_joy_reg(0x11);
00150
00151 for(iii = 0; iii != 16; iii++) {
00152 while(!joystick_interrupt()) {}
00153
00154 x_cal += read_joy_reg(0x10);
00155 y_cal += read_joy_reg(0x11);
00156 }
00157
00158
00159 offset_X = -(x_cal>>4);
00160 offset_Y = -(y_cal>>4);
00161
00162 write_joy_reg(0x12, dz - offset_X);
00163 write_joy_reg(0x13, -dz - offset_X);
00164 write_joy_reg(0x14, dz - offset_Y);
00165 write_joy_reg(0x15, -dz - offset_Y);
00166
00167
00168 if (dz)
00169 write_joy_reg(0x0f, 0x04);
00170 }
00171
00172 void init_joystick(int threshold)
00173 {
00174 byte status = 0;
00175
00176 pinMode(JOY_SWITCH, INPUT);
00177 digitalWrite(JOY_SWITCH, HIGH);
00178
00179 pinMode(JOY_nINT, INPUT);
00180 digitalWrite(JOY_nINT, HIGH);
00181
00182 pinMode(JOY_nRESET, OUTPUT);
00183
00184 digitalWrite(JOY_nRESET, 1);
00185 delay(1);
00186 digitalWrite(JOY_nRESET, 0);
00187 delay(1);
00188 digitalWrite(JOY_nRESET, 1);
00189
00190 Wire.begin();
00191
00192 do {
00193 status = read_joy_reg(0x0f);
00194 } while ((status & 0xf0) != 0xf0);
00195
00196
00197 write_joy_reg(0x2e, 0x86);
00198
00199 calibrate_joystick(threshold);
00200 }
00201
00202
00203
00204
00205
00206
00207 void read_joystick(int *x, int *y)
00208 {
00209 *x = read_joy_reg(0x10) + offset_X;
00210 *y = read_joy_reg(0x11) + offset_Y;
00211 }
00212
00213 Servo servos[3];
00214
00215 void setup_shield(){
00216
00217
00218 init_leds();
00219 init_relays();
00220 init_buttons();
00221 init_joystick( 5 );
00222
00223
00224 touch_robot.set_CS_AutocaL_Millis(0xFFFFFFFF);
00225
00226 servos[0].attach(SERVO1);
00227 servos[0].write(90);
00228 servos[1].attach(SERVO2);
00229 servos[1].write(90);
00230 servos[2].attach(SERVO3);
00231 servos[2].write(90);
00232
00233 analogWrite(LED1_RED, 255);
00234 analogWrite(LED1_GREEN, 255);
00235 analogWrite(LED1_BLUE, 255);
00236 analogWrite(LED2_RED, 255);
00237 analogWrite(LED2_GREEN, 255);
00238 analogWrite(LED2_BLUE, 255);
00239 analogWrite(LED3_RED, 255);
00240 analogWrite(LED3_GREEN, 255);
00241 analogWrite(LED3_BLUE, 255);
00242 servos[0].write(90);
00243 servos[1].write(90);
00244 servos[2].write(90);
00245 digitalWrite(RELAY1, LOW);
00246 digitalWrite(RELAY2, LOW);
00247
00248 }
00249
00250
00251 #endif