Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5

measure height of snow
#20

As promised here some more information about the laser range meter.
Attached you can find a protocol overview.

The following code sends the request to the laser device:

Code:
 //Define laser measurement on USART2 #define LASER_POWER_PIN 25 #define LASER_START_PIN 24 #define LASER_HEIGHT 1986 #define LASER_LATENCY 1999 //time for laser to answer in ms int16_t laser_distance_array[SNOW_ARRAYSIZE] = { -200, -200, -200, -200, -200, -200, -200, -200, -200, -200, -200, -200}; uint8_t laser_distance_array_ind = 0; uint8_t laser_wd; uint32_t LaserReqtime; uint8_t LaserFlag = 0; union laser_send_data_t {   struct bytes_t {     uint8_t preamble;     uint8_t command;     uint8_t data[3];     uint8_t check_code[2];   } bytes;   uint8_t array[sizeof(bytes)]; } laser_send_data; union laser_rcv_data_t {   struct bytes_t {     uint8_t preamble;     uint8_t command;     uint8_t result;     uint8_t data[3];     uint8_t check_code[2];   } bytes;   uint8_t array[sizeof(bytes)]; } laser_rcv_data; pinMode(LASER_POWER_PIN, OUTPUT);  pinMode(LASER_START_PIN, OUTPUT);  digitalWrite(LASER_POWER_PIN, HIGH);  digitalWrite(LASER_START_PIN, HIGH);  delay(50);  digitalWrite(LASER_START_PIN, LOW);  laser_send_data.bytes.preamble = 0xAA;  laser_send_data.bytes.command = 0x03;  Serial2.begin(38400);

Code:
  //Read distance with laser ---- data is retrieved at the end of callback_10s   laser_send_data.bytes.check_code[0] = (uint8_t)(laser_send_data.bytes.preamble + laser_send_data.bytes.command + laser_send_data.bytes.data[0] + laser_send_data.bytes.data[1] + laser_send_data.bytes.data[2]) << 8;   laser_send_data.bytes.check_code[1] = (uint8_t)(laser_send_data.bytes.preamble + laser_send_data.bytes.command + laser_send_data.bytes.data[0] + laser_send_data.bytes.data[1] + laser_send_data.bytes.data[2]);   Watchdog.reset(); //Reset global watchdog, just to make sure that it won't get trapped   Serial2.write(laser_send_data.array, sizeof(laser_send_data.array));   LaserReqtime = millis();   LaserFlag = 1;   //Check laser watchdog reset condition - if watchdog is not resetted within the data retrieving section reboot the laser distance sensor   if (laser_wd < 5) {     laser_wd++;   }   else {     digitalWrite(LASER_POWER_PIN, LOW);     delay(100);     digitalWrite(LASER_POWER_PIN, HIGH);     digitalWrite(LASER_START_PIN, HIGH);     delay(50);     digitalWrite(LASER_START_PIN, LOW);     laser_wd = 0;   }
After 2 seconds the results are polled from the uart rx buffer
Code:
///////////////////////////////////////////////////////////////////////////////// //////// Read values of laser distance sensor via USART 2 /////////////////////// ///////////////////////////////////////////////////////////////////////////////// void read_laser(void) {  //Get data of Laser distance measurement  Watchdog.reset(); //Reset global watchdog, just to make sure that it won't get trapped  //Try to get data from serial 2  if (Serial2.available() >= 8) {    laser_wd = 0;//Reset Watchdog of laser meter if data is received - device is resettet after 5 sending counts without an answer    //Get data from buffer    Serial2.readBytes(laser_rcv_data.array, sizeof(laser_rcv_data.array));    //Calculate checksum    uint16_t checksum = 0;    for (uint8_t i = 0; i < sizeof(laser_rcv_data.array) - 2; i++) {      checksum = checksum + laser_rcv_data.array[i];    }    //Calculate distance if command, result and checksum is ok    if ((laser_rcv_data.bytes.command == 0x03) && (laser_rcv_data.bytes.result == 0x06) && (checksum == (laser_rcv_data.bytes.check_code[0] << 8 | laser_rcv_data.bytes.check_code[1]))) {      int32_t distance_temp = ((int32_t)(laser_rcv_data.bytes.data[0] << 16) | (int32_t)(laser_rcv_data.bytes.data[1] << 8) | (int32_t)laser_rcv_data.bytes.data[2]);      if (distance_temp < 32000 && distance_temp > -1) {        laser_distance_array[laser_distance_array_ind] = (int16_t)distance_temp;        //Serial.println(laser_distance_array[laser_distance_array_ind]);      }      //Move array pointer      if (laser_distance_array_ind < SNOW_ARRAYSIZE) {        laser_distance_array_ind++;      }      else {        laser_distance_array_ind = 0;      }    }    //If laser answers with wrong values    /*      else {      laser_distance_array[laser_distance_array_ind] = -100;      //Serial.println(laser_distance_array[laser_distance_array_ind]);      //Move array pointer      if (laser_distance_array_ind < SNOW_ARRAYSIZE) {        laser_distance_array_ind++;      }      else {        laser_distance_array_ind = 0;      }      }    */    uint32_t uart_timeout = millis();    //Flush serial buffer if something is left for any reason and protect from endless looping    while (Serial2.available() > 0 && (millis() - uart_timeout < 500)) {      Serial2.read();      //Serial.println("Empty Buffer");    }  } }

To make it better the answer should be, as already described, received with an interrupt routine.
Reply


Messages In This Thread
measure height of snow - by f4aii - 08-06-2017, 06:10
RE: measure height of snow - by danner - 08-06-2017, 06:12
RE: measure height of snow - by f4aii - 06-11-2017, 05:49
RE: measure height of snow - by hornychz - 06-11-2017, 08:46
RE: measure height of snow - by f4aii - 11-11-2017, 14:30
RE: measure height of snow - by werk_ag - 12-11-2017, 01:38
RE: measure height of snow - by Barrow4491 - 12-11-2017, 23:05
RE: measure height of snow - by werk_ag - 12-11-2017, 23:37
RE: measure height of snow - by Barrow4491 - 13-11-2017, 04:39
RE: measure height of snow - by engolling - 11-12-2017, 22:56
RE: measure height of snow - by hornychz - 12-12-2017, 00:27
RE: measure height of snow - by f4aii - 12-12-2017, 06:34
RE: measure height of snow - by engolling - 12-12-2017, 22:17
RE: measure height of snow - by hornychz - 13-12-2017, 00:13
RE: measure height of snow - by werk_ag - 13-12-2017, 05:46
RE: measure height of snow - by werk_ag - 13-12-2017, 05:36
RE: measure height of snow - by hornychz - 13-12-2017, 06:25
RE: measure height of snow - by engolling - 13-12-2017, 09:45
RE: measure height of snow - by hornychz - 13-12-2017, 12:31
RE: measure height of snow - by werk_ag - 14-12-2017, 05:50
RE: measure height of snow - by engolling - 14-12-2017, 10:11
RE: measure height of snow - by AllyCat - 26-12-2017, 13:47
RE: measure height of snow - by engolling - 13-12-2017, 20:13
RE: measure height of snow - by engolling - 02-01-2018, 09:51
RE: measure height of snow - by engolling - 04-01-2018, 14:23
RE: measure height of snow - by hornychz - 04-01-2018, 14:28
RE: measure height of snow - by f4aii - 05-01-2018, 22:36
RE: measure height of snow - by engolling - 14-01-2018, 11:15
RE: measure height of snow - by werk_ag - 16-01-2018, 05:37
RE: measure height of snow - by danner - 14-01-2018, 21:37
RE: measure height of snow - by engolling - 03-02-2018, 10:43
RE: measure height of snow - by f4aii - 03-02-2018, 13:42
RE: measure height of snow - by engolling - 03-02-2018, 13:57
RE: measure height of snow - by f4aii - 03-02-2018, 16:03
RE: measure height of snow - by f4aii - 11-02-2018, 08:32
RE: measure height of snow - by AllyCat - 11-02-2018, 16:08
RE: measure height of snow - by engolling - 18-02-2018, 09:47
RE: measure height of snow - by engolling - 18-02-2018, 09:56
RE: measure height of snow - by engolling - 26-02-2018, 19:10
RE: measure height of snow - by engolling - 04-03-2018, 09:58
RE: measure height of snow - by f4aii - 05-03-2018, 06:53
RE: measure height of snow - by engolling - 05-03-2018, 21:57
RE: measure height of snow - by werk_ag - 05-03-2018, 22:24
RE: measure height of snow - by engolling - 05-03-2018, 22:39
RE: measure height of snow - by engolling - 06-03-2018, 22:19
RE: measure height of snow - by engolling - 20-03-2018, 22:25
RE: measure height of snow - by engolling - 09-04-2018, 20:22
RE: measure height of snow - by JT118 - 16-04-2018, 17:36
RE: measure height of snow - by engolling - 24-04-2018, 22:03
RE: measure height of snow - by hornychz - 25-04-2018, 14:09
RE: measure height of snow - by werk_ag - 24-04-2018, 22:31
RE: measure height of snow - by engolling - 26-04-2018, 21:45
RE: measure height of snow - by engolling - 05-06-2018, 23:37
RE: measure height of snow - by engolling - 17-07-2018, 21:39
RE: measure height of snow - by engolling - 25-07-2018, 20:26
RE: measure height of snow - by engolling - 03-10-2018, 23:09
RE: measure height of snow - by hornychz - 03-10-2018, 23:28
RE: measure height of snow - by engolling - 07-10-2018, 22:57
RE: measure height of snow - by engolling - 12-10-2018, 10:19
RE: measure height of snow - by engolling - 29-10-2018, 20:50
RE: measure height of snow - by hornychz - 02-11-2018, 18:44
RE: measure height of snow - by engolling - 07-11-2018, 23:20



Users browsing this thread: 1 Guest(s)