summaryrefslogtreecommitdiff
path: root/Venus_Skeleton/Venus_Skeleton.ino
blob: 6b901c8b01820579ee79e2c948c0eb639515b0e9 (plain)
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// TODO
//
// - limit sendData to x per second
//
// - insert into calibration files
// 		
// 
// - Make sure ENUM piDataType is correct and synced
// - Change interpretData to resemble correct ENUM data
// 

#include <Servo.h>

// **********************
// ** Calibration file **
// **********************

// Include Wally of Eve, (un)comment the right one.
#include "calibration.wall-e.h"
//#include "calibration.eve.h"


// *****************
// ** DEFINITIONS **
// *****************

// Pin configuration
#define PIN_SENS_COMPASS
#define PIN_SENS_OBSTACLE_TURRET 9
#define PIN_SENS_IR_LEFT A1
#define PIN_SENS_IR_RIGHT A0
#define PIN_SENS_SAMPLE_TURRET
#define PIN_SENS_SAMPLE_GRIPPER
#define PIN_SENS_BEACON_TURRET
#define PIN_SERVO_LEFT 12
#define PIN_SERVO_RIGHT = 13;
#define PIN_SERVO_TURRET = 11;

// Servos
Servo servoLeft;	// Declare left servo signal
Servo servoRight;	// Declare right servo signal


// 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_RANDOM, 
	OPMODE_WAIT, 
	OPMODE_INITIALSEQUENCE, 
	OPMODE_RANDOM, 
	OPMODE_MAPPING, 
	OPMODE_GOTOSAMPLE, 
	OPMODE_GRABSAMPLE, 
	OPMODE_GOTOLABLOCATION, 
	OPMODE_WAITFORLAB, 
	OPMODE_FINDMAGNET, 
	OPMODE_LABSEQUENCE
} opmode_t;

// Current operation mode
opmode_t operationMode = OPMODE_WAIT;
unsigned long operationChange = 0;

// Location values
int currValRobotX;
int currValRobotY;

// Sensor values
int currValSensCompass;
int currValSensObstacleTurret;
int currValServoTurret;

// Actuator values
int currValTurret;


// **********************
// ** PI COMMUNICATION **
// **********************

// sendData to Raspberry Pi
void sendData(pi_datatype_t method, int data, int data1 = null, int data2 = 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:
		
    break;
    case PI_DATATYPE_GRIPPER:
    
    break;
    case PI_DATATYPE_TURRET:
    
    break;
    case default:
		// ignore ????
    break;
  }
}


// **********************
// ** SENSOR FUNCTIONS **
// **********************

// sensor Compass, orientation
int sensCompass() {
	// average for last x ????????
  int degree = 359;
  sendData(PI_DATATYPE_COMPASS, degree);
  return degree;
}

// sensor Ultrasound, distance measurement
int sensObstacleTurret() {
  int distance = 100;
  int turretAngle = 90;
  sendData(PI_DATATYPE_OBSTACLETURRET, distance, turretAngle);
  return distance;
}

// sensor IR, line detection
boolean sensIRLeft() {
  int i;
	boolean inaccessible = false;
  i = analogRead(PIN_SENS_IR_LEFT);
  if (i > CAL_IR_LEFT_THRESHOLD)
    inaccessible = true;
	
  // Inaccessible terrain. True for inaccessible, false for accessible
  if(inaccessible) {
    stopMovement();
    sendData(PI_DATATYPE_IRLEFT, currValRobotX, currValRobotY, currValSensCompass);
  }
  return inaccessible;
}
int sensIRRight() {
  int i;
	boolean inaccessible = false;
  i = analogRead(PIN_SENS_IR_RIGHT);
  if (i > CAL_IR_RIGHT_THRESHOLD)
    inaccessible = true;
	
  // Inaccessible terrain. True for inaccessible, false for accessible
  if(inaccessible) {
    stopMovement();
    sendData(PI_DATATYPE_IRRIGHT, currValRobotX, currValRobotY, currValSensCompass);
  }
  return inaccessible;
}

// sensor IR, sample detection
int sensSampleTurret() {
  boolean sample = false;
  if(sample) {
		// If a sample is detected, stop all servos, send data to the PI and wait for response
    stopAllServos();
    sendData(PI_DATATYPE_SAMPLETURRET, currValRobotX, currValRobotY, currValSensCompass, currValTurret);
  }
}
int sensSampleGripper() {
  boolean sample = false;
  if(sample) {
    stopAllServos();
    sendData(PI_DATATYPE_SAMPLEGRIPPER, currValRobotX, currValRobotY, currValSensCompass);
  }
}

// sensor IR, beacon detection and recognition
int sensBeaconTurret() {
  int beacon = 0;
  if(beacon) {
    // Location
  }
}

// sensor IR, lab detection
int sensLab() {
  return 0;
}

// ************************
// ** ACTUATOR FUNCTIONS **
// ************************

void stopMovement() {
	operationMode = OPMODE_WAIT;
	servoLeft.detach();
	servoRight.detach();
}
void stopAllServos() {
	operationMode = OPMODE_WAIT;
	servoLeft.detach();
	servoRight.detach();
}

// ***********************
// ** ARDUINO FUNCTIONS **
// ***********************

void setup() {
  
}

void loop() {
	sensCompass();
	sensObstacleTurret();
	sensIRLeft();
	sensIRRight();
	sensSampleTurret();
	sensSampleGripper();
	sensBeaconTurret();
	sensLab();
	
	switch(operationMode) {
		case OPMODE_WAIT:
		stopMovement();
		break;
		
		default:
		stopMovement();
		break;
	}
	
}

// **********************
// ** HELPER FUNCTIONS **
// **********************

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

















// define types
typedef struct {
	// ir
	bool left_detected;
	bool right_detected; // usually an integer (16-bit)
	int distance; // 16-bit
	// ..
	long timer; // 32-bit
} sensor_data_t;

enum {
	CHANGED_IR_LEFT = 1 << 0,
	CHANGED_IR_RIGHT = 1 << 1,
	CHNAGED_JDHCHDUCDH = 1 << 2,
		CHANGED_IDJDHD = 1 << 3
};

sensor_data_t data;
int changedBits = 0;

// 1. gather sensor data -< sets changedBits and data
val = readFromSensor();
if (data.left_detected != val) {
	data.left_detected = val;
	// mark as changed
	changedBits |= CHANGED_IR_LEFT;
}
// other sensors: same

// 2. read whetehr you have hit a block / line sensor
if ((changedBits & (CHANGED_IR_LEFT | CHANGED_IR_RIGHT))) &&
	(data.left_detected || data.right_detected)) {
		// panic!
		stopMotor();
}

// 3. send to pi or other calculations (and clear bits)
sendData(PIN_IR_LEFT, data.left_detected);
// clear bits
changedBits &= ~CHANGED_IR_LEFT;