aaaaa

Dependencies:   mbed Servo

Committer:
Ryosei
Date:
Mon Feb 25 06:44:02 2019 +0000
Revision:
12:8321e464475f
Parent:
0:562021ed1ba9
unko

Who changed what in which revision?

UserRevisionLine numberNew contents of line
t_yamamoto 0:562021ed1ba9 1 #include "XBee.h"
t_yamamoto 0:562021ed1ba9 2
t_yamamoto 0:562021ed1ba9 3 #include <stdint.h>
t_yamamoto 0:562021ed1ba9 4 #include "mbed.h"
t_yamamoto 0:562021ed1ba9 5
t_yamamoto 0:562021ed1ba9 6 namespace XBEE
t_yamamoto 0:562021ed1ba9 7 {
t_yamamoto 0:562021ed1ba9 8 Ticker xbee_timer;
t_yamamoto 0:562021ed1ba9 9 Serial xbee_uart(XBEE_TX, XBEE_RX);
t_yamamoto 0:562021ed1ba9 10 DigitalOut XBee_LED(LED1);
t_yamamoto 0:562021ed1ba9 11
t_yamamoto 0:562021ed1ba9 12 void uartUpdate(void);
t_yamamoto 0:562021ed1ba9 13 void lostCheck(void);
t_yamamoto 0:562021ed1ba9 14
t_yamamoto 0:562021ed1ba9 15 namespace
t_yamamoto 0:562021ed1ba9 16 {
t_yamamoto 0:562021ed1ba9 17 ControllerData ctrData;
t_yamamoto 0:562021ed1ba9 18 ControllerData keepCtrData;
t_yamamoto 0:562021ed1ba9 19 const uint8_t defaultData[4] = CTR_DEFAULT_DATA;
t_yamamoto 0:562021ed1ba9 20 const char check[] = "DT=";
t_yamamoto 0:562021ed1ba9 21 volatile char packet[24];
t_yamamoto 0:562021ed1ba9 22
t_yamamoto 0:562021ed1ba9 23 bool controllerLost = false;
t_yamamoto 0:562021ed1ba9 24 uint8_t timerCount = 0;
t_yamamoto 0:562021ed1ba9 25 }
t_yamamoto 0:562021ed1ba9 26
t_yamamoto 0:562021ed1ba9 27 void Controller::Initialize(void) {
t_yamamoto 0:562021ed1ba9 28 xbee_timer.attach(lostCheck, 0.025);
t_yamamoto 0:562021ed1ba9 29 xbee_uart.baud(4800);
t_yamamoto 0:562021ed1ba9 30 xbee_uart.attach(uartUpdate, Serial::RxIrq);
t_yamamoto 0:562021ed1ba9 31 DataReset();
t_yamamoto 0:562021ed1ba9 32 }
t_yamamoto 0:562021ed1ba9 33
t_yamamoto 0:562021ed1ba9 34 ControllerData* Controller::GetData(void) {
t_yamamoto 0:562021ed1ba9 35 __disable_irq();
t_yamamoto 0:562021ed1ba9 36 for(uint8_t i = 0; i < CTR_DATA_LENGTH; i++) keepCtrData.buf[i] = ctrData.buf[i];
t_yamamoto 0:562021ed1ba9 37 __enable_irq();
t_yamamoto 0:562021ed1ba9 38 return &keepCtrData;
t_yamamoto 0:562021ed1ba9 39 }
t_yamamoto 0:562021ed1ba9 40
t_yamamoto 0:562021ed1ba9 41 void Controller::DataReset(void) {
t_yamamoto 0:562021ed1ba9 42 __disable_irq();
t_yamamoto 0:562021ed1ba9 43 for(uint8_t i = 0; i < CTR_DATA_LENGTH; i++) ctrData.buf[i] = defaultData[i];
t_yamamoto 0:562021ed1ba9 44 __enable_irq();
t_yamamoto 0:562021ed1ba9 45 }
t_yamamoto 0:562021ed1ba9 46
t_yamamoto 0:562021ed1ba9 47 bool Controller::CheckControllerLost(void) {
t_yamamoto 0:562021ed1ba9 48 return controllerLost;
t_yamamoto 0:562021ed1ba9 49 }
t_yamamoto 0:562021ed1ba9 50
t_yamamoto 0:562021ed1ba9 51 void uartUpdate(void) {
t_yamamoto 0:562021ed1ba9 52 static bool phase = false;
t_yamamoto 0:562021ed1ba9 53 static uint8_t count = 0;
t_yamamoto 0:562021ed1ba9 54
t_yamamoto 0:562021ed1ba9 55 char data = xbee_uart.getc();
t_yamamoto 0:562021ed1ba9 56
t_yamamoto 0:562021ed1ba9 57 if(phase)
t_yamamoto 0:562021ed1ba9 58 {
t_yamamoto 0:562021ed1ba9 59 packet[count] = data;
t_yamamoto 0:562021ed1ba9 60 if(count < 2)
t_yamamoto 0:562021ed1ba9 61 {
t_yamamoto 0:562021ed1ba9 62 if(data != check[count])
t_yamamoto 0:562021ed1ba9 63 {
t_yamamoto 0:562021ed1ba9 64 phase = false;
t_yamamoto 0:562021ed1ba9 65 controllerLost = true;
t_yamamoto 0:562021ed1ba9 66 XBee_LED = LED_OFF;
t_yamamoto 0:562021ed1ba9 67 }
t_yamamoto 0:562021ed1ba9 68 }
t_yamamoto 0:562021ed1ba9 69 else if(count == 8)
t_yamamoto 0:562021ed1ba9 70 {
t_yamamoto 0:562021ed1ba9 71 if(data != '\r')
t_yamamoto 0:562021ed1ba9 72 {
t_yamamoto 0:562021ed1ba9 73 phase = false;
t_yamamoto 0:562021ed1ba9 74 controllerLost = true;
t_yamamoto 0:562021ed1ba9 75 XBee_LED = LED_OFF;
t_yamamoto 0:562021ed1ba9 76 }
t_yamamoto 0:562021ed1ba9 77 else
t_yamamoto 0:562021ed1ba9 78 {
t_yamamoto 0:562021ed1ba9 79 ctrData.buf[0] = packet[4];
t_yamamoto 0:562021ed1ba9 80 ctrData.buf[1] = packet[5];
t_yamamoto 0:562021ed1ba9 81 ctrData.buf[2] = packet[6];
t_yamamoto 0:562021ed1ba9 82 ctrData.buf[3] = packet[7];
t_yamamoto 0:562021ed1ba9 83 phase = false;
t_yamamoto 0:562021ed1ba9 84 timerCount = 0;
t_yamamoto 0:562021ed1ba9 85 controllerLost = false;
t_yamamoto 0:562021ed1ba9 86 XBee_LED = LED_ON;
t_yamamoto 0:562021ed1ba9 87 }
t_yamamoto 0:562021ed1ba9 88 }
t_yamamoto 0:562021ed1ba9 89 count++;
t_yamamoto 0:562021ed1ba9 90 }
t_yamamoto 0:562021ed1ba9 91 else
t_yamamoto 0:562021ed1ba9 92 {
t_yamamoto 0:562021ed1ba9 93 if(data == '@')
t_yamamoto 0:562021ed1ba9 94 {
t_yamamoto 0:562021ed1ba9 95 count = 0;
t_yamamoto 0:562021ed1ba9 96 phase = true;
t_yamamoto 0:562021ed1ba9 97 }
t_yamamoto 0:562021ed1ba9 98 }
t_yamamoto 0:562021ed1ba9 99 }
t_yamamoto 0:562021ed1ba9 100
t_yamamoto 0:562021ed1ba9 101 void lostCheck(void) {
t_yamamoto 0:562021ed1ba9 102 timerCount++;
t_yamamoto 0:562021ed1ba9 103 if(timerCount == 2) XBee_LED = LED_OFF;
t_yamamoto 0:562021ed1ba9 104 if(timerCount >= 20) {
t_yamamoto 0:562021ed1ba9 105 controllerLost = true;
t_yamamoto 0:562021ed1ba9 106 Controller::DataReset();
t_yamamoto 0:562021ed1ba9 107 timerCount = 0;
t_yamamoto 0:562021ed1ba9 108 XBee_LED = LED_OFF;
t_yamamoto 0:562021ed1ba9 109 }
t_yamamoto 0:562021ed1ba9 110 }
t_yamamoto 0:562021ed1ba9 111 }
t_yamamoto 0:562021ed1ba9 112