[136] | 1 | #ifndef ArduinoRobot_h
|
---|
| 2 | #define ArduinoRobot_h
|
---|
| 3 |
|
---|
| 4 | #include "Arduino_LCD.h" // Hardware-specific library
|
---|
| 5 | //#include "FormattedText.h"
|
---|
| 6 | #include "SquawkSD.h"
|
---|
| 7 | #include "Multiplexer.h"
|
---|
| 8 | #include "EasyTransfer2.h"
|
---|
| 9 | #include "EEPROM_I2C.h"
|
---|
| 10 | #include "Compass.h"
|
---|
| 11 | #include "Fat16.h"
|
---|
| 12 |
|
---|
| 13 | #if ARDUINO >= 100
|
---|
| 14 | #include "Arduino.h"
|
---|
| 15 | #else
|
---|
| 16 | #include "WProgram.h"
|
---|
| 17 | #endif
|
---|
| 18 |
|
---|
| 19 |
|
---|
| 20 | #define BUTTON_NONE -1
|
---|
| 21 | #define BUTTON_LEFT 0
|
---|
| 22 | #define BUTTON_DOWN 1
|
---|
| 23 | #define BUTTON_UP 2
|
---|
| 24 | #define BUTTON_RIGHT 3
|
---|
| 25 | #define BUTTON_MIDDLE 4
|
---|
| 26 | #define NUMBER_BUTTONS 5
|
---|
| 27 |
|
---|
| 28 | //beep length
|
---|
| 29 | #define BEEP_SIMPLE 0
|
---|
| 30 | #define BEEP_DOUBLE 1
|
---|
| 31 | #define BEEP_LONG 2
|
---|
| 32 |
|
---|
| 33 | // image locations on the EEPROM
|
---|
| 34 | #define HOME_BMP 0
|
---|
| 35 | #define BATTERY_BMP 2048
|
---|
| 36 | #define COMPASS_BMP 4096
|
---|
| 37 | #define CONTROL_BMP 6144
|
---|
| 38 | #define GEARS_BMP 8192
|
---|
| 39 | #define LIGHT_BMP 10240
|
---|
| 40 | #define OSCILLO_BMP 12288
|
---|
| 41 | #define VOLT_BMP 14336
|
---|
| 42 | #define INICIO_BMP 16384 // this is a full screen splash
|
---|
| 43 |
|
---|
| 44 | //Command code
|
---|
| 45 | #define COMMAND_SWITCH_MODE 0
|
---|
| 46 | #define COMMAND_RUN 10
|
---|
| 47 | #define COMMAND_MOTORS_STOP 11
|
---|
| 48 | #define COMMAND_ANALOG_WRITE 20
|
---|
| 49 | #define COMMAND_DIGITAL_WRITE 30
|
---|
| 50 | #define COMMAND_ANALOG_READ 40
|
---|
| 51 | #define COMMAND_ANALOG_READ_RE 41
|
---|
| 52 | #define COMMAND_DIGITAL_READ 50
|
---|
| 53 | #define COMMAND_DIGITAL_READ_RE 51
|
---|
| 54 | #define COMMAND_READ_IR 60
|
---|
| 55 | #define COMMAND_READ_IR_RE 61
|
---|
| 56 | #define COMMAND_ACTION_DONE 70
|
---|
| 57 | #define COMMAND_READ_TRIM 80
|
---|
| 58 | #define COMMAND_READ_TRIM_RE 81
|
---|
| 59 | #define COMMAND_PAUSE_MODE 90
|
---|
| 60 | #define COMMAND_LINE_FOLLOW_CONFIG 100
|
---|
| 61 |
|
---|
| 62 | //component codename
|
---|
| 63 | #define CN_LEFT_MOTOR 0
|
---|
| 64 | #define CN_RIGHT_MOTOR 1
|
---|
| 65 | #define CN_IR 2
|
---|
| 66 |
|
---|
| 67 | //motor board modes
|
---|
| 68 | #define MODE_SIMPLE 0
|
---|
| 69 | #define MODE_LINE_FOLLOW 1
|
---|
| 70 | #define MODE_ADJUST_MOTOR 2
|
---|
| 71 | #define MODE_IR_CONTROL 3
|
---|
| 72 |
|
---|
| 73 | //port types, for R/W
|
---|
| 74 | #define TYPE_TOP_TK 0
|
---|
| 75 | #define TYPE_TOP_TKD 1
|
---|
| 76 | #define TYPE_BOTTOM_TK 2
|
---|
| 77 |
|
---|
| 78 | //top TKs
|
---|
| 79 | #define TK0 100
|
---|
| 80 | #define TK1 101
|
---|
| 81 | #define TK2 102
|
---|
| 82 | #define TK3 103
|
---|
| 83 | #define TK4 104
|
---|
| 84 | #define TK5 105
|
---|
| 85 | #define TK6 106
|
---|
| 86 | #define TK7 107
|
---|
| 87 |
|
---|
| 88 | #define M0 TK0
|
---|
| 89 | #define M1 TK1
|
---|
| 90 | #define M2 TK2
|
---|
| 91 | #define M3 TK3
|
---|
| 92 | #define M4 TK4
|
---|
| 93 | #define M5 TK5
|
---|
| 94 | #define M6 TK6
|
---|
| 95 | #define M7 TK7
|
---|
| 96 |
|
---|
| 97 | //bottom TKs, just for communication purpose
|
---|
| 98 | #define B_TK1 201
|
---|
| 99 | #define B_TK2 202
|
---|
| 100 | #define B_TK3 203
|
---|
| 101 | #define B_TK4 204
|
---|
| 102 |
|
---|
| 103 | #define D10 B_TK1
|
---|
| 104 | #define D9 B_TK2
|
---|
| 105 | #define D8 B_TK4
|
---|
| 106 | #define D7 B_TK3
|
---|
| 107 |
|
---|
| 108 | //bottom IRs, for communication purpose
|
---|
| 109 | #define B_IR0 210
|
---|
| 110 | #define B_IR1 211
|
---|
| 111 | #define B_IR2 212
|
---|
| 112 | #define B_IR3 213
|
---|
| 113 | #define B_IR4 214
|
---|
| 114 |
|
---|
| 115 | #ifndef LED1
|
---|
| 116 | #define LED1 17
|
---|
| 117 | #endif
|
---|
| 118 |
|
---|
| 119 | //320 - 337 username,
|
---|
| 120 | #define ADDRESS_USERNAME 320
|
---|
| 121 | //338 - 355 robotname,
|
---|
| 122 | #define ADDRESS_ROBOTNAME 338
|
---|
| 123 | //356 - 373 cityname,
|
---|
| 124 | #define ADDRESS_CITYNAME 356
|
---|
| 125 | //374- 391 countryname,
|
---|
| 126 | #define ADDRESS_COUNTRYNAME 374
|
---|
| 127 | //508-511 robot info
|
---|
| 128 | #define ADDRESS_ROBOTINFO 508
|
---|
| 129 |
|
---|
| 130 | #define BLACK ILI9163C_BLACK
|
---|
| 131 | #define BLUE ILI9163C_BLUE
|
---|
| 132 | #define RED ILI9163C_RED
|
---|
| 133 | #define GREEN ILI9163C_GREEN
|
---|
| 134 | #define CYAN ILI9163C_CYAN
|
---|
| 135 | #define MAGENTA ILI9163C_MAGENTA
|
---|
| 136 | #define YELLOW ILI9163C_YELLOW
|
---|
| 137 | #define WHITE ILI9163C_WHITE
|
---|
| 138 |
|
---|
| 139 | //A data structure for storing the current state of motor board
|
---|
| 140 | struct MOTOR_BOARD_DATA{
|
---|
| 141 | int _B_TK1;
|
---|
| 142 | int _B_TK2;
|
---|
| 143 | int _B_TK3;
|
---|
| 144 | int _B_TK4;
|
---|
| 145 |
|
---|
| 146 | /*int _B_IR0;
|
---|
| 147 | int _B_IR1;
|
---|
| 148 | int _B_IR2;
|
---|
| 149 | int _B_IR3;
|
---|
| 150 | int _B_IR4;*/
|
---|
| 151 | };
|
---|
| 152 |
|
---|
| 153 | /*
|
---|
| 154 | A message structure will be:
|
---|
| 155 | switch mode:
|
---|
| 156 | byte COMMAND_SWITCH_MODE, byte mode
|
---|
| 157 | run:
|
---|
| 158 | byte COMMAND_RUN, int speedL, int speedR
|
---|
| 159 | analogWrite:
|
---|
| 160 | byte COMMAND_ANALOG_WRITE, byte codename, byte value;
|
---|
| 161 | digitalWrite:
|
---|
| 162 | byte COMMAND_DIGITAL_WRITE, byte codename, byte value;
|
---|
| 163 | analogRead:
|
---|
| 164 | byte COMMAND_ANALOG_READ, byte codename;
|
---|
| 165 | analogRead return:
|
---|
| 166 | byte COMMAND_ANALOG_READ_RE, byte codename, int value;
|
---|
| 167 | digitalRead return:
|
---|
| 168 | byte COMMAND_DIGITAL_READ_RE, byte codename, byte value;
|
---|
| 169 | read IR:
|
---|
| 170 | byte COMMAND_READ_IR, int valueA, int valueB, int valueC, int valueD;
|
---|
| 171 |
|
---|
| 172 |
|
---|
| 173 | */
|
---|
| 174 | #define NUM_EEPROM_BMP 10
|
---|
| 175 | struct EEPROM_BMP{
|
---|
| 176 | char name[8];
|
---|
| 177 | uint8_t width;
|
---|
| 178 | uint8_t height;
|
---|
| 179 | uint16_t address;
|
---|
| 180 | };
|
---|
| 181 |
|
---|
| 182 | //if you call #undef USE_SQUAWK_SYNTH_SD at the beginning of your sketch,
|
---|
| 183 | //it's going to remove anything regarding sound playing
|
---|
| 184 |
|
---|
| 185 | class RobotControl:public Multiplexer,
|
---|
| 186 | public EEPROM_I2C,
|
---|
| 187 | public Compass,
|
---|
| 188 | public SquawkSynthSD,
|
---|
| 189 | //public FormattedText
|
---|
| 190 | public Arduino_LCD
|
---|
| 191 | {
|
---|
| 192 | public:
|
---|
| 193 | RobotControl();
|
---|
| 194 | void begin();
|
---|
| 195 | void setMode(uint8_t mode);
|
---|
| 196 |
|
---|
| 197 | //Read & Write, TK0 - TK7, TKD0 - TKD1, bottom TK0 - TK4
|
---|
| 198 | bool digitalRead(uint8_t port);
|
---|
| 199 | int analogRead(uint8_t port);
|
---|
| 200 | void digitalWrite(uint8_t port, bool value);
|
---|
| 201 | void analogWrite(uint8_t port, uint8_t value);//It's not available, as there's no pin can be used for analog write
|
---|
| 202 |
|
---|
| 203 | //IR sensors from the bottom board
|
---|
| 204 | //define an array as "int arr[4];", and supply the arry name here
|
---|
| 205 | uint16_t IRarray[5];
|
---|
| 206 | void updateIR();
|
---|
| 207 |
|
---|
| 208 | //on board Potentiometor
|
---|
| 209 | int knobRead();
|
---|
| 210 | //Potentiometor of the motor board
|
---|
| 211 | int trimRead();
|
---|
| 212 |
|
---|
| 213 | //on board piezo
|
---|
| 214 | void beginSpeaker(uint16_t frequency=44100);
|
---|
| 215 | void playMelody(char* script);
|
---|
| 216 | void playFile(char* filename);
|
---|
| 217 | void stopPlayFile();
|
---|
| 218 | void beep(int beep_length=BEEP_SIMPLE);
|
---|
| 219 | void tempoWrite(int tempo);
|
---|
| 220 | void tuneWrite(float tune);
|
---|
| 221 |
|
---|
| 222 | //compass
|
---|
| 223 | uint16_t compassRead();
|
---|
| 224 | void drawCompass(uint16_t value);
|
---|
| 225 | void drawBase();
|
---|
| 226 | void drawDire(int16_t dire);
|
---|
| 227 |
|
---|
| 228 | //keyboard
|
---|
| 229 | void keyboardCalibrate(int *vals);
|
---|
| 230 | int8_t keyboardRead();//return the key that is being pressed?Has been pressed(with _processKeyboard)?
|
---|
| 231 |
|
---|
| 232 | //movement
|
---|
| 233 | void moveForward(int speed);
|
---|
| 234 | void moveBackward(int speed);
|
---|
| 235 | void turnLeft(int speed);
|
---|
| 236 | void turnRight(int speed);
|
---|
| 237 | void motorsStop();
|
---|
| 238 | void motorsWritePct(int speedLeftPct, int speedRightPct);
|
---|
| 239 |
|
---|
| 240 | void motorsWrite(int speedLeft,int speedRight);
|
---|
| 241 | void pointTo(int degrees);//turn to an absolute angle from the compass
|
---|
| 242 | void turn(int degress);//turn certain degrees from the current heading
|
---|
| 243 |
|
---|
| 244 | //Line Following
|
---|
| 245 | void lineFollowConfig(uint8_t KP, uint8_t KD, uint8_t robotSpeed, uint8_t intergrationTime);//default 11 5 50 10
|
---|
| 246 |
|
---|
| 247 | //TFT LCD
|
---|
| 248 | //use the same commands as Arduino_LCD
|
---|
| 249 | void beginTFT(uint16_t foreGround=BLACK, uint16_t background=WHITE);
|
---|
| 250 | /*void text(int value, uint8_t posX, uint8_t posY, bool EW);
|
---|
| 251 | void text(long value, uint8_t posX, uint8_t posY, bool EW);
|
---|
| 252 | void text(char* value, uint8_t posX, uint8_t posY, bool EW);
|
---|
| 253 | void text(char value, uint8_t posX, uint8_t posY, bool EW);*/
|
---|
| 254 | void debugPrint(long value, uint8_t x=0, uint8_t y=0);
|
---|
| 255 | void clearScreen();
|
---|
| 256 |
|
---|
| 257 | void drawBMP(char* filename, uint8_t x, uint8_t y);//detect if draw with EEPROM or SD, and draw it
|
---|
| 258 | void _drawBMP(uint32_t iconOffset, uint8_t x, uint8_t y, uint8_t width, uint8_t height);//draw from EEPROM
|
---|
| 259 | void _drawBMP(char* filename, uint8_t x, uint8_t y);//draw from SD
|
---|
| 260 | void beginBMPFromEEPROM();
|
---|
| 261 | void endBMPFromEEPROM();
|
---|
| 262 |
|
---|
| 263 | uint16_t foreGround;//foreground color
|
---|
| 264 | uint16_t backGround;//background color
|
---|
| 265 |
|
---|
| 266 |
|
---|
| 267 | //SD card
|
---|
| 268 | void beginSD();
|
---|
| 269 |
|
---|
| 270 | //Information
|
---|
| 271 | void userNameRead(char* container);
|
---|
| 272 | void robotNameRead(char* container);
|
---|
| 273 | void cityNameRead(char* container);
|
---|
| 274 | void countryNameRead(char* container);
|
---|
| 275 |
|
---|
| 276 | void userNameWrite(char* text);
|
---|
| 277 | void robotNameWrite(char* text);
|
---|
| 278 | void cityNameWrite(char* text);
|
---|
| 279 | void countryNameWrite(char* text);
|
---|
| 280 |
|
---|
| 281 | //Others
|
---|
| 282 | bool isActionDone();
|
---|
| 283 | void pauseMode(uint8_t onOff);
|
---|
| 284 | void displayLogos();
|
---|
| 285 | void waitContinue(uint8_t key=BUTTON_MIDDLE);
|
---|
| 286 |
|
---|
| 287 | private:
|
---|
| 288 | //Read & Write
|
---|
| 289 | uint8_t _getTypeCode(uint8_t port);//different ports need different actions
|
---|
| 290 | uint8_t _portToTopMux(uint8_t port);//get the number for multiplexer within top TKs
|
---|
| 291 | uint8_t _topDPortToAPort(uint8_t port);//get the corrensponding analogIn pin for top TKDs
|
---|
| 292 |
|
---|
| 293 | bool _digitalReadTopMux(uint8_t port);//TK0 - TK7
|
---|
| 294 | int _analogReadTopMux(uint8_t port);
|
---|
| 295 |
|
---|
| 296 | bool _digitalReadTopPin(uint8_t port);
|
---|
| 297 | int _analogReadTopPin(uint8_t port);
|
---|
| 298 | void _digitalWriteTopPin(uint8_t port, bool value);
|
---|
| 299 |
|
---|
| 300 | MOTOR_BOARD_DATA motorBoardData;
|
---|
| 301 | int* parseMBDPort(uint8_t port);
|
---|
| 302 | int get_motorBoardData(uint8_t port);
|
---|
| 303 | void set_motorBoardData(uint8_t port, int value);
|
---|
| 304 |
|
---|
| 305 | bool _requestDigitalRead(uint8_t port);
|
---|
| 306 | int _requestAnalogRead(uint8_t port);
|
---|
| 307 | void _requestDigitalWrite(uint8_t port, uint8_t value);
|
---|
| 308 |
|
---|
| 309 | //LCD
|
---|
| 310 | void _enableLCD();
|
---|
| 311 | void _setWrite(uint8_t posX, uint8_t posY);
|
---|
| 312 | void _setErase(uint8_t posX, uint8_t posY);
|
---|
| 313 |
|
---|
| 314 |
|
---|
| 315 | //SD
|
---|
| 316 | SdCard card;
|
---|
| 317 | Fat16 file;
|
---|
| 318 | Fat16 melody;
|
---|
| 319 | void _enableSD();
|
---|
| 320 |
|
---|
| 321 | //keyboard
|
---|
| 322 | void _processKeyboard(); //need to run in loop, detect if the key is actually pressed
|
---|
| 323 | int averageAnalogInput(int pinNum);
|
---|
| 324 |
|
---|
| 325 | //Ultrasonic ranger
|
---|
| 326 | //uint8_t pinTrigger_UR;
|
---|
| 327 | //uint8_t pinEcho_UR;
|
---|
| 328 |
|
---|
| 329 | //Melody
|
---|
| 330 | void playNote(byte period, word length, char modifier);
|
---|
| 331 |
|
---|
| 332 | //Communication
|
---|
| 333 |
|
---|
| 334 | EasyTransfer2 messageOut;
|
---|
| 335 | EasyTransfer2 messageIn;
|
---|
| 336 |
|
---|
| 337 | //TFT LCD
|
---|
| 338 | bool _isEEPROM_BMP_Allocated;
|
---|
| 339 | EEPROM_BMP * _eeprom_bmp;
|
---|
| 340 | void _drawBMP_EEPROM(uint16_t address, uint8_t width, uint8_t height);
|
---|
| 341 | void _drawBMP_SD(char* filename, uint8_t x, uint8_t y);
|
---|
| 342 |
|
---|
| 343 |
|
---|
| 344 | };
|
---|
| 345 |
|
---|
| 346 | inline void RobotControl::userNameRead(char* container){
|
---|
| 347 | EEPROM_I2C::readBuffer(ADDRESS_USERNAME,(uint8_t*)container,18);
|
---|
| 348 | }
|
---|
| 349 | inline void RobotControl::robotNameRead(char* container){
|
---|
| 350 | EEPROM_I2C::readBuffer(ADDRESS_ROBOTNAME,(uint8_t*)container,18);
|
---|
| 351 | }
|
---|
| 352 | inline void RobotControl::cityNameRead(char* container){
|
---|
| 353 | EEPROM_I2C::readBuffer(ADDRESS_CITYNAME,(uint8_t*)container,18);
|
---|
| 354 | }
|
---|
| 355 | inline void RobotControl::countryNameRead(char* container){
|
---|
| 356 | EEPROM_I2C::readBuffer(ADDRESS_COUNTRYNAME,(uint8_t*)container,18);
|
---|
| 357 | }
|
---|
| 358 |
|
---|
| 359 | inline void RobotControl::userNameWrite(char* text){
|
---|
| 360 | EEPROM_I2C::writePage(ADDRESS_USERNAME,(uint8_t*)text,18);
|
---|
| 361 | }
|
---|
| 362 | inline void RobotControl::robotNameWrite(char* text){
|
---|
| 363 | EEPROM_I2C::writePage(ADDRESS_ROBOTNAME,(uint8_t*)text,18);
|
---|
| 364 | }
|
---|
| 365 | inline void RobotControl::cityNameWrite(char* text){
|
---|
| 366 | EEPROM_I2C::writePage(ADDRESS_CITYNAME,(uint8_t*)text,18);
|
---|
| 367 | }
|
---|
| 368 | inline void RobotControl::countryNameWrite(char* text){
|
---|
| 369 | EEPROM_I2C::writePage(ADDRESS_COUNTRYNAME,(uint8_t*)text,18);
|
---|
| 370 | }
|
---|
| 371 |
|
---|
| 372 | extern RobotControl Robot;
|
---|
| 373 |
|
---|
| 374 | #endif
|
---|