Nucleo-transfer
Dependencies: ADS1015 MPU6050 PixelArray PixelArray-Nucleo mbed WS2813
Fork of Nucleo-transfer by
Sensorplate/main.cpp@2:c6497fc90768, 2017-09-14 (annotated)
- Committer:
- DEldering
- Date:
- Thu Sep 14 08:41:26 2017 +0000
- Revision:
- 2:c6497fc90768
- Parent:
- ADS1115-hello_world/main.cpp@1:a8e61f3910ad
- Child:
- 3:cb38f2b1fd5a
Renamed something
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DEldering | 0:c0e44c46c573 | 1 | #include "mbed.h" |
DEldering | 0:c0e44c46c573 | 2 | #include "Adafruit_ADS1015.h" |
DEldering | 0:c0e44c46c573 | 3 | #include "USBSerial.h" |
DEldering | 0:c0e44c46c573 | 4 | #include "MPU6050.h" |
DEldering | 0:c0e44c46c573 | 5 | |
DEldering | 1:a8e61f3910ad | 6 | I2C i2c(p28, p27); // I2C |
DEldering | 1:a8e61f3910ad | 7 | MPU6050 agu(p28,p27); // Accelerometer/Gyroscope Unit |
DEldering | 1:a8e61f3910ad | 8 | Adafruit_ADS1115 pr1(&i2c, 0x48); // first PiëzoResistive ADC |
DEldering | 1:a8e61f3910ad | 9 | Adafruit_ADS1115 pr2(&i2c, 0x49); // second PiëzoResistive ADC |
DEldering | 1:a8e61f3910ad | 10 | Adafruit_ADS1115 pel(&i2c, 0x4B); // PiëzoElectric ADC |
DEldering | 1:a8e61f3910ad | 11 | Serial pc(USBTX, USBRX); // tx, rx // Serial USB connection |
DEldering | 1:a8e61f3910ad | 12 | Timer t; // Timer for equally time-spaced samples |
DEldering | 1:a8e61f3910ad | 13 | Ticker sample_cycle; // Polling cycle |
DEldering | 1:a8e61f3910ad | 14 | int cycle_time = 100000; // Cycle time in us |
DEldering | 1:a8e61f3910ad | 15 | int i2c_freq = 400000; // I2C Frequency |
DEldering | 1:a8e61f3910ad | 16 | int usb_baud = 115200; // USB Baud rate |
DEldering | 1:a8e61f3910ad | 17 | short res[8] = {0,0,0,0,0,0,0,0}; // 8 PR sensors 1 time per cycle |
DEldering | 1:a8e61f3910ad | 18 | short elec[5] = {0,0,0,0,0}; // 1 PE sensor 5 times per cycle |
DEldering | 1:a8e61f3910ad | 19 | int angle = 0; // Accelerometer Z-axis |
DEldering | 1:a8e61f3910ad | 20 | int k = 0; |
DEldering | 1:a8e61f3910ad | 21 | float acce[3]; // Raw accelerometer data |
DEldering | 1:a8e61f3910ad | 22 | float gyro[3]; // Raw gyroscope data |
DEldering | 1:a8e61f3910ad | 23 | bool r = 0; |
DEldering | 0:c0e44c46c573 | 24 | |
DEldering | 0:c0e44c46c573 | 25 | void read_adc() |
DEldering | 0:c0e44c46c573 | 26 | { |
DEldering | 1:a8e61f3910ad | 27 | t.reset(); |
DEldering | 1:a8e61f3910ad | 28 | t.start(); |
DEldering | 1:a8e61f3910ad | 29 | |
DEldering | 1:a8e61f3910ad | 30 | elec[0] = pel.readADC_SingleEnded(0); |
DEldering | 1:a8e61f3910ad | 31 | |
DEldering | 0:c0e44c46c573 | 32 | for (k = 0; k < 4; k = k + 1) { |
DEldering | 1:a8e61f3910ad | 33 | res[k] = pr1.readADC_SingleEnded(k); |
DEldering | 1:a8e61f3910ad | 34 | } |
DEldering | 1:a8e61f3910ad | 35 | while(t.read_us()<(1*(cycle_time/5))) {} |
DEldering | 1:a8e61f3910ad | 36 | |
DEldering | 1:a8e61f3910ad | 37 | elec[1] = pel.readADC_SingleEnded(0); |
DEldering | 1:a8e61f3910ad | 38 | |
DEldering | 1:a8e61f3910ad | 39 | for (k = 0; k < 4; k = k + 1) { |
DEldering | 1:a8e61f3910ad | 40 | res[k+4] = pr2.readADC_SingleEnded(k); |
DEldering | 0:c0e44c46c573 | 41 | } |
DEldering | 1:a8e61f3910ad | 42 | while(t.read_us()<(2*(cycle_time/5))) {} |
DEldering | 1:a8e61f3910ad | 43 | |
DEldering | 1:a8e61f3910ad | 44 | elec[2] = pel.readADC_SingleEnded(0); |
DEldering | 1:a8e61f3910ad | 45 | |
DEldering | 1:a8e61f3910ad | 46 | agu.getAccelero(acce); |
DEldering | 1:a8e61f3910ad | 47 | angle = acce[2]*10; |
DEldering | 1:a8e61f3910ad | 48 | |
DEldering | 1:a8e61f3910ad | 49 | while(t.read_us()<(3*(cycle_time/5))) {} |
DEldering | 1:a8e61f3910ad | 50 | |
DEldering | 1:a8e61f3910ad | 51 | elec[3] = pel.readADC_SingleEnded(0); |
DEldering | 1:a8e61f3910ad | 52 | |
DEldering | 1:a8e61f3910ad | 53 | agu.getGyro(gyro); |
DEldering | 1:a8e61f3910ad | 54 | |
DEldering | 1:a8e61f3910ad | 55 | while(t.read_us()<(4*(cycle_time/5))) {} |
DEldering | 1:a8e61f3910ad | 56 | |
DEldering | 1:a8e61f3910ad | 57 | elec[4] = pel.readADC_SingleEnded(0); |
DEldering | 1:a8e61f3910ad | 58 | |
DEldering | 1:a8e61f3910ad | 59 | while(t.read_us()<(4.5*(cycle_time/5))) {} |
DEldering | 1:a8e61f3910ad | 60 | pc.printf(",%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\r\n", res[4], res[7], res[6], res[5], res[1], res[0], res[2], res[3], angle, elec[0], elec[1], elec[2], elec[3], elec[4], acce[0]*100, acce[1]*100, acce[2]*100, gyro[0]*100, gyro[1]*100, gyro[2]*100); // print all to serial port |
DEldering | 0:c0e44c46c573 | 61 | } |
DEldering | 0:c0e44c46c573 | 62 | |
DEldering | 0:c0e44c46c573 | 63 | int main() |
DEldering | 0:c0e44c46c573 | 64 | { |
DEldering | 1:a8e61f3910ad | 65 | i2c.frequency(i2c_freq); |
DEldering | 1:a8e61f3910ad | 66 | pc.baud(usb_baud); |
DEldering | 1:a8e61f3910ad | 67 | pr1.setGain(GAIN_TWOTHIRDS); // set range to +/-6.144V |
DEldering | 1:a8e61f3910ad | 68 | pr2.setGain(GAIN_TWOTHIRDS); // set range to +/-6.144V |
DEldering | 1:a8e61f3910ad | 69 | pel.setGain(GAIN_TWOTHIRDS); // set range to +/-6.144V |
DEldering | 1:a8e61f3910ad | 70 | sample_cycle.attach_us(&read_adc, cycle_time); |
DEldering | 0:c0e44c46c573 | 71 | while (1) { |
DEldering | 1:a8e61f3910ad | 72 | wait_us(cycle_time+1); // wait indefinitely because the ticker restarts every 50 ms |
DEldering | 0:c0e44c46c573 | 73 | } |
DEldering | 0:c0e44c46c573 | 74 | } |