test of maxbotix analog read

Dependencies:   mbed

Committer:
scohennm
Date:
Wed Apr 19 18:04:48 2017 +0000
Revision:
0:6c3159d4d3af
test of maxbotix analog read

Who changed what in which revision?

UserRevisionLine numberNew contents of line
scohennm 0:6c3159d4d3af 1 #include "mbed.h"
scohennm 0:6c3159d4d3af 2 /* Set up as stete machine with case/switch formulation
scohennm 0:6c3159d4d3af 3 */
scohennm 0:6c3159d4d3af 4 // Stated
scohennm 0:6c3159d4d3af 5 #define IDLE 0
scohennm 0:6c3159d4d3af 6 #define PULSE 1
scohennm 0:6c3159d4d3af 7 #define READ 2
scohennm 0:6c3159d4d3af 8
scohennm 0:6c3159d4d3af 9 #define PRINTDBUG
scohennm 0:6c3159d4d3af 10
scohennm 0:6c3159d4d3af 11 #define WAITTIME 50 // ms -for time between data takes
scohennm 0:6c3159d4d3af 12 #define CONVERTTIME 50 // ms - for tiee for Maxbotix to convert range measurement
scohennm 0:6c3159d4d3af 13 #define NEARLIMIT 0.025
scohennm 0:6c3159d4d3af 14 #define FARLIMIT 0.40
scohennm 0:6c3159d4d3af 15 #define LEDON 1
scohennm 0:6c3159d4d3af 16 #define LEDOFF 0
scohennm 0:6c3159d4d3af 17 #define LEDFULLBRITE 0.0
scohennm 0:6c3159d4d3af 18 #define LEDNOBRITE 1.0
scohennm 0:6c3159d4d3af 19 #define HALFBRITE 0.5
scohennm 0:6c3159d4d3af 20 #define PULSE_ON 1
scohennm 0:6c3159d4d3af 21 #define PULSE_OFF 0
scohennm 0:6c3159d4d3af 22 #define RXWIDTH 100 // uSec
scohennm 0:6c3159d4d3af 23 #define RESETPULSE 100 // ms
scohennm 0:6c3159d4d3af 24 #define RESETTIME 20000 // ms
scohennm 0:6c3159d4d3af 25 #define MAXBOOTTIME 500 // mw
scohennm 0:6c3159d4d3af 26 #define PROGNAME "AnalogMAX v\r\n" // puTTY does not automatically add carriage return
scohennm 0:6c3159d4d3af 27
scohennm 0:6c3159d4d3af 28 AnalogIn input(PTC2);
scohennm 0:6c3159d4d3af 29 DigitalOut Relay(PTE29);
scohennm 0:6c3159d4d3af 30 DigitalOut RXRead(PTE23);
scohennm 0:6c3159d4d3af 31 DigitalOut PWRReset(PTE22);
scohennm 0:6c3159d4d3af 32
scohennm 0:6c3159d4d3af 33 // Set up PWM on all three colors
scohennm 0:6c3159d4d3af 34 PwmOut greenLED(LED_GREEN); // green led
scohennm 0:6c3159d4d3af 35 PwmOut blueLED(LED_BLUE);
scohennm 0:6c3159d4d3af 36 PwmOut redLED(LED_RED); // red led
scohennm 0:6c3159d4d3af 37 Serial pc(USBTX, USBRX);
scohennm 0:6c3159d4d3af 38 Timer millis;
scohennm 0:6c3159d4d3af 39 Timer resetTimer;
scohennm 0:6c3159d4d3af 40
scohennm 0:6c3159d4d3af 41 int ledState = false;
scohennm 0:6c3159d4d3af 42
scohennm 0:6c3159d4d3af 43 void RevPulseOut_ms(DigitalOut outport, int pulseWidth) {
scohennm 0:6c3159d4d3af 44 outport = PULSE_ON;
scohennm 0:6c3159d4d3af 45 outport = PULSE_OFF;
scohennm 0:6c3159d4d3af 46 wait_ms(pulseWidth);
scohennm 0:6c3159d4d3af 47 outport = PULSE_ON;
scohennm 0:6c3159d4d3af 48 return;
scohennm 0:6c3159d4d3af 49 }
scohennm 0:6c3159d4d3af 50 // --------------
scohennm 0:6c3159d4d3af 51 void pulseOut(DigitalOut outport, int pulseWidth) {
scohennm 0:6c3159d4d3af 52 outport = PULSE_OFF;
scohennm 0:6c3159d4d3af 53 outport = PULSE_ON;
scohennm 0:6c3159d4d3af 54 wait_us(pulseWidth);
scohennm 0:6c3159d4d3af 55 outport = PULSE_OFF;
scohennm 0:6c3159d4d3af 56 return;
scohennm 0:6c3159d4d3af 57 }
scohennm 0:6c3159d4d3af 58 // ---------------
scohennm 0:6c3159d4d3af 59 void takeData(float spanScale) {
scohennm 0:6c3159d4d3af 60
scohennm 0:6c3159d4d3af 61 float samples;
scohennm 0:6c3159d4d3af 62 float scaledPWM;
scohennm 0:6c3159d4d3af 63
scohennm 0:6c3159d4d3af 64
scohennm 0:6c3159d4d3af 65 samples = input.read();
scohennm 0:6c3159d4d3af 66 #ifdef PRINTDBUG
scohennm 0:6c3159d4d3af 67 pc.printf("%f\r\n", samples);
scohennm 0:6c3159d4d3af 68 #endif
scohennm 0:6c3159d4d3af 69 if ((samples > NEARLIMIT) && (samples < FARLIMIT)){
scohennm 0:6c3159d4d3af 70 scaledPWM = (samples - NEARLIMIT)*spanScale;
scohennm 0:6c3159d4d3af 71 redLED = 1.0 - scaledPWM;
scohennm 0:6c3159d4d3af 72 blueLED = scaledPWM;
scohennm 0:6c3159d4d3af 73 Relay = LEDON;
scohennm 0:6c3159d4d3af 74 #ifdef PRINTDBUG
scohennm 0:6c3159d4d3af 75 pc.printf("RED -> %f BLUE -> %f\r\n", redLED.read(), blueLED.read());
scohennm 0:6c3159d4d3af 76 #endif
scohennm 0:6c3159d4d3af 77 }else{
scohennm 0:6c3159d4d3af 78 redLED = LEDNOBRITE; // off
scohennm 0:6c3159d4d3af 79 blueLED = LEDNOBRITE;
scohennm 0:6c3159d4d3af 80 Relay = LEDOFF;
scohennm 0:6c3159d4d3af 81 }
scohennm 0:6c3159d4d3af 82 return;
scohennm 0:6c3159d4d3af 83 }
scohennm 0:6c3159d4d3af 84 // end takeData()
scohennm 0:6c3159d4d3af 85 // ----------------------------
scohennm 0:6c3159d4d3af 86 int main() {
scohennm 0:6c3159d4d3af 87 int currentState = IDLE;
scohennm 0:6c3159d4d3af 88 int stateTime = WAITTIME;
scohennm 0:6c3159d4d3af 89 float spanScale; // compute scale to expand to full range of PWM duty factors
scohennm 0:6c3159d4d3af 90
scohennm 0:6c3159d4d3af 91 // initialization
scohennm 0:6c3159d4d3af 92 // calulate scaling for red/blue LED
scohennm 0:6c3159d4d3af 93
scohennm 0:6c3159d4d3af 94 RXRead = PULSE_OFF;
scohennm 0:6c3159d4d3af 95 PWRReset = PULSE_ON;
scohennm 0:6c3159d4d3af 96 spanScale = 1.0/(FARLIMIT-NEARLIMIT);
scohennm 0:6c3159d4d3af 97 pc.printf(PROGNAME);
scohennm 0:6c3159d4d3af 98
scohennm 0:6c3159d4d3af 99 greenLED = HALFBRITE; // 1/2 on
scohennm 0:6c3159d4d3af 100 redLED = LEDNOBRITE; // pwm off
scohennm 0:6c3159d4d3af 101 blueLED = LEDFULLBRITE; // pwm on
scohennm 0:6c3159d4d3af 102 RevPulseOut_ms(PWRReset, RESETPULSE);
scohennm 0:6c3159d4d3af 103 millis.start();
scohennm 0:6c3159d4d3af 104 resetTimer.start();
scohennm 0:6c3159d4d3af 105 millis.reset();
scohennm 0:6c3159d4d3af 106 resetTimer.reset();
scohennm 0:6c3159d4d3af 107 // Bring Maxbotix RX line to 0 volts
scohennm 0:6c3159d4d3af 108 // Loop forever
scohennm 0:6c3159d4d3af 109 while(true){
scohennm 0:6c3159d4d3af 110
scohennm 0:6c3159d4d3af 111 switch (currentState) {
scohennm 0:6c3159d4d3af 112 case IDLE: {
scohennm 0:6c3159d4d3af 113 if (millis.read_ms() > stateTime) { // WAITTIME
scohennm 0:6c3159d4d3af 114 currentState = PULSE;
scohennm 0:6c3159d4d3af 115 }
scohennm 0:6c3159d4d3af 116 break;
scohennm 0:6c3159d4d3af 117 }
scohennm 0:6c3159d4d3af 118 case PULSE: {
scohennm 0:6c3159d4d3af 119 pulseOut(RXRead, RXWIDTH);
scohennm 0:6c3159d4d3af 120 millis.reset(); //reset timer to 0 for wait for convert time;
scohennm 0:6c3159d4d3af 121 stateTime = CONVERTTIME;
scohennm 0:6c3159d4d3af 122 currentState = READ;
scohennm 0:6c3159d4d3af 123 break;
scohennm 0:6c3159d4d3af 124 }
scohennm 0:6c3159d4d3af 125 case READ: {
scohennm 0:6c3159d4d3af 126 if (millis.read_ms() > stateTime) { // PULSETIME
scohennm 0:6c3159d4d3af 127 takeData(spanScale);
scohennm 0:6c3159d4d3af 128 currentState = IDLE;
scohennm 0:6c3159d4d3af 129 stateTime = WAITTIME;
scohennm 0:6c3159d4d3af 130 millis.reset();
scohennm 0:6c3159d4d3af 131 }
scohennm 0:6c3159d4d3af 132 break;
scohennm 0:6c3159d4d3af 133 }
scohennm 0:6c3159d4d3af 134 }
scohennm 0:6c3159d4d3af 135 /* Does not work yet */
scohennm 0:6c3159d4d3af 136 if(resetTimer.read_ms() > RESETTIME){ // recalibrate the Maxbotix
scohennm 0:6c3159d4d3af 137 RevPulseOut_ms(PWRReset, RESETPULSE);
scohennm 0:6c3159d4d3af 138 wait_ms(MAXBOOTTIME);
scohennm 0:6c3159d4d3af 139 resetTimer.reset();
scohennm 0:6c3159d4d3af 140 }
scohennm 0:6c3159d4d3af 141 /* --------------- */
scohennm 0:6c3159d4d3af 142 }
scohennm 0:6c3159d4d3af 143 } // end main