How to predict if there would be a potential vehicle collision (V2V)

I'm trying to make a project based on vehicle-to-vehicle communication using nrf24l01+, the idea is that both vehicles have the prototype(Arduino, nrf24l01+ and such) to detect if there would be a potential collision. I have also recently found out that you could use Haversine formula to find the distance between two points, but the problem really lies on how would I know if there would be a potential vehicle collision.

I am very new here and at the same time a beginner in Arduino environment. I tried to research if there's any sensor that is suitable for this case. I also search for some Arduino projects wherein the object can know if they are approaching to each other(for my project so that it could tell if there will a collision), but there is none, at least from my web results. One project I saw from Youtube has the same concept but what I also saw is that there is no accelerometer, they only used GPS which, of course, not ideal for moving cars. link

After researching for like days, I am tempting to give up that there is really no exact sensor suitable for my project but what I have known is that I can use Kalman filter to accurately predict final position of the vehicle using accelerometer and GPS based on this link. However people said it is a tough subject, and I'm honestly afraid to take this approach.

Is there really no sensor for this kind of project? Please do not hesitate to guide me if there's another method for my project to avoid Kalman filter or if there is an easier approach for this prototype, and sorry if there are grammatical errors.

Sure, there are quite a few sensors...
LIDAR, IR, Microwave and variations thereof.

Call Tesla and ask what they're using.

If you can trust the GPS you can look at position over time and kind of predict the course if there is no change of direction

for example if the angle between vehicle 1 and and vehicle 2 does not change over time and headings cross then you are on a collision course

This is how you estimate there is a risk when you are on a boat (constant bearing, decreasing range = collision course)

Many articles on this topic

1 Like

The classic racing the train problem's answer is that any object keeping a steady bearing relative to your course WILL collide. If they're falling back, you'll get there first, if they're gaining they'll get there first. If their bearing is steady, sound the collision alarm, take evasive action, and brace for impact! (sound of ships klaxon blaring!)

So you need to be able to determine bearing more than speed. Somehow you need to scan like a radar and sense at what ANGLE something is at. So a stepper or servo with a detector that can also give distance is what is needed. Sonar or laser ranging. The time of flight for laser ranging is expensive. So sonar echolocation it is.

Mount your echo sensor on the axis of the servo. Pan back and forth noting the nearest object and the servo angle. Move. Repeat. If servo angle is the same - crash coming.

(Use'ta work for Northrop and Martin Marietta back in the day, missile guidance don'cha know! I actually WAS a "rocket scientist", well guidance anyway. LOL)

1 Like

This is difficult ..

I think you need to do research on the present state of art - lane control , stability control , distance between cars , speed etc . I would doubt an Arduino would be powerful enough for something that needs fast complex calculations.

Think about you would , as a human , detect a crash in various scenarios and what actions you would take , that will help you know what you need to detect to sense a possible collision -

Thank you for the response, I need the value of angle between two vehicles, that makes sense, however how should I find if the heading of the vehicle will cross?

Is it? Oh man, this is for my thesis in my final year of computer engineering, I mean I'm only focusing on class 1 vehicles so it should be not that hard.. If it really needs much power then I guess I will just go for raspberry pi or ultrasonic sensors to ease calculations..

mmm...heard that one before.

1 Like

what matters is the angle towards the other vehicle

I'm assuming you have a GPS on board each vehicle and that each vehicle broadcast an updated position/time with some sort of ID to whoever wants to listen or may be just to the other vehicle in your case

most GPS libraries know how to calculate the bearing to a destination (lat/long) and the distance (great circle) to a destination.

So when you receive the position of the other vehicle, simulate you want to go to that position and calculate that bearing B0 and distance to the destination D0

then when you get the next updated position from the other car, do the same thing:
calculate that bearing B1 and distance to the destination D1

if B0 is the same as B1 and D0 is greater than D1 then you are on a collision course

ideally you would confirm that over a number of messages with some interval of confidence

if bearing is the same and distance is the same then you are on parallel routes

if bearing stays the same but distance is increasing, then you are moving further apart

mind precision of your GPS though... it's a rough evaluation of a situation.
having a GPS working at 10Hz would probably also help get a more frequent position update. It depends how fast your vehicle go as your GPS fix can be 1 second old and you might have travel a bit during that time

1 Like

You could perhaps develop this model. That is that each vehicle transmits its position in real time to central control unit, maybe using GPS supplemented with a compass/inertial navigation system and maybe some interpolation.
On that central control unit the main calculation of each vehicles trajectory is calculated, in a manner similar to that which @J-M-L suggested, and a message sent back if there is a pending collision, possibly supplemented with an avoidance action.
For a simple proof of concept and demonstration that could work. The calculations to do that may force the use of a higher end Arduino (or better) on the central control unit.

I also imagine that, in practice, such a system would have to be augmented by an autonomous system on each vehicle for collision detection. That could use a range of sensors like image processing etc. However, these would require considerable processing power. Radar is a possibility and there are cheap sensors but the simple ones can only indicate proximity to another object, not direction.

Also ideal would be a direct negotiation of avoidance action between all the vehicles potentially involved in a collision ( one vehicle swerves right, one brakes etc.) but that gets complicated.

1 Like

Wow many thanks! So it's not really necessary for me to use Kalman Filter, I guess it is just an optional thing for more accuracy, but I can just formulate what you just have said. Seriously, thank you.

Thank you for the reply. Will it be okay if instead I use Raspberry Pi as my 2nd option if Arduino cannot handle my prototype? or maybe Mega? I honestly do not know what kind of microcontroller should I use yet that is why I only left Arduino unspecified.

Substantial difference in scale there.

A Mega 2560 has a few times more RAM and program memory than the UNO (or preferably, Nano) with exactly the same processing capability.

A Pi has thousands of times more of these resources as well as hundreds of times the processor speed with several times the word size. Vastly different, basically incomparable. :roll_eyes:

1 Like

I would probably try to envision a demonstration environment and work backwards from that in order to dimension the computing requirements.

Lets say you start with the model that each vehicle transmits its coordinates every say 100ms from a GPS system supplemented by some sort of inertial guidance system chip (maybe based on an MPU6050 for intermediate/averaged results). I'm guessing that could be done by a basic Arduino in each vehicle (but even that assertion would have to be tested).
Then you have to decide how many vehicles this system would monitor. Let's say 3. You could take over a car park and use 3 toy radio controlled vehicles each transmitting their coordinates to a central controller. That central controller has to work quite hard to keep up to date with the current position of each vehicle and, based on its trajectory, look ahead a few seconds for potential collisions. That is quite a lot of precision geometry etc. and probably you'd want some sort of graphical display showing the current position of each vehicle and maybe also indicating potential conflicts. The safest option is probably a PC.

If you decide to add some autonomous check in each vehicle for proximity to another vehicle then you have to look at which sensors you are going to use and base the computing power on their requirements. Any sort of artificial intelligence based interpretation of visual sensors is going to rule out an Arduino. A basic radar sensor which alerts when a foreign object has a distance of X meters or less requires less processing power.

There is a huge amount of hype about smart cities and managed traffic flows, usage based billing of private vehicles, IoT infrastructures for handling all this data, remote management of vehicles and including safety issues like collision detection etc. etc. All I can say is ensure that you can focus on something that is practical and demonstrable.

1 Like

In my opinion you should be looking at this a different way.

By this stage of your studies...

... you should know how calculate the parts of the system. You should be looking at the amount of work that the system needs to do, and then work out how much oomph you need to provide. Engineering is about, well, engineering- figuring out the loads, calculating how big a "machine" you need, and then looking up the specs of the products available and configuring a solution that suits. You can't thumb suck the size of the equipment. A civil engineer can't say "hmmm ok "this" beam looks about right, what do you guys think?- maybe "that" one would be better?" Nor should a computer engineer do that- engineer the solution, don't guess.

1 Like

Really the maths are not that complicated if you go for bearing and distance and you receive the position at 1Hz.

Any arduino can handle that.

if you want to verify the performance, run this code on your target architecture

#include <Time.h>

// let's imagine this is the structure that we broadcast for each vehicle
struct __attribute__((packed)) t_VehicleInfo {
  uint32_t ID; // vehicle ID
  double lat;  // current latitude
  double lng;  // current longitude
  time_t when; // when the fix was taken (UTC)
};


// see https://github.com/mikalhart/TinyGPSPlus or http://arduiniana.org/libraries/tinygpsplus/
// functions taken out of the TinyGPS++ library

// returns distance in meters between two positions, both specified
// as signed decimal-degrees latitude and longitude. Uses great-circle
// distance computation for hypothetical sphere of radius 6372795 meters.
// Because Earth is no exact sphere, rounding errors may be up to 0.5%.
// Courtesy of Maarten Lamers

double distanceBetween(double lat1, double long1, double lat2, double long2) {
  double delta = radians(long1 - long2);
  double sdlong = sin(delta);
  double cdlong = cos(delta);
  lat1 = radians(lat1);
  lat2 = radians(lat2);
  double slat1 = sin(lat1);
  double clat1 = cos(lat1);
  double slat2 = sin(lat2);
  double clat2 = cos(lat2);
  delta = (clat1 * slat2) - (slat1 * clat2 * cdlong);
  delta = sq(delta);
  delta += sq(clat2 * sdlong);
  delta = sqrt(delta);
  double denom = (slat1 * slat2) + (clat1 * clat2 * cdlong);
  delta = atan2(delta, denom);
  return delta * 6372795;
}

// returns course in degrees (North=0, West=270) from position 1 to position 2,
// both specified as signed decimal-degrees latitude and longitude.
// Because Earth is no exact sphere, calculated course may be off by a tiny fraction.
// Courtesy of Maarten Lamers
double courseTo(double lat1, double long1, double lat2, double long2) {
  double dlon = radians(long2 - long1);
  lat1 = radians(lat1);
  lat2 = radians(lat2);
  double a1 = sin(dlon) * cos(lat2);
  double a2 = sin(lat1) * cos(lat2) * cos(dlon);
  a2 = cos(lat1) * sin(lat2) - a2;
  a2 = atan2(a1, a2);
  if (a2 < 0.0)  a2 += TWO_PI;
  return degrees(a2);
}


t_VehicleInfo vehicle1 = {1, 49.239038, -2.101748, 0};
t_VehicleInfo vehicle2 = {2, 49.239058, -2.102092, 0};

void setup() {
  Serial.begin(115200); Serial.println();
}

void loop() {

  uint32_t t0 = micros();
  double distance = distanceBetween(vehicle1.lat, vehicle1.lng, vehicle2.lat, vehicle2.lng);
  double course = courseTo(vehicle1.lat, vehicle1.lng, vehicle2.lat, vehicle2.lng);
  uint32_t deltaT = micros() - t0;

  Serial.print(F("Course : ")); Serial.print(course, 2); Serial.println(F("°"));
  Serial.print(F("Distance : ")); Serial.print(distance, 2); Serial.println(F(" m"));
  Serial.print(F("Compute time : ")); Serial.print(deltaT); Serial.println(F(" µs"));
  Serial.println(F("-------------"));
  
  vehicle2.lat += ((double) random(-100, 100)) / 100000.0; // create a small variation
  vehicle2.lng += ((double) random(-100, 100)) / 100000.0; // create a small variation

  delay(1000); // update at 1Hz
}

I would expect each evaluation to take less than 1.5ms on an Arduino Nano or UNO and probably ten time faster (less than 150µs) on an ESP32.

and because the 2 functions we call in sequence have some maths in common, you could optimise that by having only one function that returns both the course and the distance

Taking a board with 2 or more serial ports will make listening to the GPS more robust than a software serial solution, that's the only reason why I would move away from Uno or Nano (in prototyping stage of course)

1 Like

Don’t forget if a car is getter nearer it does not automatically mean there will be a crash - eg cars joining motorways .
If this is your final yr project , you will need to show research methods and algorithms you’ve considered . Looked at the state of the art , what others have done etc etc
Quoting “ I asked others on a forum how to do it “ won’t give you many marks

If I was your tutor I’d also be looking here

1 Like

Closing on a CONSTANT bearing does mean a crash. Highway speeds reduce you processing time. 60mph = 88fps. 1S sample rate you're closing by 90', 100mS, 9', 2x that if the other vehicle is coming head on. You can't rely on the other vehicle having ANY co-ordinating hardware or software. You have to be able to operate stand alone.

1 Like

But … I agree a constant bearing = crash .

But at what point do you decide it’s a crash or just part of a normal
Manoeuvre ( like joining motorway ) which will change the path in due course .
Needs method to know they’ve seen you lol

1 Like

Rate of closure drops in a merge. Constant bearing AND closing fast is your primary target.

Q. You've determined you're on a collision course, now what? Slam on the brakes? Not helpful if you're stationary and THEY'RE headed at you. Swerve? Which way? Into what? Off a cliff? What do you do in heavy traffic when you're in a "target rich" environment. Is that 100mS sample time per target?

Collision avoidance is the inverse of targeting.

1 Like