Program to read maxbotix sensor for KL46Z

Dependencies:   mbed

Committer:
scohennm
Date:
Tue Sep 30 22:36:50 2014 +0000
Revision:
0:55be60794fc3
Program to read maxbotix sensor for KL46Z

Who changed what in which revision?

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