Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed Watchdog stm32-sensor-base2
Diff: main.cpp
- Revision:
- 8:c3cffab85b0d
- Parent:
- 7:2f218add711e
- Child:
- 9:859bcb293e46
--- a/main.cpp Thu Jul 23 11:34:08 2020 +0000 +++ b/main.cpp Tue Jul 28 14:25:33 2020 +0000 @@ -1,19 +1,18 @@ #include <global.h> -//Serial pc(UART1_TX, UART1_RX); RS485 RS485(UART2_TX,UART2_RX,DE_TXD_2); // Tx, Rx , !RE and DE MAX485 pin -Serial pc(USBTX, USBRX); +volatile bool serialArrived = false; +int n=0; +char a; +bool send_flag = false; + Timer timer; typedef uint8_t byte; -DigitalOut select(DE_TXD_2); -byte full_value[9]; -byte base_data[4] = {0xAA,0x55,0x03,0x01}; -byte data[9] = {0x01,0x04,0x00,0x48,0x00,0x02,0xf1,0xdd};//your data - -char test_buffer[10]; - +DigitalOut select(DE_TXD_1); +byte regvalue[1] ; + byte data[6]; const unsigned char CRC7_POLY = 0x91; unsigned char getCRC(unsigned char message[], unsigned char length) @@ -32,94 +31,50 @@ } return crc; } - - int main() { - DigitalOut led(DEBUG_LED); - DigitalOut led2(DE_TXD_1, 1); // activate transmitting rs485-1 - - -#if TEST_ULTRASONIC - int time_to_check_us = 1000; - JSN_SR04 sensor1(TIM1_CH2, TRIG_PA8_OUT); -// JSN_SR04 sensor1(TIM1_CH2, TRIG_PB14_OUT); //no triger signal -// JSN_SR04 sensor1(TIM1_CH2, TRIG_PB15_OUT); - sensor1.setRanges(30, 200); -// sensor2.setRanges(30, 200); -// sensor3.setRanges(30, 200); -// pc.printf("Min. range = %g cm\n\r",sensor1.getMinRange()); - while(true) { - timer.reset(); - timer.start(); - sensor1.startMeasurement(); - //while (sensor1.isNewDataReady() == false && timer.read_ms() < time_to_check_us) { - //pc.printf("Distance1: %5.1f mm -- Distance2: %5.1f mm -- -- Distance3: %5.1f mm \r\n", sensor1.getDistance_cm()), sensor2.getDistance_cm(), sensor3.getDistance_cm()); - pc.printf("Distance1: %5.1f mm \r\n", sensor1.getDistance_cm()); - - - timer.stop(); - wait_ms(2); - - } - -#endif - -#if TEST_ENCODERS -int min1_value = 2000; -int max1_value = 0; - AS5045 encoder_1(SP1_NSS1); - AS5045 encoder_2(SP1_NSS2); - while(1) { - led = !led; - double encoder_1_Value = encoder_1.getPosition(); - wait_ms(30); - double encoder_2_Value = encoder_2.getPosition(); - wait_ms(30); - //encoder_1_Value = - //if( encoder_1_Value.IsValid() && encoder_2_Value.IsValid() ) +# if DEMO_CODE - pc.printf("Sensor Values = %f --- %f \n\r",encoder_1_Value, encoder_2_Value ); // - //else - //pc.printf("Invalid data read"); - - //wait_ms(50); - } - -#endif - -# if TEST_IR -PwmIn a(PB_4); - //E18_D80NK infared1 (IR1_PB12_OUT); -// E18_D80NK infared2 (IR2_PB13_OUT); - while (true) { - // pc.printf ("Is there any obstacle: %d ---- %d ", infared1.checkObstacle(),infared2.checkObstacle() ); - - pc.printf ("\n\r"); - } - - -#endif - -# if DEMO_CODE - JSN_SR04 sensor1(TIM1_CH2, TRIG_PA8_OUT); E18_D80NK infared1 (IR1_PB12_OUT); E18_D80NK infared2 (IR2_PB13_OUT); - pc.baud(115200); + + select = 1; sensor1.setRanges(10, 200); - while(true) { - timer.reset(); - timer.start(); - sensor1.startMeasurement(); - //while (sensor1.isNewDataReady() == false && timer.read_ms() < time_to_check_us) { - //pc.printf("Distance1: %5.1f mm -- Distance2: %5.1f mm -- -- Distance3: %5.1f mm \r\n", sensor1.getDistance_cm()), sensor2.getDistance_cm(), sensor3.getDistance_cm()); - pc.printf("%5.1f_%d_%d \r\n", sensor1.getDistance_cm(),infared1.checkObstacle(),infared2.checkObstacle()); + memset(data,0,sizeof(data)); + float distance; + + while(true) { + if(RS485.readable() >0){ + //memset(regvalue,0,sizeof(regvalue)); + //wait_ms(100); + RS485.recvMsg(regvalue,sizeof(uint8_t),30); + } + //printf("Done\n"); - timer.stop(); - wait_ms(20); + if((int)regvalue[1] == 4 ) + { + select = 1; + timer.reset(); + timer.start(); + sensor1.startMeasurement(); + + distance = sensor1.getDistance_cm(); + data[0] = (uint8_t)(distance >> 24) ; + data[1] = (uint8_t)(distance >> 16) ; + data[2] = (uint8_t)(distance >> 8) ; + data[3] = (uint8_t)distance ; + data[4] = (uint8_t)infared1.checkObstacle(); + data[5] = (uint8_t)infared2.checkObstacle(); + RS485.sendMsg(data,sizeof(data)); + timer.stop(); + send_flag = false; + wait_ms(20); + + select = 0 ; + } }