Issue with HC-SR04 Rangefinder

if its mavlink errors you will need to add the mavlink files to the same folder as the ino sketch.

extract the mavlink folder and place it in the same folder as your Arduino sketch

there is a test version of the sketch that will output values to serial monitor so you can see if your getting the data without the flight controller

Ok, so i got 4 sensors. One of them (the L1X one) Can’t be plugged in, because everytime i plug it in into anything, it just makes everything stuck on boot loop or smth. so we are left with 3 sensors in total. 2 of them (one L1X and one L0X) are not responding at all while uploading the 2nd code without the FC. The only sensors that works is the second L0X, it’s the only sensor working correctly right now. These two (L1X and L0X) probbably work too (but not at this moment), but not so much luck with the second L1X, i think this one is broken…

that sketch is for the vl53l0x so it might need modified to work with the VL53l1x change this at the start to make it work

add

#include <VL53L1X.h>

change this part of the code

 Sensor1.init();
  Sensor2.init();
  Sensor3.init();
  Sensor4.init();

  
  Sensor1.setTimeout(500);
  Sensor2.setTimeout(500);
  Sensor3.setTimeout(500);
  Sensor4.setTimeout(500);



  Sensor1.startContinuous();
  Sensor2.startContinuous();
  Sensor3.startContinuous();
  Sensor4.startContinuous();


float Sensor1Smooth = Sensor1.readRangeContinuousMillimeters();
  float Sensor1Filter = constrain(Sensor1Smooth, MIN , MAX);
   
  float Sensor2Smooth = Sensor2.readRangeContinuousMillimeters();
  float Sensor2Filter constrain(Sensor2Smooth, MIN , MAX);
  
  float Sensor3Smooth = Sensor3.readRangeContinuousMillimeters();
  float Sensor3Filter = constrain(Sensor3Smooth, MIN, MAX);
   
  float Sensor4Smooth = Sensor4.readRangeContinuousMillimeters();
  float Sensor4Filter constrain(Sensor4Smooth, MIN , MAX);

to this

  Sensor1.init();
  Sensor2.init();
  Sensor3.init();
  Sensor4.init();


    Serial.println ("init");
  
  Sensor1.setTimeout(idle);
  Sensor2.setTimeout(idle);
  Sensor3.setTimeout(idle);
  Sensor4.setTimeout(idle);

  Serial.println ("timeout");


//set long distance mode
  Sensor1.setDistanceMode(VL53L1X::Long);
  Sensor2.setDistanceMode(VL53L1X::Long);
  Sensor3.setDistanceMode(VL53L1X::Long);
  Sensor4.setDistanceMode(VL53L1X::Long);
   Serial.println ("distance mode");

//set timing budget to 50ms
  Sensor1.setMeasurementTimingBudget(50000);
  Sensor2.setMeasurementTimingBudget(50000);
  Sensor3.setMeasurementTimingBudget(50000);
  Sensor4.setMeasurementTimingBudget(50000);
  Serial.println ("timing budget");
}


  float Sensor1Smooth  = Sensor1.readRangeSingleMillimeters();
  float Sensor1Filter = constrain(Sensor1Smooth, MIN , MAX);
   
  float Sensor2Smooth = Sensor2.readRangeSingleMillimeters();
  float Sensor2Filter constrain(Sensor2Smooth, MIN , MAX);
  
  float Sensor3Smooth = Sensor3.readRangeSingleMillimeters();
  float Sensor3Filter = constrain(Sensor3Smooth, MIN, MAX);
   
  float Sensor4Smooth = Sensor4.readRangeSingleMillimeters();
  float Sensor4Filter constrain(Sensor4Smooth, MIN , MAX);
  

tested the code, also changed this fragment:

VL53L0X Sensor1;

VL53L0X Sensor2;

VL53L0X Sensor3;

VL53L0X Sensor4;

to L1X sensor, but after uploading, all i get is the minimum value from EVERY sensor, which is ‘10’ in the code. None of them are posting.

I also turnd my phones camera on, and looked at the sensors through it, and it seems like only one sensor (the only working one) is sending any light to measure…

Hmm, i changed the Delay time on every address change of a sensor, and now all 3 of them are lit up (still not posting data though), slow progress i guess…

OK, so i unplugged one L1X sensor and came back to original code, and now we are left with 2 L0X sensors. The code now works much faster and most importantly, 2 SENSORS ARE POSTING SEPARATE DATA. Nice, i am getting some progress now!

1 Like

Tried this code again, to hopefully send the data via mavlink, but can’t compile the code, the error is:

exit status 1

Compilation error: too few arguments to function ‘uint16_t mavlink_msg_distance_sensor_pack(uint8_t, uint8_t, mavlink_message_t*, uint32_t, uint16_t, uint16_t, uint16_t, uint8_t, uint8_t, uint8_t, uint8_t, float, float, const float*, uint8_t)’

Any idea? something is missing i think, or maybe something wrong with libraries?

I have just spent a few hours getting this to compile. The issue was it’s old code using old libraries and mavlink definitions, so trying to get everything working was an issue.

all is not lost I have got it to compile but i havent got the hardware together to test it yet. I plan on using a very similar setup you have on one of my boats so I do want to make this work for you. From what I can tell the mavlink distance message has changed and so has the library so I have updated the code with the new mavlink messages and updated the library files.

the message had changed from this
mavlink_msg_distance_sensor_pack(sysid,compid,&msg,time_boot_ms,min_distance,max_distance,current_distance,type,id,orientation,covariance);

to this, thats why it was complaining of too few arguments.
mavlink_msg_distance_sensor_pack(sysid,compid,&msg,time_boot_ms,min_distance,max_distance,current_distance,type,id,orientation,covariance,horizontal_fov,vertical_fov,signal_quality,quaternion);

download this and unzip and place it into your arduino library folder. delete the old mavlink folder with the sketch

Again getting library error:

fatal error: mavlink.h: No such file or directory

my mistake, place that mavlink folder in the same folder as the sketch. I have so many version i got them mixed up.

I changed 2 lines to

#include “mavlink/common/mavlink.h” // Mavlink interface
#include “mavlink/common/mavlink_msg_distance_sensor.h”

latest version compiles I removed the extra sensors that were causing issues so its only sensor 1-4

Awesome! now let me load it to the board.

1 Like

hmmm…


can this be an issue if nothing is connected to tx and rx? (i needed to upload the code, so i disconnected them)

thats how mavlink messages look on the CLI. its sending data. plug it into the flight controller and check mission planner mavlink inspector for mavlink distance messages.

on mission planner select setup tab, then advanced then mavlink inspector.

oh ok, i thought it would look more normal :sweat_smile:, will do

What should i look for there?
image

can’t find any distance messages…

have you configured the serial port on the flight controller to mavlink 2 at 115200 baud?

Forgot about baud rate… Anyway got it!
image

1 Like