Eurobot2012_Primary

Dependencies:   mbed Eurobot_2012_Primary

Committer:
narshu
Date:
Sat Apr 28 17:21:24 2012 +0000
Revision:
9:377560539b74
Restructured project to have a single shared lib; Also raised the RF baud rate

Who changed what in which revision?

UserRevisionLine numberNew contents of line
narshu 9:377560539b74 1 #include "IR.h"
narshu 9:377560539b74 2 #include "Kalman.h"
narshu 9:377560539b74 3 #include "system.h"
narshu 9:377560539b74 4 #include "geometryfuncs.h"
narshu 9:377560539b74 5 #include "globals.h"
narshu 9:377560539b74 6 #include "mbed.h"
narshu 9:377560539b74 7
narshu 9:377560539b74 8 IR::IR(Kalman &kalmanin):
narshu 9:377560539b74 9 #ifdef ROBOT_PRIMARY
narshu 9:377560539b74 10 IRserial(p9, p10),
narshu 9:377560539b74 11 #else
narshu 9:377560539b74 12 IRserial(p13, p14),
narshu 9:377560539b74 13 #endif
narshu 9:377560539b74 14 kalman(kalmanin) {
narshu 9:377560539b74 15
narshu 9:377560539b74 16 //Setting up IR serial
narshu 9:377560539b74 17 IRserial.baud(115200);
narshu 9:377560539b74 18 IRserial.format(8,Serial::Odd,1);
narshu 9:377560539b74 19 }
narshu 9:377560539b74 20
narshu 9:377560539b74 21 void IR::detachisr() {
narshu 9:377560539b74 22 IRserial.attach(NULL,Serial::RxIrq);
narshu 9:377560539b74 23 }
narshu 9:377560539b74 24
narshu 9:377560539b74 25 void IR::attachisr() {
narshu 9:377560539b74 26 IRserial.attach(this, &IR::vIRValueISR, Serial::RxIrq);
narshu 9:377560539b74 27 }
narshu 9:377560539b74 28
narshu 9:377560539b74 29 void IR::vIRValueISR (void) {
narshu 9:377560539b74 30
narshu 9:377560539b74 31 // A workaround for mbed UART ISR bug
narshu 9:377560539b74 32 // Clear the RBR flag to make sure the interrupt doesn't loop
narshu 9:377560539b74 33 // UART3 for the port on pins 9/10, UART2 for pins 28/27, and UART1 for pins 13/14.
narshu 9:377560539b74 34 // UART0 for USB UART
narshu 9:377560539b74 35
narshu 9:377560539b74 36 #ifdef ROBOT_PRIMARY
narshu 9:377560539b74 37 unsigned char RBR = LPC_UART3->RBR;
narshu 9:377560539b74 38 #else
narshu 9:377560539b74 39 unsigned char RBR = LPC_UART1->RBR;
narshu 9:377560539b74 40 #endif
narshu 9:377560539b74 41
narshu 9:377560539b74 42 // bytes packing/unpacking for IR turret serial comm
narshu 9:377560539b74 43 static union IRValue_t {
narshu 9:377560539b74 44 float IR_floats[3];
narshu 9:377560539b74 45 int IR_ints[3];
narshu 9:377560539b74 46 unsigned char IR_chars[12];
narshu 9:377560539b74 47 } IRValues;
narshu 9:377560539b74 48
narshu 9:377560539b74 49 const char Alignment_char[4] = {0xFF,0xFE,0xFD,0xFC};
narshu 9:377560539b74 50 static int Alignment_ptr = 0;
narshu 9:377560539b74 51 static bool data_flag = false;
narshu 9:377560539b74 52 static int buff_pointer = 0;
narshu 9:377560539b74 53
narshu 9:377560539b74 54 if (!data_flag) { // look for alignment bytes
narshu 9:377560539b74 55 if (RBR == Alignment_char[Alignment_ptr]) {
narshu 9:377560539b74 56 Alignment_ptr ++;
narshu 9:377560539b74 57 }
narshu 9:377560539b74 58 if (Alignment_ptr >= 4) {
narshu 9:377560539b74 59 Alignment_ptr = 0;
narshu 9:377560539b74 60 data_flag = true; // set the dataflag
narshu 9:377560539b74 61 }
narshu 9:377560539b74 62 } else { // fetch data bytes
narshu 9:377560539b74 63 IRValues.IR_chars[buff_pointer] = RBR;
narshu 9:377560539b74 64 buff_pointer ++;
narshu 9:377560539b74 65 if (buff_pointer >= 12) {
narshu 9:377560539b74 66 buff_pointer = 0;
narshu 9:377560539b74 67 data_flag = false; // dessert the dataflag
narshu 9:377560539b74 68 kalman.runupdate(Kalman::measurement_t(IRValues.IR_ints[0]+3),IRValues.IR_floats[1],IRvariance);
narshu 9:377560539b74 69
narshu 9:377560539b74 70
narshu 9:377560539b74 71 }
narshu 9:377560539b74 72
narshu 9:377560539b74 73 }
narshu 9:377560539b74 74 }