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:
- 12:406f75196a12
- Parent:
- 10:0b7f23df690a
- Child:
- 13:0c732e06d675
--- a/main.cpp Wed Aug 26 14:26:27 2020 +0530 +++ b/main.cpp Sun Dec 06 11:05:19 2020 +0000 @@ -1,185 +1,167 @@ #include <global.h> - + //RS485 RS485(UART2_TX,UART2_RX,DE_TXD_2); // Tx, Rx , !RE and DE MAX485 pin - Serial pc(UART2_TX, UART2_RX); -volatile bool serialArrived = false; -int n=0; -char a; -bool send_flag = false; - +Serial RS2(UART2_TX, UART2_RX); +Serial RS1(UART1_TX, UART1_RX); + Timer timer; typedef uint8_t byte; - -DigitalOut select(DE_TXD_2); + +DigitalOut Select1(DE_TXD_1); +DigitalOut Select2(DE_TXD_2); + + -byte regvalue[1] ; - byte data[6]; - - byte data_t[9] = {0x02,0x05,0x01,0x49,0x01,0x03,0xf2,0xde};//your data - +bool sendFlag = false; +bool sendFlag1 = false; +int cmd[13] = {0xA5,0x40,0x90,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7D}; +char inChar[52]; +int index = 0; +char c = '1'; const unsigned char CRC7_POLY = 0x91; - + unsigned char getCRC(unsigned char message[], unsigned char length) { - unsigned char i, j, crc = 0; - - for (i = 0; i < length; i++) - { - crc ^= message[i]; - for (j = 0; j < 8; j++) - { - if (crc & 1) - crc ^= CRC7_POLY; - crc >>= 1; + unsigned char i, j, crc = 0; + + for (i = 0; i < length; i++) { + crc ^= message[i]; + for (j = 0; j < 8; j++) { + if (crc & 1) + crc ^= CRC7_POLY; + crc >>= 1; + } } - } - return crc; + return crc; + } + +void UART2_callback() +{ + c = RS2.getc(); + if(c == '4') { + sendFlag = true; + } else { + Select2 = 1; + wait_ms(100); + Select2 = 0; + } + +} + int main() { -//# if DEMO1_CODE -// -// JSN_SR04 sensor1(TIM1_CH2, TRIG_PA8_OUT); -// E18_D80NK infared1 (IR1_PB12_OUT); -// E18_D80NK infared2 (IR2_PB13_OUT); -// -// OneWire oneWire(D8); // substitute D8 with the actual pin name connected to the 1-wire bus -// int sensorsFound = 0; -// -// -// select =0; -// -// sensor1.setRanges(10, 200); -// -// memset(data,0,sizeof(data)); -// int distance; -// regvalue[0] = 1; -// byte in; -// -// while(true) { -// -// //memset(regvalue,0,sizeof(regvalue)); -// //wait_ms(100); -// //RS485.recvMsg(regvalue,sizeof(regvalue),500); -// -// //printf("Done\n"); -// if (RS485.readable() > 0) -// { -// -// in = RS485.getc(); -// -// } -// -// if(in == '\4') -// { -// -// timer.reset(); -// timer.start(); -// sensor1.startMeasurement(); -// -// distance = sensor1.getDistance_mm(); -// data[0] = (distance >> 24) ; -// data[1] =(distance >> 16) ; -// data[2] = (distance >> 8) ; -// data[3] = distance ; -// data[4] = infared1.checkObstacle(); -// data[5] = infared2.checkObstacle(); -// select = 1; -// RS485.sendMsg(data,sizeof(data)); -// wait_ms(20); -// select = 0 ; -// timer.stop(); -// send_flag = false; -// wait_ms(20); -// in = '\1'; -// -// -// } -// -// } -//#endif + RS2.baud (115200); + Select1 = 0; + Select2 = 0; + RS2.attach(&UART2_callback); + //RS1.attach(&UART1_callback); + +#if DEMO_CODE + int US1_data = 0,US2_data = 0, US3_data = 0; + + int Lift_IR1 = 0,Lift_IR2 = 0; + + bool IR1_data = 0,IR2_data = 0; + + float temp_one_value = -1, temp_two_value = -1, Enc_left_data = 0, Enc_right_data = 0, time = 0; + + Timer timer1; + timer1.start(); -# if DEMO_CODE - - int US1_data = 0,US2_data = 0, US3_data = 0; - - int Lift_IR1 = 0,Lift_IR2 = 0; - - bool IR1_data = 0,IR2_data = 0; - - float temp_one_value = -1 , temp_two_value = -1 ; - - - - OneWire oneWire(PA_11); // substitute D8 with the actual pin name connected to the 1-wire bus + OneWire oneWire(PA_11); // substitute D8 with the actual pin name connected to the 1-wire bus int sensorsFound = 0; - - AnalogIn Lift1 (PB_0); - AnalogIn Lift2 (PB_1); - JSN_SR04 US_sensor_left (PB_14, PA_9); - JSN_SR04 US_sensor_middle (PB_15, PA_9); - JSN_SR04 US_sensor_right (PA_8, PA_9); - - US_sensor_left.setRanges (20, 300); - US_sensor_middle.setRanges (20, 300); - US_sensor_right.setRanges (20, 300); - - E18_D80NK IR_sensor_left (PB_13); - E18_D80NK IR_sensor_right (PB_12); - - for (sensorsFound = 0; sensorsFound < MAX_SENSOSRS; sensorsFound++) { + + DigitalIn Lift1 (PB_0); + DigitalIn Lift2 (PB_1); + + AS5045 Enc_left(SP1_NSS1); + AS5045 Enc_right(SP1_NSS2); + + JSN_SR04 US_sensor_left (PB_14, PA_9); + JSN_SR04 US_sensor_middle (PB_15, PA_9); + JSN_SR04 US_sensor_right (PA_8, PA_9); + + US_sensor_left.setRanges (20, 300); + US_sensor_middle.setRanges (20, 300); + US_sensor_right.setRanges (20, 300); + + E18_D80NK IR_sensor_left (PB_13); + E18_D80NK IR_sensor_right (PB_12); + + for (sensorsFound = 0; sensorsFound < MAX_SENSOSRS; sensorsFound++) { ds1820[sensorsFound] = new DS1820(&oneWire); if (!ds1820[sensorsFound]->begin()) { delete ds1820[sensorsFound]; break; } } - - pc.baud (115200); - select = 1; - - while (true) { - - for (int i = 0; i < sensorsFound; i++) - { + + while (true) { + timer1.reset(); + + for (int i = 0; i < sensorsFound; i++) { ds1820[i]->startConversion(); - wait_ms(100); + wait_ms(1); //temp_one_value = ds1820[i]->read(); //temp_two_value = i; } - - US_sensor_left.startMeasurement (); - US1_data = US_sensor_left.getDistance_cm (); - - US_sensor_middle.startMeasurement (); - US2_data = US_sensor_middle.getDistance_cm (); - - US_sensor_right.startMeasurement (); - US3_data = US_sensor_right.getDistance_cm (); - - IR1_data = IR_sensor_left.checkObstacle (); - IR2_data = IR_sensor_right.checkObstacle (); - - Lift_IR1 = Lift1.read () <= 0.53? 1: 0; - Lift_IR2 = Lift2.read () <= 0.53? 1: 0; - - - if (ds1820[0]->isPresent() ){ - //pc.printf("temp[%d] = %3.1f%cC\r\n", 0, ds1820[0]->read(), 176); // read temperature - temp_one_value = ds1820[0]->read(); - } - if (ds1820[1]->isPresent() ){ - //pc.printf("temp[%d] = %3.1f%cC\r\n", 1, ds1820[1]->read(), 176); // read temperature - temp_two_value = ds1820[1]->read(); - } - - pc.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)); - - wait_ms (100); - } - + + US_sensor_left.startMeasurement (); + US1_data = US_sensor_left.getDistance_cm (); + wait_ms(15); + US_sensor_middle.startMeasurement (); + US2_data = US_sensor_middle.getDistance_cm (); + wait_ms(15); + US_sensor_right.startMeasurement (); + US3_data = US_sensor_right.getDistance_cm (); + wait_ms(15); + IR1_data = IR_sensor_left.checkObstacle (); + IR2_data = IR_sensor_right.checkObstacle (); + Enc_left_data = Enc_left.getAngle(); + Enc_right_data = Enc_right.getAngle(); + + Lift_IR1 = Lift1.read(); + Lift_IR2 = Lift2.read(); + + time = timer1.read(); + + if (ds1820[0]->isPresent() ) { + //pc.printf("temp[%d] = %3.1f%cC\r\n", 0, ds1820[0]->read(), 176); // read temperature + temp_one_value = ds1820[0]->read(); + } + if (ds1820[1]->isPresent() ) { + //pc.printf("temp[%d] = %3.1f%cC\r\n", 1, ds1820[1]->read(), 176); // read temperature + temp_two_value = ds1820[1]->read(); + } + //if(sendFlag) { + Select2 = 1; + RS2.printf ("%d_%d_%d_%d_%d_%d_%d_%3.1f_%3.1f_%3.1f_%3.1f_%f\r\n", US1_data, US2_data, US3_data, IR1_data, IR2_data, Lift_IR1, Lift_IR2, temp_one_value, temp_two_value, Enc_right_data, Enc_left_data, time); //,(int)((Vcc -24 ) * 5 + 50)); + wait_ms(5); + sendFlag = false; + Select2 = 0; + // } + } #endif - + +#if TEST_REQUEST + + while (true) { + // if(RS1.readable()) { +// Select1 = 1; +// RS1.putc(RS1.getc()); +// wait_ms (1000); +// Select1 = 0; +// }else{ + Select1 = 1; + RS1.printf ("nothing \n"); + wait_ms (5); + Select1 = 0; + wait_ms (1000); + //} + } + +#endif } \ No newline at end of file