Test for MS5803 and MS5837 pressure sensors (pressure and temp readings) and QEI testing for encoder.

Dependencies:   MS5803 QEI mbed

Committer:
alex93
Date:
Sun Mar 20 03:05:25 2016 +0000
Revision:
0:f8bc804eadbc
Test for MS5803 and MS5837 pressure sensors (pressure and temp readings) and QEI testing for encoder.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
alex93 0:f8bc804eadbc 1 /*********************************************************************************
alex93 0:f8bc804eadbc 2 * This code initializes the MS5803 and MS5837 pressure sensors on pins used
alex93 0:f8bc804eadbc 3 * during testing (in March 2016 version of fish, the Open ROV IMU/Pressure Sensor
alex93 0:f8bc804eadbc 4 * is connected to p28/p27, whereas this code uses both p28/p27 and p9/p10). It
alex93 0:f8bc804eadbc 5 * constantly prints out the pressure and temperature readings of the two sensors.
alex93 0:f8bc804eadbc 6 * It also establishes a QEI object on pins 25 and 24 from the encoder and
alex93 0:f8bc804eadbc 7 * constantly prints the revolutions.
alex93 0:f8bc804eadbc 8 *********************************************************************************/
alex93 0:f8bc804eadbc 9
alex93 0:f8bc804eadbc 10 #include "mbed.h"
alex93 0:f8bc804eadbc 11 #include "MS5803.h"
alex93 0:f8bc804eadbc 12 #include "MS5837.h"
alex93 0:f8bc804eadbc 13 #include "QEI.h"
alex93 0:f8bc804eadbc 14 using namespace std;
alex93 0:f8bc804eadbc 15
alex93 0:f8bc804eadbc 16 Serial pc(USBTX, USBRX);
alex93 0:f8bc804eadbc 17
alex93 0:f8bc804eadbc 18 // Motor Driver
alex93 0:f8bc804eadbc 19 AnalogIn pot(p15);
alex93 0:f8bc804eadbc 20 PwmOut enable(p26);
alex93 0:f8bc804eadbc 21
alex93 0:f8bc804eadbc 22 // Hall Sensor
alex93 0:f8bc804eadbc 23 InterruptIn hallVoltage(p8);
alex93 0:f8bc804eadbc 24 DigitalOut led2(LED2);
alex93 0:f8bc804eadbc 25
alex93 0:f8bc804eadbc 26 // QEI
alex93 0:f8bc804eadbc 27 QEI wheel(p25, p24, NC, 12); // 12=counts per revolution of motor shaft for the encoder
alex93 0:f8bc804eadbc 28
alex93 0:f8bc804eadbc 29 void flip() {
alex93 0:f8bc804eadbc 30 led2 = !led2;
alex93 0:f8bc804eadbc 31 }
alex93 0:f8bc804eadbc 32
alex93 0:f8bc804eadbc 33 int main() {
alex93 0:f8bc804eadbc 34 // set serial transfer rate
alex93 0:f8bc804eadbc 35 pc.baud(9600);
alex93 0:f8bc804eadbc 36
alex93 0:f8bc804eadbc 37 // Initialize big pressure sensor
alex93 0:f8bc804eadbc 38 MS5803 big_sensor = MS5803(p28, p27, ms5803_addrCH);
alex93 0:f8bc804eadbc 39 big_sensor.MS5803Init();
alex93 0:f8bc804eadbc 40 float pressure_big;
alex93 0:f8bc804eadbc 41 float temp_big;
alex93 0:f8bc804eadbc 42
alex93 0:f8bc804eadbc 43 // Initialize small pressure sensor
alex93 0:f8bc804eadbc 44 MS5837 small_sensor = MS5837(p9, p10, ms5837_addr_no_CS);
alex93 0:f8bc804eadbc 45 small_sensor.MS5837Init();
alex93 0:f8bc804eadbc 46 float pressure_small;
alex93 0:f8bc804eadbc 47 float temp_small;
alex93 0:f8bc804eadbc 48
alex93 0:f8bc804eadbc 49 // Motor & QEI initialization
alex93 0:f8bc804eadbc 50 wheel.reset();
alex93 0:f8bc804eadbc 51 enable.period(.000001);
alex93 0:f8bc804eadbc 52 int count = 0;
alex93 0:f8bc804eadbc 53 int previousHallVoltage = 0;
alex93 0:f8bc804eadbc 54
alex93 0:f8bc804eadbc 55 while(1) {
alex93 0:f8bc804eadbc 56 hallVoltage.rise(&flip);
alex93 0:f8bc804eadbc 57 hallVoltage.fall(&flip);
alex93 0:f8bc804eadbc 58
alex93 0:f8bc804eadbc 59 // Update big pressure sensor
alex93 0:f8bc804eadbc 60 big_sensor.Barometer_MS5803();
alex93 0:f8bc804eadbc 61 pressure_big = big_sensor.MS5803_Pressure();
alex93 0:f8bc804eadbc 62 temp_big = big_sensor.MS5803_Temperature();
alex93 0:f8bc804eadbc 63 pc.printf("Big Sensor Pressure: %f\n", pressure_big);
alex93 0:f8bc804eadbc 64 pc.printf("Big Sensor Temperature: %f\n", temp_big);
alex93 0:f8bc804eadbc 65
alex93 0:f8bc804eadbc 66 // Update small pressure sensor
alex93 0:f8bc804eadbc 67 small_sensor.Barometer_MS5837();
alex93 0:f8bc804eadbc 68 pressure_small = small_sensor.MS5837_Pressure();
alex93 0:f8bc804eadbc 69 temp_small = small_sensor.MS5837_Temperature();
alex93 0:f8bc804eadbc 70 pc.printf("Small Sensor Pressure: %f\n", pressure_small);
alex93 0:f8bc804eadbc 71 pc.printf("Small Sensor Temperature: %f\n\n", temp_small);
alex93 0:f8bc804eadbc 72
alex93 0:f8bc804eadbc 73 // // Simply feedback loop for speed, changing values every 5 revolutions as detected by hall sensor
alex93 0:f8bc804eadbc 74 // if (count < 5) {
alex93 0:f8bc804eadbc 75 // enable = pot; //PWM enable output is proportional to analog pot input
alex93 0:f8bc804eadbc 76 // } else if (count < 10) {
alex93 0:f8bc804eadbc 77 // enable = pot/2;
alex93 0:f8bc804eadbc 78 // } else {
alex93 0:f8bc804eadbc 79 // enable = pot;
alex93 0:f8bc804eadbc 80 // count = 0;
alex93 0:f8bc804eadbc 81 // }
alex93 0:f8bc804eadbc 82 // if (previousHallVoltage==1 && hallVoltage==0) {
alex93 0:f8bc804eadbc 83 // count += 1;
alex93 0:f8bc804eadbc 84 // pc.printf("\n\n----END OF REVOLUTION----\nNumber of pulses: %i\n", wheel.getPulses());
alex93 0:f8bc804eadbc 85 // wheel.reset();
alex93 0:f8bc804eadbc 86 // }
alex93 0:f8bc804eadbc 87 // if (hallVoltage) {
alex93 0:f8bc804eadbc 88 // led2 = 1;
alex93 0:f8bc804eadbc 89 // previousHallVoltage = 1;
alex93 0:f8bc804eadbc 90 // } else {
alex93 0:f8bc804eadbc 91 // led2 = 0;
alex93 0:f8bc804eadbc 92 // previousHallVoltage = 0;
alex93 0:f8bc804eadbc 93 // }
alex93 0:f8bc804eadbc 94
alex93 0:f8bc804eadbc 95 pc.printf("Pulses is: %i\n", wheel.getPulses());
alex93 0:f8bc804eadbc 96 }
alex93 0:f8bc804eadbc 97 }