diff options
author | Peter de Kok <p.j.s.d.kok@gmail.com> | 2015-05-27 12:05:16 +0200 |
---|---|---|
committer | Peter de Kok <p.j.s.d.kok@gmail.com> | 2015-05-27 12:05:16 +0200 |
commit | 16d6305f3246dfbd95233cb19f5ff0d13a79f91e (patch) | |
tree | 0d0f6b76522f2fc6d98393af7977d5b42681df9a | |
parent | ad6d3568fb3dbfa3f41ec2f72e065a329fa7e1f6 (diff) | |
download | code-16d6305f3246dfbd95233cb19f5ff0d13a79f91e.tar.gz |
Changed some structure and fixed some compile errors
Some C++ structures don't work for Arduino, unless you put them in a header file.
-rw-r--r-- | Venus_Skeleton/Venus_Skeleton.ino | 164 | ||||
-rw-r--r-- | Venus_Skeleton/calibration_wall-e.h (renamed from Venus_Skeleton/calibration.wall-e.h) | 0 | ||||
-rw-r--r-- | Venus_Skeleton/dataTypes.h | 60 |
3 files changed, 132 insertions, 92 deletions
diff --git a/Venus_Skeleton/Venus_Skeleton.ino b/Venus_Skeleton/Venus_Skeleton.ino index 9b4dd54..9b18248 100644 --- a/Venus_Skeleton/Venus_Skeleton.ino +++ b/Venus_Skeleton/Venus_Skeleton.ino @@ -17,13 +17,14 @@ // 3 Error: Sensor IR Line detection #include <Servo.h> +#include "dataTypes.h" // ********************** // ** Calibration file ** // ********************** // Include Wally of Eve, (un)comment the right one. -#include "calibration.wall-e.h" +#include "calibration_wall-e.h" //#include "calibration.eve.h" @@ -32,7 +33,6 @@ // ***************** // Pin configuration -# #define PIN_SENS_COMPASS #define PIN_SENS_OBSTACLE_TURRET 9 #define PIN_SENS_IR_LEFT A1 @@ -48,61 +48,10 @@ Servo servoLeft; // Declare left servo signal Servo servoRight; // Declare right servo signal -// Types - -// Sensor data -typedef struct { - // ir - bool IR_left_detected; - bool IR_right_detected; // usually an integer (16-bit) - bool sample_turret_detected; - bool sample_gripper_detected; - int distance; // 16-bit - // .. - long timer; // 32-bit -} sensor_data_t; - -enum { - CHANGED_SENS_COMPASS = 1 << 0, - CHANGED_SENS_OBSTACLETURRET = 1 << 1, - CHANGED_SENS_IRLEFT = 1 << 2, - CHANGED_SENS_IRRIGHT = 1 << 3, - CHANGED_SENS_SAMPLETURRET = 1 << 4, - CHANGED_SENS_SAMPLEGRIPPER = 1 << 5 -}; - +// Raspberry PI data variables sensor_data_t data; int dataToPiChangedBits = 0; - -// Raspberry Pi Data type declarations -typedef enum { - PI_DATATYPE_COMPASS, - PI_DATATYPE_OBSTACLETURRET, - PI_DATATYPE_IRLEFT, - PI_DATATYPE_IRRIGHT, - PI_DATATYPE_SAMPLETURRET, - PI_DATATYPE_SAMPLEGRIPPER, - PI_DATATYPE_BEACONTURRET, - PI_DATATYPE_LAB, - PI_DATATYPE_MOVETO, - PI_DATATYPE_GRIPPER, - PI_DATATYPE_TURRET -} pi_datatype_t; -// Operation modes -typedef enum { - OPMODE_WAIT, - OPMODE_INITIALSEQUENCE, - OPMODE_RANDOM, - OPMODE_MAPPING, - OPMODE_CHECKSAMPLE, - OPMODE_GRABSAMPLE, - OPMODE_GOTOLABLOCATION, - OPMODE_WAITFORLAB, - OPMODE_FINDMAGNET, - OPMODE_LABSEQUENCE -} opmode_t; - // Current operation mode opmode_t operationMode = OPMODE_WAIT; unsigned long operationChange = 0; @@ -135,22 +84,10 @@ int counterMovement = 0; // ********************** // sendData to Raspberry Pi -void sendData(pi_datatype_t method, int data, int data1 = null, int data2 = null) { +void sendData(pi_datatype_t method, int data, int data1 = NULL, int data2 = NULL, int data3 = NULL) { } -// receive data from Raspberry Pi -// sequence = ... ????? -void readData() { - - // if data received -> interpretate it - // change operation mode + update timer - operationMode = OPMODE_WAIT; - operationChange = millis(); - // for example: - interpretData(PI_DATATYPE_MOVETO, 0); -} - void interpretData(pi_datatype_t dataType, int message) { switch(dataType) { case PI_DATATYPE_MOVETO: @@ -168,6 +105,18 @@ void interpretData(pi_datatype_t dataType, int message) { } } +// receive data from Raspberry Pi +// sequence = ... ????? +void readData() { + + // if data received -> interpretate it + // change operation mode + update timer + operationMode = OPMODE_WAIT; + operationChange = millis(); + // for example: + interpretData(PI_DATATYPE_MOVETO, 0); +} + // ********************** // ** SENSOR FUNCTIONS ** @@ -190,11 +139,11 @@ int sensObstacleTurret() { } // sensor IR, line detection -bool sensIRLine(char LorR) { +bool sensIRLine(lr_t LorR) { int i; bool inaccessible = false; - if (LorR == 'L') { + if (LorR == L) { i = analogRead(PIN_SENS_IR_LEFT); if (i > CAL_IR_LEFT_THRESHOLD) inaccessible = true; @@ -208,7 +157,7 @@ bool sensIRLine(char LorR) { return inaccessible; } - if (LorR == 'R') { + if (LorR == R) { i = analogRead(PIN_SENS_IR_RIGHT); if (i > CAL_IR_RIGHT_THRESHOLD) inaccessible = true; @@ -306,7 +255,7 @@ bool moveTurnTo(int direction) { } } -void moveStraight(int direction) { +void moveStraight(int direction, int distance) { if (direction != 0 && direction != NUM_DIRECTIONS) { return; } @@ -324,7 +273,7 @@ void opMode(opmode_t opmode) { } void checkFreePath() { - if ((dataToPiChangedBits & (CHANGED_SENS_IRLEFT | CHANGED_SENS_IRRIGHT))) && (data.IR_left_detected || data.IR_right_detected)) { + if ((dataToPiChangedBits & (CHANGED_SENS_IRLEFT | CHANGED_SENS_IRRIGHT)) && (data.IR_left_detected || data.IR_right_detected)) { // Left or Right IR sensor sees inaccessible terrain opMode(OPMODE_WAIT); stopMovement(); @@ -335,8 +284,6 @@ void checkFreePath() { } int checkBestRoute() { - // IMPLEMENT SOME TIMER HERE... - // DON'T RUN EVERY TIME, TOO MUCH JITTER THEN // possibly use time instead of boolean values, to remember them, default value = default time, possible to let pi change this // possibly change directionArray to global variable if (counterMovement) { @@ -372,7 +319,7 @@ int eliminateDirections(int directionArray[]) { for (int i = 0; i < NUM_DIRECTIONS * 2; i++) { // LINE PANIC! If a line is detected on the left, only a right turn is allowed between 90deg and 180deg - if (data.IR_left_detected && ( (i < ((float)NUM_DIRECTIONS / 4.0) + NUM_DIRECTIONS) || (i > ((float)NUM_DIRECTIONS / 4.0) * 2.0 + NUM_DIRECTIONS)) )) { + if (data.IR_left_detected && ((i < ((float)NUM_DIRECTIONS / 4.0) + NUM_DIRECTIONS) || (i > ((float)NUM_DIRECTIONS / 4.0) * 2.0 + NUM_DIRECTIONS)) ) { directionArray[i] = 0; } // LINE PANIC! If a line is detected on the right, only a left turn is allowed between 90deg and 180deg @@ -400,7 +347,7 @@ int eliminateDirections(int directionArray[]) { } void checkSample() { - if ((dataToPiChangedBits & (CHANGED_SENS_SAMPLETURRET | CHANGED_SENS_SAMPLEGRIPPER))) && (data.sample_turret_detected || data.sample_gripper_detected)) { + if ((dataToPiChangedBits & (CHANGED_SENS_SAMPLETURRET | CHANGED_SENS_SAMPLEGRIPPER)) && (data.sample_turret_detected || data.sample_gripper_detected)) { // One of the turret sees a sample (or the lab) opMode(OPMODE_CHECKSAMPLE); stopAllServos(); @@ -433,8 +380,8 @@ void setup() { void loop() { sensCompass(); sensObstacleTurret(); - sensIRLine('L'); - sensIRLine('R'); + sensIRLine(L); + sensIRLine(R); sensSampleTurret(); sensSampleGripper(); sensBeaconTurret(); @@ -445,7 +392,7 @@ void loop() { // calculateLocation(); - sendData(); + sendData(PI_DATATYPE_ALL, 'lala', 'lala'); dataToPiChangedBits = 0; // Clear changed bits readData(); @@ -463,26 +410,45 @@ void loop() { switch(operationMode) { case OPMODE_WAIT: + counterMovement = 0; + timerMovement = 0; + break; + case OPMODE_INITIALSEQUENCE: + + break; + case OPMODE_RANDOM: break; - // ... case OPMODE_MAPPING: checkBestRoute(); moveTurnTo(currValDirection); - moveStraight(currValDirection, SOMEDISTANCE!!!!!!!!!!!!!); + moveStraight(currValDirection, 1); // 1 is supposed to be some distance / time break; case OPMODE_CHECKSAMPLE: break; + case OPMODE_GRABSAMPLE: + + break; + case OPMODE_GOTOLABLOCATION: + + break; + case OPMODE_WAITFORLAB: + + break; + case OPMODE_FINDMAGNET: + + break; + case OPMODE_LABSEQUENCE: + + break; + default: + // ?? + break; } - // Operations (probably in a switch or something, according to operation modes) - // Opmode WAIT: set counterMovement to zero - // set timerMovement to zero - checkBestRoute(); - moveTurnTo(currValDirection); - moveStraight(currValDirection, SOMEDISTANCE!!!!!!!!!!!!!); - + // Delay for stability. + delay(20); } // ********************** @@ -490,10 +456,24 @@ void loop() { // ********************** // error helper -error(int errorCode) { - opMode(OPMODE_WAIT); +void error(int errorCode) { + opMode(OPMODE_ERROR); stopAllServos(); - //errorSequence(); + errorSequence(errorCode); +} +void errorSequence(int errorCode) { + while(1) { + // - move turret to middle (fast) + // - move turret to left (slow) + // - move turret to middle (slow) + delay(1000); + for(int i = 0; i < errorCode; i++) { + // - move turret to right (fast) + // - move turret to middle (fast) + delay(500); + } + delay(3000); + } } long microsecondsToInches(long microseconds) @@ -570,4 +550,4 @@ if ((changedBits & (CHANGED_IR_LEFT | CHANGED_IR_RIGHT))) && // 3. send to pi or other calculations (and clear bits) sendData(PIN_IR_LEFT, data.left_detected); // clear bits -changedBits &= ~CHANGED_IR_LEFT;
\ No newline at end of file +changedBits &= ~CHANGED_IR_LEFT;dataTypes.h
\ No newline at end of file diff --git a/Venus_Skeleton/calibration.wall-e.h b/Venus_Skeleton/calibration_wall-e.h index a7adf7a..a7adf7a 100644 --- a/Venus_Skeleton/calibration.wall-e.h +++ b/Venus_Skeleton/calibration_wall-e.h diff --git a/Venus_Skeleton/dataTypes.h b/Venus_Skeleton/dataTypes.h new file mode 100644 index 0000000..374f23b --- /dev/null +++ b/Venus_Skeleton/dataTypes.h @@ -0,0 +1,60 @@ +// **************** +// ** Data types ** +// **************** + +// Left and Right +typedef enum { + L, + R +} lr_t; + +// Sensor data +typedef struct { + // ir + bool IR_left_detected; + bool IR_right_detected; // usually an integer (16-bit) + bool sample_turret_detected; + bool sample_gripper_detected; + int distance; // 16-bit + // .. + long timer; // 32-bit +} sensor_data_t; +enum { + CHANGED_SENS_COMPASS = 1 << 0, + CHANGED_SENS_OBSTACLETURRET = 1 << 1, + CHANGED_SENS_IRLEFT = 1 << 2, + CHANGED_SENS_IRRIGHT = 1 << 3, + CHANGED_SENS_SAMPLETURRET = 1 << 4, + CHANGED_SENS_SAMPLEGRIPPER = 1 << 5 +}; + +// Raspberry Pi Data type declarations +typedef enum { + PI_DATATYPE_COMPASS, + PI_DATATYPE_OBSTACLETURRET, + PI_DATATYPE_IRLEFT, + PI_DATATYPE_IRRIGHT, + PI_DATATYPE_SAMPLETURRET, + PI_DATATYPE_SAMPLEGRIPPER, + PI_DATATYPE_BEACONTURRET, + PI_DATATYPE_LAB, + PI_DATATYPE_MOVETO, + PI_DATATYPE_GRIPPER, + PI_DATATYPE_TURRET, + PI_DATATYPE_ALL +} pi_datatype_t; + +// Operation modes +typedef enum { + OPMODE_WAIT, + OPMODE_INITIALSEQUENCE, + OPMODE_RANDOM, + OPMODE_MAPPING, + OPMODE_CHECKSAMPLE, + OPMODE_GRABSAMPLE, + OPMODE_GOTOLABLOCATION, + OPMODE_WAITFORLAB, + OPMODE_FINDMAGNET, + OPMODE_LABSEQUENCE, + OPMODE_ERROR +} opmode_t; |