1 /*******************************************************************/
 2 /*
 3 	hsv_receiver.c
 4 
 5 	Handle high level communication between HSV and LUC
 6 
 7 	Richard Harris + Abigail Soong
 8 */
 9 /*******************************************************************/
10 
11 // includes
12 #include "hsv_receiver.h"
13 #include "uart.h"
14 #include "pic_timer.h"
15 #include <htc.h>
16 #include "pic_timer.h"
17 
18 /*******************************************************************/
19 
20 
21 /*******************************************************************/
22 
23 // definitions
24 #define CONTROL_TIMEOUT					1000	// ms
25 #define MAX_PACKET_LENGTH				20
26 #define MAX_LENGTH_ACK_PACKET			11		// length of acknowledge packet
27 #define API_INDICATOR_BYTE_NUM			3
28 #define MESSAGE_INDICATOR_BYTE_NUM		8
29 #define ADDRESS_MSB						4
30 #define ADDRESS_LSB						5
31 
32 typedef enum HSVState
33 {
34     WAITING_FOR_CONTROLLER,
35     WAITING_FOR_COMMAND
36 } HSVState_t;
37 
38 typedef enum PacketState
39 {
40     PACKET_EMPTY,
41     PACKET_IN_PROGRESS,
42     PACKET_FULL
43 } PacketState_t;
44 
45 typedef enum PacketType
46 {
47     CONTROL_REQUEST,
48     CONTROL_MESSAGE,
49     NECROMANCER_MESSAGE,
50     STATUS_MESSAGE,
51     BALLOON_MESSAGE,
52     OTHER
53 } PacketType_t;
54 
55 typedef enum ResponseType
56 {
57     CONTROL_REQUEST_ACK,
58     CONTROL_MESSAGE_ACK
59 } ResponseType_t;
60 
61 /*******************************************************************/
62 
63 
64 /*******************************************************************/
65 
66 // module variables
67 static char m_lastReceivedByte;							// last byte received
68 static char m_paired = 0;								// whether or not HSV is paired
69 static char m_address_lsb = 0x00;						// This is the address we are connected to
70 static char m_address_msb = 0x00;
71 //static char m_address = 0;							// address of the controller
72 static HSVState_t m_HSVState = WAITING_FOR_CONTROLLER;	// the state of the HSV controller
73 static PacketState_t m_packetState = PACKET_EMPTY;		// the state of the packet were in
74 static char m_possessFullPacket = 0;					// reveal whether or not we have a full packet
75 
76 // mode status (human vs zombie
77 static char m_isHuman = 1;								// tells whether were in human or zombie mode
78 static char	m_necSP = 0;								// speed enhancement
79 static char m_necLR = 0;								// left turn rate
80 static char	m_necRR = 0;								// right turn rate
81 static char	m_necSW = 0;								// swap left and right
82 
83 
84 // buffers
85 
86 // transmit related variables
87 static int m_txCurrInPacket = 0;						// place in transmit packet
88 static int m_txLength;									// transmit message length
89 static char m_transmitPacket[MAX_LENGTH_ACK_PACKET] = {0};	// transmit buffer
90 static char m_readyToTransmit = 0;						// whether or not were ready to transmit
91 static PacketType_t m_packetType = OTHER;
92 
93 // receive related variables
94 static int m_rxCurrInPacket = 0;						// place in receive packet
95 static int m_rxLength;									// receive message length
96 static char m_receivePacket[MAX_PACKET_LENGTH] = {0};	// receive buffer
97 static ResponseType_t m_response_type;
98 
99 // control instructions
100 static unsigned char m_direction = 0;					// boat direction
101 static unsigned char m_throttle = 0;					// boat throttle
102 static unsigned char m_shoot = 0;						// shoot or not
103 
104 /*******************************************************************/
105 
106 
107 /*******************************************************************/
108 
109 // module functions
110 
111 // update state machine to keep track of where we are in state machine
112 static void updateHSVStateMachine(char packet_type);
113 // update state machine to keep track of packet state
114 static void updatePacketStateMachine(char c);
115 // send control request acknowledged
116 static void sendControlRequestAcknowledged(void);
117 // get the address of the controller
118 static void storeControllerAddress(void);
119 // clear the buffer and counter keeping track of the current message
120 static void clearPacketBuffer(void);
121 
122 // transmit buffer related functions
123 // pack a message to transmit
124 static void packMessageToTransmit(void);
125 // calculate checksum for message to transmit
126 static char calculateTransmitChecksum(void);
127 
128 // receive buffer related functions
129 // get type of packet
130 static PacketType_t getPacketType(void);
131 // calculate checksum for message that is received
132 static char calculateReceiveChecksum(void);
133 
134 /*******************************************************************/
135 
136 
137 /*******************************************************************/
138 // determine if we're ready to transmit a message
139 char readyToTransmit(void)
140 {
141     if (transmitBufferEmpty())
142         {
143             if (m_readyToTransmit)
144                 {
145                     return 1;
146                 }
147         }
148     return 0;
149 }
150 /*******************************************************************/
151 
152 
153 /*******************************************************************/
154 // get the next byte to transmit
155 char getTransmitByte(void)
156 {
157     char txByte = m_transmitPacket[m_txCurrInPacket];
158     m_txCurrInPacket++;
159 
160     if (m_txCurrInPacket >= m_txLength+4)
161         {
162             m_readyToTransmit = 0;
163             m_txCurrInPacket = 0;
164         }
165 
166     return txByte;
167 }
168 /*******************************************************************/
169 
170 
171 /*******************************************************************/
172 // determine if were ready to receive
173 char readyToReceive(void)
174 {
175     if (receiveBufferFull())
176         return 1;
177     else
178         return 0;
179 }
180 /*******************************************************************/
181 
182 
183 /*******************************************************************/
184 // determine where to put received byte
185 void placeReceiveByte(char c)
186 {
187     m_lastReceivedByte = c;
188     updatePacketStateMachine(c);
189 }
190 /*******************************************************************/
191 
192 
193 /*******************************************************************/
194 // was a full packet received
195 char fullPacketReceived(void)
196 {
197     return m_possessFullPacket;
198 }
199 /*******************************************************************/
200 
201 
202 /*******************************************************************/
203 // say we have processed a packet
204 void packetProcessed(void)
205 {
206     // DO SOMETHING WITH PACKET HERE IF DESIRED
207 
208     // reset
209     m_possessFullPacket = 0;
210     clearPacketBuffer();
211 }
212 /*******************************************************************/
213 
214 
215 /*******************************************************************/
216 // handle expired timer to do list
217 void timeExpired(void)
218 {
219     // Disconnect to boat
220     m_paired = 0;
221     m_txCurrInPacket = 0;
222     m_rxCurrInPacket = 0;
223     m_readyToTransmit = 0;
224     m_packetState = PACKET_EMPTY;
225     m_HSVState = WAITING_FOR_CONTROLLER;
226     clearPacketBuffer();
227     resetTimer();
228 }
229 /*******************************************************************/
230 
231 
232 /*******************************************************************/
233 // get the throttle command
234 char getThrottle(void)
235 {
236     return m_throttle;
237 }
238 /*******************************************************************/
239 
240 
241 /*******************************************************************/
242 // get the direction command
243 char getDirection(void)
244 {
245     return m_direction;
246 }
247 /*******************************************************************/
248 
249 
250 /*******************************************************************/
251 // get whether or not to shoot
252 char getShoot(void)
253 {
254     return m_shoot;
255 }
256 /*******************************************************************/
257 
258 
259 /*******************************************************************/
260 // get whether we're paired or not
261 char getIsPaired(void)
262 {
263     return m_paired;
264 }
265 /*******************************************************************/
266 
267 
268 /*******************************************************************/
269 // set whether its paired or not
270 void setIsPaired(char isPaired)
271 {
272     if ( (isPaired == 1) || (isPaired == 0) )
273         m_paired = isPaired;
274 }
275 /*******************************************************************/
276 
277 /*******************************************************************/
278 // set what mode were in (human or zombie
279 void setIsHuman(char isHuman)
280 {
281     if ( (isHuman == 1) || (isHuman == 0) )
282         {
283             m_isHuman = isHuman;
284         }
285 }
286 /*******************************************************************/
287 
288 
289 /*******************************************************************/
290 // get what mode were in (human or zombie)
291 char getIsHuman(void)
292 {
293     return m_isHuman;
294 }
295 /*******************************************************************/
296 
297 
298 /*******************************************************************/
299 // adjust throttle if a zombie
300 unsigned char zombieAdjustThrottle(unsigned char orig)
301 {
302     unsigned char newThrottle = orig;
303 
304     // set parameters here
305 
306     // by 0.75
307     if (m_necSP != 0)
308         {
309             // adjust by 0.75
310             if ( orig <= 0x7F )
311                 {
312                     newThrottle = (orig/4)*3;
313                 }
314             else
315                 {
316                     newThrottle = 0xFF - ((0xFF - orig)/4)*3;
317                 }
318         }
319     // by 0.5
320     else
321         {
322             if ( orig <= 0x7F )
323                 {
324                     newThrottle = orig/2;
325                 }
326             else
327                 {
328                     newThrottle = 0xFF - (0xFF - orig)/2;
329                 }
330         }
331 
332     // switch forward and reverse if asked
333     if ((m_necSW&0xF0) != 0)
334         {
335             newThrottle = 0xFF - newThrottle;
336         }
337 
338     return newThrottle;
339 }
340 /*******************************************************************/
341 
342 
343 /*******************************************************************/
344 // adjust direction if a zombie
345 unsigned char zombieAdjustDirection(unsigned char orig)
346 {
347     unsigned char newDirection = orig;
348 
349     if (orig <= 0x7F)
350         {
351             // right turn reduction
352             if (m_necRR != 0)
353                 {
354                     newDirection = orig/2;
355                 }
356 
357         }
358     else
359         {
360             // left turn reduction
361             if (m_necLR != 0)
362                 {
363                     newDirection = 0xFF - (0xFF - orig)/2;
364                 }
365         }
366 
367 
368     // swap directions? if so, reverse the direction
369     if ((m_necSW&0x0F) != 0)
370         {
371             newDirection = 0xFF - newDirection;
372         }
373 
374     return newDirection;
375 }
376 /*******************************************************************/
377 
378 
379 /*******************************************************************/
380 
381 // PRIVATE MODULE FUNCTIONS
382 
383 /*******************************************************************/
384 
385 
386 /*******************************************************************/
387 // update state machine to keep track of where we are in state machine
388 static void updateHSVStateMachine(char packet_type)
389 {
390     unsigned char handshake_byte = m_receivePacket[5];
391 
392     switch (m_HSVState)
393         {
394         case WAITING_FOR_CONTROLLER:
395             switch (packet_type)
396                 {
397                 case CONTROL_REQUEST:
398                     // store source address
399                     storeControllerAddress();
400                     m_HSVState = WAITING_FOR_COMMAND;
401                     m_paired = 1;
402                     // send control request acknowledged
403                     m_response_type = CONTROL_REQUEST_ACK;
404                     sendControlRequestAcknowledged();
405                     break;
406                 case OTHER:
407                     break;
408                 default:
409                     break;
410                 }
411             break;
412         case WAITING_FOR_COMMAND:
413             switch (packet_type)
414                 {
415                 case STATUS_MESSAGE:
416                     if (handshake_byte==0x00 )  // Ack
417                         {
418                             // Do nothing
419                         }
420                     else   						// Nack
421                         {
422                             // Resend last response
423                             m_response_type = CONTROL_REQUEST_ACK;
424                             sendControlRequestAcknowledged();
425                         }
426                     break;
427                 case NECROMANCER_MESSAGE:
428                     // if were not human, listen to message
429                     if (!m_isHuman)
430                         {
431                             // parse necromancer commands here
432                             m_necSP = m_receivePacket[9];
433                             m_necLR = m_receivePacket[10];
434                             m_necRR = m_receivePacket[11];
435                             m_necSW = m_receivePacket[12];
436                         }
437                     break;
438                 case CONTROL_MESSAGE:
439                     m_response_type = CONTROL_MESSAGE_ACK;
440                     sendControlRequestAcknowledged();
441                     // store commands here
442                     m_throttle = m_receivePacket[9];
443                     m_direction = m_receivePacket[10];
444                     m_shoot = m_receivePacket[11];
445                     break;
446                 case OTHER:
447                     break;
448                 default:
449                     break;
450                 }
451             break;
452         default:
453             break;
454         }
455 }
456 /*******************************************************************/
457 
458 
459 /*******************************************************************/
460 // update state machine to keep track of packet state
461 static void updatePacketStateMachine(char c)
462 {
463     char packet_type;
464 
465     switch (m_packetState)
466         {
467         case PACKET_EMPTY:
468             // set timeout timer
469             m_rxCurrInPacket = 0;
470             if (c == 0x7E)
471                 {
472                     m_receivePacket[m_rxCurrInPacket] = c;
473                     m_rxCurrInPacket++;
474                     m_packetState = PACKET_IN_PROGRESS;
475                     m_rxLength = MAX_PACKET_LENGTH;
476                 }
477             break;
478 
479         case PACKET_IN_PROGRESS:
480             m_receivePacket[m_rxCurrInPacket] = c;
481             m_rxCurrInPacket++;
482 
483             // if we have 3 things in the packet, set the length
484             if (m_rxCurrInPacket == 3)
485                 {
486                     m_rxLength = m_receivePacket[1] << 8;
487                     m_rxLength += m_receivePacket[2];
488                     // 4 added for start, length, and stop bytes
489                     m_rxLength += 4;
490                 }
491 
492             // if packet is almost full, go to packet full state
493             if (m_rxCurrInPacket == (m_rxLength-1))
494                 {
495                     m_packetState = PACKET_FULL;
496                 }
497             break;
498 
499         case PACKET_FULL:
500             m_receivePacket[m_rxCurrInPacket] = c;
501             m_rxCurrInPacket++;
502             if (calculateReceiveChecksum() == 0xFF)
503                 {
504                     m_possessFullPacket = 1;
505                     // determine packet type
506                     packet_type = getPacketType();
507                     // update HSV state machine
508                     m_packetType = packet_type;
509                     updateHSVStateMachine(packet_type);
510                 }
511             else
512                 {
513                     clearPacketBuffer();
514                 }
515             m_packetState = PACKET_EMPTY;
516 
517             // restart the timer for timeout
518             resetTimer();
519             startTimer(CONTROL_TIMEOUT);
520             break;
521 
522         default:
523             // should never be here
524             break;
525 
526         }
527 }
528 /*******************************************************************/
529 
530 
531 /*******************************************************************/
532 // send control request acknowledged
533 static void sendControlRequestAcknowledged(void)
534 {
535     // put packet together
536     packMessageToTransmit();
537     // notify module that we'd like to send a message
538     m_txCurrInPacket = 0;
539     // notify were ready to transmit
540     m_readyToTransmit = 1;
541 }
542 /*******************************************************************/
543 
544 
545 /*******************************************************************/
546 // get the address of the controller
547 static void storeControllerAddress(void)
548 {
549     // get the source address of the controller from the buffer
550     m_address_lsb = m_receivePacket[ADDRESS_LSB];
551     m_address_msb = m_receivePacket[ADDRESS_MSB];
552 }
553 /*******************************************************************/
554 
555 
556 /*******************************************************************/
557 // clear the buffer and counter keeping track of the current message
558 static void clearPacketBuffer(void)
559 {
560     int i;
561     // clear contents of the buffer
562     for (i = 0; i < MAX_PACKET_LENGTH; i++)
563         {
564             m_receivePacket[i] = '0';
565         }
566     m_rxCurrInPacket = 0;
567     m_receivePacket[0] = '1';
568 }
569 /*******************************************************************/
570 
571 
572 /*******************************************************************/
573 // pack a message to transmit
574 static void packMessageToTransmit(void)
575 {
576     // make the message to transmit here
577 
578     if ( m_response_type == CONTROL_REQUEST_ACK)
579         {
580             m_txLength = 0x07;
581         }
582     else if ( m_response_type == CONTROL_MESSAGE_ACK)
583         {
584             m_txLength= 0x06;
585         }
586 
587     // FILL BUFFER HERE
588     m_transmitPacket[0] = 0x7e;								// start
589     m_transmitPacket[1] = 0x00;								// msb
590     m_transmitPacket[2] = m_txLength;						// lsb
591     m_transmitPacket[3] = 0x01;								// api identifier
592     m_transmitPacket[4] = 0x01;								// anything but 0x00
593     m_transmitPacket[5] = m_address_msb;					// destination msb
594     m_transmitPacket[6] = m_address_lsb;					// destination lsb
595     m_transmitPacket[7] = 0x00;								// options
596 
597     if ( m_response_type==CONTROL_REQUEST_ACK)
598         {
599             m_transmitPacket[8] = 0x03;							// identifier
600             m_transmitPacket[9] = 0xCD;							// code
601             m_transmitPacket[10] = calculateTransmitChecksum();	// checksum
602         }
603     else if ( m_response_type==CONTROL_MESSAGE_ACK)
604         {
605             m_transmitPacket[8] = 0x04;							// identifier
606             m_transmitPacket[9] = calculateTransmitChecksum();	// checksum
607         }
608 }
609 /*******************************************************************/
610 
611 
612 /*******************************************************************/
613 // get type of packet
614 static PacketType_t getPacketType(void)
615 {
616     char api_indicator;
617     char message_indicator;
618 
619     api_indicator = m_receivePacket[API_INDICATOR_BYTE_NUM];
620 
621     if (api_indicator == 0x81)
622         {
623             message_indicator = m_receivePacket[MESSAGE_INDICATOR_BYTE_NUM];
624             if (message_indicator == 0x03)
625                 {
626                     return CONTROL_REQUEST;
627                 }
628             else if (message_indicator == 0x01)
629                 {
630                     return NECROMANCER_MESSAGE;
631                 }
632             else if (message_indicator == 0x02)
633                 {
634                     return CONTROL_MESSAGE;
635                 }
636             else
637                 return OTHER;
638             // Never do this;
639         }
640     else if (api_indicator == 0x89)
641         {
642             return STATUS_MESSAGE;
643         }
644     else
645         {
646             return OTHER;
647         }
648 }
649 /*******************************************************************/
650 
651 
652 /*******************************************************************/
653 // get transmit checksum
654 static char calculateTransmitChecksum(void)
655 {
656     int i;
657     char total = 0;
658 
659     for (i=3; i<m_txLength+3; i++)
660         {
661             total += m_transmitPacket[i];
662         }
663     total = 0xFF - total;
664     return total;
665 }
666 /*******************************************************************/
667 
668 
669 
670 /*******************************************************************/
671 // get receive checksum
672 static char calculateReceiveChecksum(void)
673 {
674     int i;
675     char total = 0;
676 
677     for (i=3; i<=m_rxLength-1; i++)
678         {
679             total += m_receivePacket[i];
680         }
681     return total;
682 }
683 /*******************************************************************/
684