Device to measure angle and get IMU measurements.

Dependencies:   mbed commands BLE_API nRF51822

Committer:
dkester
Date:
Thu Jun 11 20:59:22 2015 +0000
Revision:
8:c6345e8d607c
Parent:
6:75263c93daf7
working without Iic

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dkester 2:871b5efb2043 1 #include "Controller.h"
dkester 8:c6345e8d607c 2 #include "InterruptManager.h"
dkester 2:871b5efb2043 3
dkester 8:c6345e8d607c 4 InterruptIn imu(p16);
dkester 4:2a5a08b14539 5 InterruptIn button(p23);
dkester 8:c6345e8d607c 6
dkester 8:c6345e8d607c 7
dkester 8:c6345e8d607c 8 //Ticker ticker;
dkester 8:c6345e8d607c 9 //Ticker tickerData;
dkester 4:2a5a08b14539 10
dkester 5:46947b447701 11 Controller::Controller()
dkester 5:46947b447701 12 {
dkester 8:c6345e8d607c 13 buttonIntFlag = false;
dkester 8:c6345e8d607c 14 imuIntFlag = false;
dkester 8:c6345e8d607c 15 tickerIntFlag = false;
dkester 8:c6345e8d607c 16 currentModeSelector = 3;
dkester 8:c6345e8d607c 17 tickerIntDataFlag = false;
dkester 8:c6345e8d607c 18
dkester 8:c6345e8d607c 19 angle = 0;
dkester 8:c6345e8d607c 20 peak = false;
dkester 8:c6345e8d607c 21
dkester 8:c6345e8d607c 22 sensors = new Sensors();
dkester 8:c6345e8d607c 23 storage = new Storage();
dkester 8:c6345e8d607c 24 gonioService = new GonioService();
dkester 8:c6345e8d607c 25
dkester 8:c6345e8d607c 26 //Set up IMU with sample frequency
dkester 8:c6345e8d607c 27 sensors->setupIMU(0x4f);
dkester 8:c6345e8d607c 28
dkester 8:c6345e8d607c 29 modeList.push_back(new TrainingCommand(sensors, gonioService));
dkester 8:c6345e8d607c 30 modeList.push_back(new OfflineCommand(sensors, storage));
dkester 8:c6345e8d607c 31 modeList.push_back(new ReadCommand(storage, gonioService));
dkester 8:c6345e8d607c 32 modeList.push_back(new IdleCommand(sensors, gonioService));
dkester 8:c6345e8d607c 33 modeList.push_back(new SleepCommand(sensors));
dkester 8:c6345e8d607c 34
dkester 8:c6345e8d607c 35 wait(0.01);
dkester 8:c6345e8d607c 36
dkester 8:c6345e8d607c 37 //sensors->disableIMU();
dkester 5:46947b447701 38
dkester 4:2a5a08b14539 39 button.fall(this, &Controller::buttonInt);
dkester 4:2a5a08b14539 40 imu.fall(this, &Controller::imuInt);
dkester 5:46947b447701 41
dkester 5:46947b447701 42 //Set idle command at startup
dkester 8:c6345e8d607c 43 currentMode()->initialize();
dkester 5:46947b447701 44 }
dkester 5:46947b447701 45
dkester 5:46947b447701 46 void Controller::setCommand(int n)
dkester 5:46947b447701 47 {
dkester 8:c6345e8d607c 48 currentMode()->finish();
dkester 8:c6345e8d607c 49 currentModeSelector = n;
dkester 8:c6345e8d607c 50 currentMode()->initialize();
dkester 8:c6345e8d607c 51 }
dkester 8:c6345e8d607c 52
dkester 8:c6345e8d607c 53 Command* Controller::currentMode()
dkester 8:c6345e8d607c 54 {
dkester 8:c6345e8d607c 55 return modeList[currentModeSelector];
dkester 5:46947b447701 56 }
dkester 5:46947b447701 57
dkester 5:46947b447701 58 void Controller::imuInt()
dkester 5:46947b447701 59 {
dkester 8:c6345e8d607c 60
dkester 8:c6345e8d607c 61 imuIntFlag = true;
dkester 5:46947b447701 62 }
dkester 5:46947b447701 63
dkester 5:46947b447701 64 void Controller::buttonInt()
dkester 5:46947b447701 65 {
dkester 8:c6345e8d607c 66 wait(0.25); //wait for debounce
dkester 8:c6345e8d607c 67 buttonIntFlag = true;
dkester 8:c6345e8d607c 68 }
dkester 8:c6345e8d607c 69
dkester 8:c6345e8d607c 70 void Controller::tickerInt()
dkester 8:c6345e8d607c 71 {
dkester 8:c6345e8d607c 72 tickerIntFlag = true;
dkester 8:c6345e8d607c 73 }
dkester 8:c6345e8d607c 74
dkester 8:c6345e8d607c 75 void Controller::tickerDataInt()
dkester 8:c6345e8d607c 76 {
dkester 8:c6345e8d607c 77 tickerIntDataFlag = true;
dkester 5:46947b447701 78 }
dkester 5:46947b447701 79
dkester 5:46947b447701 80 void Controller::run()
dkester 5:46947b447701 81 {
dkester 8:c6345e8d607c 82 /*
dkester 8:c6345e8d607c 83 ticker.attach(this, &Controller::tickerInt, 0.1);
dkester 8:c6345e8d607c 84 wait(0.05);
dkester 8:c6345e8d607c 85 tickerData.attach(this, &Controller::tickerDataInt, 0.1);
dkester 8:c6345e8d607c 86 */
dkester 8:c6345e8d607c 87
dkester 5:46947b447701 88
dkester 5:46947b447701 89 while(1) {
dkester 8:c6345e8d607c 90
dkester 8:c6345e8d607c 91 if(gonioService->isConnected() & gonioService->newValue()){
dkester 8:c6345e8d607c 92
dkester 8:c6345e8d607c 93 int8_t command = gonioService->getWriteValue() - 1;
dkester 8:c6345e8d607c 94 printf("%d", command);
dkester 5:46947b447701 95 switch(command) {
dkester 8:c6345e8d607c 96 case 0 :
dkester 8:c6345e8d607c 97 printf("een\n");
dkester 5:46947b447701 98 setCommand(0);
dkester 5:46947b447701 99 break;
dkester 8:c6345e8d607c 100 case 1 :
dkester 8:c6345e8d607c 101 printf("twee\n");
dkester 8:c6345e8d607c 102 gonioService->disconnect();
dkester 5:46947b447701 103 setCommand(1);
dkester 5:46947b447701 104 break;
dkester 8:c6345e8d607c 105 case 2 :
dkester 8:c6345e8d607c 106 printf("drie\n");
dkester 5:46947b447701 107 setCommand(2);
dkester 5:46947b447701 108 break;
dkester 8:c6345e8d607c 109 case 3 :
dkester 8:c6345e8d607c 110 printf("vier\n");
dkester 5:46947b447701 111 setCommand(3);
dkester 5:46947b447701 112 break;
dkester 8:c6345e8d607c 113 }
dkester 5:46947b447701 114 }
dkester 8:c6345e8d607c 115
dkester 8:c6345e8d607c 116
dkester 8:c6345e8d607c 117 if(buttonIntFlag) {
dkester 8:c6345e8d607c 118 buttonIntFlag = false;
dkester 8:c6345e8d607c 119 currentMode()->button();
dkester 8:c6345e8d607c 120 }
dkester 8:c6345e8d607c 121
dkester 8:c6345e8d607c 122 if(tickerIntDataFlag) {
dkester 8:c6345e8d607c 123 tickerIntDataFlag = false;
dkester 8:c6345e8d607c 124 }
dkester 8:c6345e8d607c 125
dkester 8:c6345e8d607c 126 if(tickerIntFlag) {
dkester 8:c6345e8d607c 127 tickerIntFlag = false;
dkester 8:c6345e8d607c 128 }
dkester 8:c6345e8d607c 129
dkester 8:c6345e8d607c 130 if(imuIntFlag) {
dkester 8:c6345e8d607c 131 imuIntFlag = false;
dkester 8:c6345e8d607c 132 currentMode()->execute();
dkester 5:46947b447701 133 }
dkester 2:871b5efb2043 134 }
dkester 5:46947b447701 135 }