Roomba Tank
CSC 460 Project 3
base.c
Go to the documentation of this file.
1 #define F_CPU 16000000
2 
3 #include <avr/io.h>
4 #include <avr/interrupt.h>
5 #include <util/delay.h>
6 #include "../roomba/roomba.h"
7 #include "../rtos/os.h"
8 #include "../uart/uart.h"
9 
10 unsigned int IdlePID;
11 
15 
16 int x, y = 375;
17 int rx, ry;
19 
20 uint8_t LASER = 0;
21 uint8_t SERVO = 1;
22 uint8_t LS = 2;
23 uint8_t SCREEN = 3;
24 uint8_t ROOMBA = 4;
25 uint8_t MODE = 5;
26 
27 int servoState = 375;
28 
29 uint8_t laser = 0;
30 uint8_t previousLaser = 0;
31 
32 uint8_t mode = 0;
33 uint8_t previousMode = 0;
34 
35 #define MAX 10
36 
37 int lSQueue[MAX];
38 int lSFront = 0;
39 int lSRear = 0;
40 
44 int buffer_isFull(int *front, int *rear) {
45  return *rear == (*front - 1) & MAX;
46 }
47 
51 int buffer_isEmpty(int *front, int *rear) {
52  return *rear == *front;
53 }
54 
58 void buffer_enqueue(int val, int *queue, int *front, int *rear){
59  if(buffer_isFull(front, rear)) {
60  return;
61  }
62 
63  queue[*rear] = val;
64  *rear = (*rear + 1) % MAX;
65 }
66 
70 int buffer_dequeue(int *queue, int *front, int *rear) {
71  if(buffer_isEmpty(front, rear)) {
72  return -1;
73  }
74 
75  int result = queue[*front];
76 
77  *front = (*front + 1) % MAX;
78 
79  return result;
80 }
81 
85 void Idle() {
86  for(;;) {
87  }
88 }
89 
93 void InitADC() {
94  ADMUX |= (1<<REFS0);
95  ADCSRA|=(1<<ADEN)|(1<<ADPS0)|(1<<ADPS1)|(1<<ADPS2); //ENABLE ADC, PRESCALER 128
96 }
97 
101 void JoystickTask() {
102  for(;;) {
103 
105 
106  // Read x
107  ADMUX = (ADMUX & 0xE0); // Channel 8
108 
109  ADCSRB |= (1<<MUX5);
110 
111  ADCSRA |= (1<<ADSC); // Start conversion
112 
113  while((ADCSRA)&(1<<ADSC)); // wait until conversion is complete
114 
115  x = ADC;
116 
117  x = (0.458*x) + 140;
118 
120 
122 
123  if ((x - servoState > 0 && x >= 380) || (x < 370 && x - servoState < 0)) {
124 
125  uint8_t x1 = x>>8;
126  uint8_t x2 = x;
127 
131 
132  servoState = x;
133  }
134  else if ((x <= 380) && (x >= 370)) {
135  x = 375;
136  servoState = 375;
137 
138  uint8_t x1 = x>>8;
139  uint8_t x2 = x;
140 
144  }
145 
147 
148  Task_Sleep(20);
149  }
150 }
151 
155 void SwitchTask() {
156  int mode = 0;
157  DDRL |= (0<<DDL1);
158  PORTL |= (1<<PORTL1);
159  for(;;) {
160  // Read button
161  mode = (PINL & _BV(PL1)) ? 0 : 1;
162 
163  if(mode != previousMode) {
165 
167 
169 
171  }
172 
173  // Sleep 100 ms
174  Task_Sleep(20);
175  }
176 }
177 
181 void LaserTask() {
182  DDRB |= (0<<DDB1);
183  PORTB |= (1<<PORTB1);
184  for(;;) {
185  // Read laser
186  laser = (PINB & _BV(PB1)) ? 0 : 1;
187 
188  if (laser != previousLaser) {
190 
193 
195 
197  }
198 
199  // Sleep 100 ms
200  Task_Sleep(10);
201  }
202 }
203 
208 
209  for(;;) {
210  uint8_t flag;
211  uint16_t ls_data;
212  uint8_t ls_data1;
213  uint8_t ls_data2;
214  if(( UCSR1A & (1<<RXC1))) {
215  flag = Bluetooth_Receive_Byte();
216 
217  if (flag == LS){
218  ls_data1 = Bluetooth_Receive_Byte();
219  ls_data2 = Bluetooth_Receive_Byte();
220  ls_data = (ls_data1<<8) | (ls_data2);
221 
223  buffer_enqueue(ls_data, lSQueue, &lSFront, &lSRear);
225  }
226  // else if(flag == SERVO) {
227 
228  // }
229  }
230 
231  Task_Sleep(15);
232  }
233 }
234 
235 void screenTask() {
236  for(;;) {
238  uint16_t lSState = buffer_dequeue(lSQueue, &lSFront, &lSRear);
240 
241  Task_Sleep(15);
242  }
243 }
244 
248 void RoombaTask() {
249  char command = 'B';
250 
251  for(;;) {
253  // Read rx
254  ADMUX = (ADMUX & 0xE0); // Channel 8
255 
256  ADCSRB |= (1<<MUX5);
257 
258  ADCSRA |= (1<<ADSC); // Start conversion
259 
260  while((ADCSRA)&(1<<ADSC)); // wait until conversion is complete
261 
262  rx = ADC;
263 
264  // Read ry
265  ADMUX = (ADMUX & 0xE0); // Channel 9
266 
267  ADMUX |= (1<<MUX0);
268 
269  ADCSRB |= (1<<MUX5);
270 
271  ADCSRA |= (1<<ADSC); // Start conversion
272 
273  while((ADCSRA)&(1<<ADSC)); // wait until conversion is complete
274 
275  ry = ADC;
276 
278 
279  if ((ry > 750) && (rx > 250) && (rx < 750)) {
280  command = 'B';
281  }
282  else if ((ry < 250) && (rx > 250) && (rx < 750)) {
283  command = 'G';
284  }
285  else if ((rx > 750) && (ry > 250) && (ry < 750)) {
286  command = 'D';
287  }
288  else if ((rx < 250) && (ry > 250) && (ry < 750)) {
289  command = 'E';
290  }
291  else if ((ry > 750) && (rx > 750)) {
292  command = 'A';
293  }
294  else if ((ry > 750) && (rx < 250)) {
295  command = 'C';
296  }
297  else if ((ry < 250) && (rx > 750)) {
298  command = 'F';
299  }
300  else if ((ry < 250) && (rx < 250)) {
301  command = 'H';
302  }
303  else {
304  command = 'X';
305  }
306 
308  Bluetooth_Send_Byte(command);
309 
310  Task_Sleep(20);
311  }
312 }
313 
317 void a_main() {
318  DDRL |= _BV(DDL6);
319 
321  ls_mutex = Mutex_Init();
322  adc_mutex = Mutex_Init();
323 
325 
326  InitADC();
327 
328 
329  // Task_Create(ServoTask, 2, 1);
330  Task_Create(screenTask, 2, 1);
331  Task_Create(LaserTask, 2, 1);
332  Task_Create(RoombaTask, 2, 1);
335  Task_Create(SwitchTask, 2, 2);
336 
337  Task_Terminate();
338 }
#define MINPRIORITY
Definition: os.h:9
#define MAX
Definition: base.c:35
uint8_t LASER
Definition: base.c:20
int x
Definition: base.c:16
uint8_t mode
Definition: base.c:32
void InitADC()
Definition: base.c:93
uint8_t SCREEN
Definition: base.c:23
uint16_t photocellReading
Definition: base.c:18
MUTEX Mutex_Init()
Definition: os.c:674
int buffer_isFull(int *front, int *rear)
Definition: base.c:44
void Bluetooth_Send_Byte(uint8_t data_out)
Definition: uart.c:73
int rx
Definition: base.c:17
void screenTask()
Definition: base.c:235
uint8_t LS
Definition: base.c:22
int y
Definition: base.c:16
uint8_t previousMode
Definition: base.c:33
uint8_t previousLaser
Definition: base.c:30
int lSRear
Definition: base.c:39
uint8_t ROOMBA
Definition: base.c:24
void SwitchTask()
Definition: base.c:155
uint8_t SERVO
Definition: base.c:21
void JoystickTask()
Definition: base.c:101
void Task_Terminate(void)
Definition: os.c:817
MUTEX ls_mutex
Definition: base.c:13
unsigned char Bluetooth_Receive_Byte()
Definition: uart.c:83
unsigned int MUTEX
Definition: os.h:22
void Task_Sleep(TICK t)
Definition: os.c:779
void Idle()
Definition: base.c:85
int servoState
Definition: base.c:27
int buffer_isEmpty(int *front, int *rear)
Definition: base.c:51
int lSFront
Definition: base.c:38
PID Task_Create(voidfuncptr f, PRIORITY py, int arg)
Definition: os.c:747
void RoombaTask()
Definition: base.c:248
MUTEX adc_mutex
Definition: base.c:14
uint8_t MODE
Definition: base.c:25
uint8_t laser
Definition: base.c:29
void Bluetooth_UART_Init()
Definition: uart.c:56
int ry
Definition: base.c:17
int buffer_dequeue(int *queue, int *front, int *rear)
Definition: base.c:70
void bluetoothReceive()
Definition: base.c:207
void LaserTask()
Definition: base.c:181
unsigned int IdlePID
Definition: base.c:10
void Mutex_Lock(MUTEX m)
Definition: os.c:686
void Mutex_Unlock(MUTEX m)
Definition: os.c:699
MUTEX bluetooth_mutex
Definition: base.c:12
void buffer_enqueue(int val, int *queue, int *front, int *rear)
Definition: base.c:58
int lSQueue[MAX]
Definition: base.c:37
void a_main()
Definition: base.c:317