4180 Final Project Jenny Hunter, Derek Travisano, Sandeep Vijayasekar, Zechariah Lin
Dependencies: LSM9DS0 PinDetect mbed
main.cpp@0:1fa4ca8a4394, 2015-12-09 (annotated)
- Committer:
- dtravisano3
- Date:
- Wed Dec 09 17:51:26 2015 +0000
- Revision:
- 0:1fa4ca8a4394
4180 Final Project; Jenny Hunter, Derek Travisano, Zechariah Lin, Sandeep Vijayasekar
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dtravisano3 | 0:1fa4ca8a4394 | 1 | #include "mbed.h" |
dtravisano3 | 0:1fa4ca8a4394 | 2 | #include "LSM9DS0.h" |
dtravisano3 | 0:1fa4ca8a4394 | 3 | #include "PinDetect.h" |
dtravisano3 | 0:1fa4ca8a4394 | 4 | |
dtravisano3 | 0:1fa4ca8a4394 | 5 | // SDO_XM and SDO_G are pulled up, so our addresses are: |
dtravisano3 | 0:1fa4ca8a4394 | 6 | #define LSM9DS0_XM_ADDR 0x1D // Would be 0x1E if SDO_XM is LOW |
dtravisano3 | 0:1fa4ca8a4394 | 7 | #define LSM9DS0_G_ADDR 0x6B // Would be 0x6A if SDO_G is LOW |
dtravisano3 | 0:1fa4ca8a4394 | 8 | |
dtravisano3 | 0:1fa4ca8a4394 | 9 | LSM9DS0 imu(p9, p10, LSM9DS0_G_ADDR, LSM9DS0_XM_ADDR); // IMU |
dtravisano3 | 0:1fa4ca8a4394 | 10 | DigitalIn L(p21); // Left flipper |
dtravisano3 | 0:1fa4ca8a4394 | 11 | DigitalIn R(p22); // Right flipper |
dtravisano3 | 0:1fa4ca8a4394 | 12 | Serial pc(USBTX, USBRX); // Serial read out |
dtravisano3 | 0:1fa4ca8a4394 | 13 | AnalogIn plunger(p20); // Slide potentiometer |
dtravisano3 | 0:1fa4ca8a4394 | 14 | DigitalOut led1(LED1); //left |
dtravisano3 | 0:1fa4ca8a4394 | 15 | DigitalOut led2(LED2); //right |
dtravisano3 | 0:1fa4ca8a4394 | 16 | DigitalOut led3(LED3); //coin |
dtravisano3 | 0:1fa4ca8a4394 | 17 | AnalogIn coin(p19); // IR Sensor |
dtravisano3 | 0:1fa4ca8a4394 | 18 | |
dtravisano3 | 0:1fa4ca8a4394 | 19 | //variables changed by callback functions to be printed to serial |
dtravisano3 | 0:1fa4ca8a4394 | 20 | int coins=0; |
dtravisano3 | 0:1fa4ca8a4394 | 21 | int volatile left=0; |
dtravisano3 | 0:1fa4ca8a4394 | 22 | int volatile right=0; |
dtravisano3 | 0:1fa4ca8a4394 | 23 | int plungers=0; |
dtravisano3 | 0:1fa4ca8a4394 | 24 | void L_hit_callback (void) |
dtravisano3 | 0:1fa4ca8a4394 | 25 | { |
dtravisano3 | 0:1fa4ca8a4394 | 26 | left=1; |
dtravisano3 | 0:1fa4ca8a4394 | 27 | } |
dtravisano3 | 0:1fa4ca8a4394 | 28 | |
dtravisano3 | 0:1fa4ca8a4394 | 29 | void R_hit_callback (void) |
dtravisano3 | 0:1fa4ca8a4394 | 30 | { |
dtravisano3 | 0:1fa4ca8a4394 | 31 | right=1; |
dtravisano3 | 0:1fa4ca8a4394 | 32 | } |
dtravisano3 | 0:1fa4ca8a4394 | 33 | |
dtravisano3 | 0:1fa4ca8a4394 | 34 | int main() |
dtravisano3 | 0:1fa4ca8a4394 | 35 | { |
dtravisano3 | 0:1fa4ca8a4394 | 36 | // Use the begin() function to initialize the LSM9DS0 library. |
dtravisano3 | 0:1fa4ca8a4394 | 37 | // You can either call it with no parameters (the easy way): |
dtravisano3 | 0:1fa4ca8a4394 | 38 | uint16_t status = imu.begin(); |
dtravisano3 | 0:1fa4ca8a4394 | 39 | |
dtravisano3 | 0:1fa4ca8a4394 | 40 | //Make sure communication is working (DEBUG) |
dtravisano3 | 0:1fa4ca8a4394 | 41 | pc.printf("LSM9DS0 WHO_AM_I's returned: 0x%X\n", status); |
dtravisano3 | 0:1fa4ca8a4394 | 42 | pc.printf("Should be 0x49D4\n\n"); |
dtravisano3 | 0:1fa4ca8a4394 | 43 | while(1) { |
dtravisano3 | 0:1fa4ca8a4394 | 44 | //Setup default values |
dtravisano3 | 0:1fa4ca8a4394 | 45 | plungers=0; |
dtravisano3 | 0:1fa4ca8a4394 | 46 | led1=L; |
dtravisano3 | 0:1fa4ca8a4394 | 47 | led2=R; |
dtravisano3 | 0:1fa4ca8a4394 | 48 | left=L; |
dtravisano3 | 0:1fa4ca8a4394 | 49 | right=R; |
dtravisano3 | 0:1fa4ca8a4394 | 50 | |
dtravisano3 | 0:1fa4ca8a4394 | 51 | // imu readings |
dtravisano3 | 0:1fa4ca8a4394 | 52 | imu.readAccel(); |
dtravisano3 | 0:1fa4ca8a4394 | 53 | float heading = imu.calcHeading(); |
dtravisano3 | 0:1fa4ca8a4394 | 54 | |
dtravisano3 | 0:1fa4ca8a4394 | 55 | //detect if coin was inserted |
dtravisano3 | 0:1fa4ca8a4394 | 56 | if (coin>0.35f) { |
dtravisano3 | 0:1fa4ca8a4394 | 57 | led3=1; |
dtravisano3 | 0:1fa4ca8a4394 | 58 | coins=1; |
dtravisano3 | 0:1fa4ca8a4394 | 59 | } else { |
dtravisano3 | 0:1fa4ca8a4394 | 60 | led3=0; |
dtravisano3 | 0:1fa4ca8a4394 | 61 | coins=0; |
dtravisano3 | 0:1fa4ca8a4394 | 62 | } |
dtravisano3 | 0:1fa4ca8a4394 | 63 | // which speed to launch ball at |
dtravisano3 | 0:1fa4ca8a4394 | 64 | if (plunger<0.25f) { |
dtravisano3 | 0:1fa4ca8a4394 | 65 | plungers=1; |
dtravisano3 | 0:1fa4ca8a4394 | 66 | } else if(plunger<0.5f) { |
dtravisano3 | 0:1fa4ca8a4394 | 67 | plungers=2; |
dtravisano3 | 0:1fa4ca8a4394 | 68 | } else if(plunger<0.75f) { |
dtravisano3 | 0:1fa4ca8a4394 | 69 | plungers=3; |
dtravisano3 | 0:1fa4ca8a4394 | 70 | } else if(plunger<2.0f) { |
dtravisano3 | 0:1fa4ca8a4394 | 71 | plungers=4; |
dtravisano3 | 0:1fa4ca8a4394 | 72 | } |
dtravisano3 | 0:1fa4ca8a4394 | 73 | // Write to serial - order matters (left, right coin, plunger, imu) |
dtravisano3 | 0:1fa4ca8a4394 | 74 | pc.printf("%i,%i,%i,%i,%.1f\r\n",left,right,coins,plungers, imu.ay); |
dtravisano3 | 0:1fa4ca8a4394 | 75 | wait(0.20); |
dtravisano3 | 0:1fa4ca8a4394 | 76 | } |
dtravisano3 | 0:1fa4ca8a4394 | 77 | } |