Руслан Бредун / Mbed 2 deprecated stm32-sensor-base2

Dependencies:   mbed Watchdog

Dependents:   STM32-MC_node

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include <global.h>
00002 
00003 Serial RS2(UART2_TX, UART2_RX);
00004 Serial RS1(UART1_TX, UART1_RX);
00005 
00006 Timer timer;
00007 Watchdog  watchDog;
00008 
00009 typedef uint8_t byte;
00010 
00011 DigitalOut Select1(DE_TXD_1);
00012 DigitalOut Select2(DE_TXD_2);
00013 
00014 bool sendFlag = false;
00015 char c = '1';
00016 
00017 // ----------------------------- For future ---------------------------------------
00018 // Counting CRC for modbus package
00019 //const unsigned char CRC7_POLY = 0x91;
00020 
00021 //unsigned char getCRC(unsigned char message[], unsigned char length)
00022 //{
00023 //    unsigned char i, j, crc = 0;
00024 //
00025 //    for (i = 0; i < length; i++) {
00026 //        crc ^= message[i];
00027 //        for (j = 0; j < 8; j++) {
00028 //            if (crc & 1)
00029 //                crc ^= CRC7_POLY;
00030 //            crc >>= 1;
00031 //        }
00032 //    }
00033 //    return crc;
00034 //
00035 //}
00036 
00037 void UART2_callback()
00038 {
00039     c =  RS2.getc();
00040     if(c == '5') {
00041         sendFlag = true;
00042     } else {
00043         Select2 = 1;
00044         wait_ms(90);
00045         Select2 = 0;
00046     }
00047 }
00048 
00049 int main()
00050 {
00051     RS2.baud (115200);
00052     RS1.baud (115200);
00053     Select1 = 0;
00054     Select2 = 0;
00055     RS2.attach(&UART2_callback);
00056 
00057 #if MAIN_CODE
00058     int US1_data = 0,US2_data = 0, US3_data = 0, Lift_IR1 = 0,Lift_IR2 = 0, Enc_left_data = 0, Enc_right_data = 0;
00059     bool IR1_data = 0,IR2_data = 0;
00060     float temp_one_value = -1, temp_two_value = -1, currentTime = 0, previousTime = 0;
00061     
00062     timer.start();
00063 
00064     watchDog.Configure(5.0);
00065 
00066     OneWire oneWire(PA_11);        // substitute D8 with the actual pin name connected to the 1-wire bus
00067     int sensorsFound = 0;
00068 
00069     DigitalIn  Lift1 (PB_0);
00070     DigitalIn  Lift2 (PB_1);
00071 
00072     AS5045 Enc_left(SP1_NSS1);
00073     AS5045 Enc_right(SP1_NSS2);
00074 
00075     JSN_SR04 US_sensor_left (PB_14, PA_9);
00076     JSN_SR04 US_sensor_middle (PB_15, PA_9);
00077     JSN_SR04 US_sensor_right (PA_8, PA_9);
00078 
00079     US_sensor_left.setRanges (20, 300);
00080     US_sensor_middle.setRanges (20, 300);
00081     US_sensor_right.setRanges (20, 300);
00082 
00083     E18_D80NK IR_sensor_left (PB_13);
00084     E18_D80NK IR_sensor_right (PB_12);
00085 
00086     for (sensorsFound = 0; sensorsFound < MAX_SENSOSRS; sensorsFound++) {
00087         ds1820[sensorsFound] = new DS1820(&oneWire);
00088         if (!ds1820[sensorsFound]->begin()) {
00089             delete ds1820[sensorsFound];
00090             break;
00091         }
00092     }
00093 
00094     while (true) {
00095         watchDog.Service();
00096         
00097         currentTime = timer.read();
00098 
00099         if((currentTime - previousTime) >= 0.1) {
00100             previousTime = currentTime;
00101             
00102             for (int i = 0; i < sensorsFound; i++) {
00103                 ds1820[i]->startConversion();
00104                 wait_ms(1);
00105             }
00106 
00107             US_sensor_left.startMeasurement ();
00108             US1_data = US_sensor_left.getDistance_cm ();
00109             wait_ms(20);
00110             
00111             US_sensor_middle.startMeasurement ();
00112             US2_data = US_sensor_middle.getDistance_cm ();
00113             wait_ms(20);
00114             
00115             US_sensor_right.startMeasurement ();
00116             US3_data = US_sensor_right.getDistance_cm ();
00117             wait_ms(20);
00118             
00119             IR1_data = IR_sensor_left.checkObstacle ();
00120             IR2_data = IR_sensor_right.checkObstacle ();
00121             
00122             Lift_IR1 = Lift1.read();
00123             Lift_IR2 = Lift2.read();
00124 
00125 
00126             if (ds1820[0]->isPresent() ) {
00127                 temp_one_value = ds1820[0]->read();
00128             }
00129             if (ds1820[1]->isPresent() ) {
00130                 temp_two_value = ds1820[1]->read();
00131             }
00132         }
00133 
00134         Enc_left_data = Enc_left.getPosition();
00135         Enc_right_data = Enc_right.getPosition();
00136         
00137         Select1 = 1;
00138         RS1.printf ("%d\r\n", Enc_left_data);//, Enc_right_data);
00139         wait_ms(1);
00140         Select1 = 0;
00141 
00142         if(sendFlag) {
00143             Select2 = 1;
00144             RS2.printf ("%d_%d_%d_%d_%d_%d_%d_%3.1f_%3.1f\r\n", US1_data, US2_data, US3_data, IR1_data, IR2_data, Lift_IR1, Lift_IR2, temp_one_value, temp_two_value);  //,(int)((Vcc -24 ) * 5 + 50));
00145             wait_ms(5);
00146             sendFlag = false;
00147             Select2 = 0;
00148         }
00149     }
00150 #endif
00151 
00152 #if TEST_REQUEST
00153 
00154     while (true) {
00155         // if(RS1.readable()) {
00156 //            Select1 = 1;
00157 //            RS1.putc(RS1.getc());
00158 //            wait_ms (1000);
00159 //            Select1 = 0;
00160 //        }else{
00161         Select1 = 1;
00162         RS1.printf ("nothing \n");
00163         wait_ms (5);
00164         Select1 = 0;
00165         wait_ms (1000);
00166         //}
00167     }
00168 
00169 #endif
00170 
00171 }