-
Notifications
You must be signed in to change notification settings - Fork 2
/
Decabot.h
executable file
·294 lines (286 loc) · 8.52 KB
/
Decabot.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
Decabot.h
Daniel Chagas
*/
// ensure this library description is only included once
#ifndef DECABOT_H
#define DECABOT_H
#include <Arduino.h>
#include <EEPROM.h>
//#include <ArduinoUniqueID.h>
#define code_square "[square]X4FLYO"
#define code_scan "[scan]L45u5Ru2L45O"
//ledDisplay const
// the MAX7219 address map (datasheet table 2)
#define MAX7219_DECODE_REG (0x09)
#define MAX7219_INTENSITY_REG (0x09)
#define MAX7219_SCANLIMIT_REG (0x0B)
#define MAX7219_SHUTDOWN_REG (0X0C)
#define MAX7219_DISPLAYTEST_REG (0x0F)
#define MAX7219_DIGIT_REG(pos) ((pos) + 1)
#define MAX7219_COLUMN_REG(pos) MAX7219_DIGIT_REG(pos)
// shutdown mode (datasheet table 3)
#define MAX7219_OFF (0x0)
#define MAX7219_ON (0x1)
// number of columns of the display matrx
#define NUM_OF_COLUMNS (8)
// for each character bitmap, it consumes 4 bytes
#define BYTE_PER_MAP (4)
// library interface description
class Decabot
{
public:
//constructor
Decabot(int delay, byte configuration);
//~Decabot();
//basics
void boot();
void inputSerial(char dataSerial);
void runCodeOnSerial();
void whoAmI();
String sayMyName();
void yourNameIs(String parameter);
void yourOwnerIs(String parameter);
void outputln(String message);
void output(String message);
void dumpMemory();
void erase(int blockMemory);
void formatROM();
void primeDirectives();
void setMaxBattery(int value);
void setMinBattery(int value);
//sounds
void beep(int time);
void soundBegin();
void soundBoot();
void soundEnd();
void soundError();
void soundRecording();
//basic movements
void resetMotors();
void updateMotors(byte data);
void updateMotors();
void oneStepLeft(int dir);
void oneStepRight(int dir);
void move(int distance, int leftDirection, int RightDirection);
void forward(int centimeters);
void left(int degrees);
void right(int degrees);
void servo(int degrees);
void invertLeftMotor();
void invertRightMotor();
//buttons
int readButton();
void executeButton(int button);
void loadLedsCheck();
//Code Domino
void update();
bool isCodeDominoChar(char command);
void codeDomino(char code[]);
void run(bool soundsOn);
void run(int blockMemory, bool soundsOn);
void runCodeDominoSetup();
//void checkCodeDominoLoops();
void nextCommand();
char infiniteCode(int index);
void codeInterpreter(char command, int parameter);
void codeForward(int distance);
void codeForward(int distance,int speed);
void codeLeft(int degrees);
void codeRight(int degrees);
void codeSpeed(int speed);
void codeGoTo(int piece);
void codeRepeat(int times);
void codeStopRepeat();
void codeRunBlockMem(int block);
void codeWait(int timeWait);
void codeMusic(int toneFreq);
void setRadious(float radious);
void codeCurveLeft(int);
void setPosition(float x, float y);
void setHeading(float degrees);
void incrementXYPosition(float distanceForward);
void showPosition();
void adjustHeading();
void codeEnd();
void unknowCode();
void emergencyStop();
void abort();
void saveCodeROM(int memoryBlock);
String programName(int memoryPosition);
//RFID functions
void rfidCodeRecord();
void rfidCodeRecord(int blockMemory);
void injectRFID(String rfidData);
void injectRFIDposition(String rfidData);
//ultrasonic functions
double measureDistance();
void objectDetection(bool forced);
void codeScanObjectPrecision(int range);
//ledMatrix display functions
void ledMatrixInit();
void ledMatrixSetRegister(byte address, byte value);
void ledMatrixClear();
void ledMatrixRandom();
void ledMatrixBattery(int value);
void ledFaceClearMem();
void ledFaceEyes(int position, bool verbose);
void ledFaceEyebrows(int closed, int angry, bool verbose);
void ledFaceMouth(int index, bool verbose);
void printFace(bool verbose);
void faceError();
void faceReadingRfid();
void faceBlink();
private:
byte decabotConfiguration = B00000000;
int decabotMaxBattery = 5000; //maximum battery value
int decabotMinBattery = 4000; //minimum battery value
/*defines wich configuration the robot is set
0- Ultrasonic on 4,5
1- RFID on 9-13
2- Servo on A2
3- Led Matrix on 4,5,A2
4- Gyroscope on i2c
5- Laser distance sensor on i2c
6- Step motor drives on 7 to 13
7- PIR sensor on A2
*/
const int sck = 13; //SPI
const int miso = 12; //SPI
const int mosi = 11; //SPI
const int sda = 10; //SS_SDA pin MFRC522
const int reset = 9; //RST pin MFRC522
const int latchPin = 8; //Pin connected to ST_CP of 74HC595
const int clockPin = 7; //Pin connected to SH_CP of 74HC595
const int dataPin = 6; //Pin connected to DS of 74HC595
const int trigPin = 5; //Pin connected to HC-SR04 ultrasonic
const int echoPin = 4; //Pin connected to HC-SR04 ultrasonic
const int buzzerPin = 3; //Pin connected to buzzer
const int ledPin = 2; //Pin connected to frontal LED
const int servoPin = 16; //Pin connected to servo
const int ledLatchPin = 16; //Pin connected to Max7219 module CS
const int ledClockPin = 4; //Pin connected to Max7219 module CLK
const int ledDataPin = 5; //Pin connected to Max7219 module DIN
bool ledPinState = 0;
String decabotName = "A01 ";
String decabotOwner = "[email protected]";
String tmpOutput = "";
String inputSerialString;
//RFID variables
String inputRfidString;
/*
byte rfidBuffer[18]; //data transfer buffer (16+2 bytes data+CRC)
byte size = sizeof(rfidBuffer);
uint8_t pageAddr = 0x06; //rfid address to begin reading, 0x06 to Mifare Ultra
*/
//movements variables
int millisDelay = 4;
int leftStep = 0;
byte leftBin = B00000000;
int rightStep = 0;
byte rightBin = B00000000;
int leftSpeed = 5;
int rightSpeed = 5;
bool leftDirection = 1;
bool rightDirection = 1;
bool turningLeft = 0;
float curveRadious = 5;
bool leftMotorInverted = 0;
bool rightMotorInverted = 0;
int servoPosition = 90; //variable who have the last servo position, for performance purposes
//self position variables
float xPos = 0;
float yPos = 0;
float heading = 0;
//Code Domino variables
bool moving = 0;
int stepsToMove = 0;
bool recording = 0;
bool executing = 0;
int runningCodeIndex = 0;
char runningCode[128];
long actualMillis = 0;
long lastLeftMillis = 0;
long lastRightMillis = 0;
long codeMillisBegin = 0;
//Code Domino User variables
int varX = 10;
int varY = 90;
int varZ = 4;
float varN = 0;
float varP = 0;
float varQ = 0;
//Code Domino Loop variables
long varI = 5;
long lastMillisLoopOne = 0;
long varJ = 20;
long lastMillisLoopTwo = 0;
long varK = 60;
long lastMillisLoopThree = 0;
//Face variables
int varA = 2; //angry face
int varB = 0; //mouth
int varC = 0; //close eyebrows
int varE = 5; //eyeposition
//runtime repeat variables
int repeatCalls = 0;
int repeatPointers[64];
int repeatCounters[64];
//button variables
long timerButton = 0;
bool lastButtonState = 0;
int lastButtonValue = 0;
//Ultrasonic measure variables
uint32_t ultrasonicTimer;
double lastDetection = 0;
int objectDetectionDelay = 500;
long objectDetectionMillis = 0;
int rotatingTotal = 0;
//private moving functions
void updateSteps();
//private functions
//void debug();
//private math
int poten(int base, int expoent);
float radian(float degree);
long readVcc(byte samples);
int measureBattery();
//ledMatrix variables
byte ledFaceMem[8] = {B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000}; //video ram memory for 8x8 display
bool faceChanged = 0;
const byte mouth[9][3] = {
{B00000000,B00100100,B00011000}, //small smile
{B01000010,B01100110,B00111100}, //smile
{B01111110,B00100100,B00011000}, //open smile
{B00000000,B00011000,B00011000}, //shut
{B00011000,B00100100,B00011000}, //oh
{B00111100,B01000010,B00111100}, //ah
{B00000011,B00001110,B00111000}, //side mouth
{B00000000,B01111110,B00000000}, //neutral
{B00111100,B01100110,B01000010} //sad
};
const byte eye[3][2] = {
{B11001100,B11001100}, //right
{B01100110,B01100110},
{B00110011,B00110011}, //left
};
const byte battery[3] = {B00011000,B00111100,B00100100};
const byte eyeBrow[3][3] = {
{B10000001,B01000010,B00100100}, //angry
{B00000000,B11100111,B00000000}, //normal
{B00100100,B01000010,B10000001}, //sad
};
const byte eyeClear[3][3] = {
{B00000000,B10000001,B11000011}, //angry
{B00000000,B00000000,B11111111}, //normal
{B00000000,B00111100,B01111110}, //sad
};
const int decabotMusic[4][2]{
{494,2},
{554,2},
{440,2},
{880,1}
};
bool runningSounds = 1;
};
#endif