### Help with writing my first code?

Hello everyone,

I am new to this forum as I just started learning C++.

I got to write a code for parallel parking for cars.
Its basically a ultra sonic sensor on the left side of the car which measures distance of the parking space and shows whether there is space for the car to park in parallel.
This measurements are processed by the small mbed processor from ARM.

So to measure the distance, following equations must be used(unless there is a mistake):

Note:
USI= Ultrasonic sensor input
Car width= x
Car length= y
Car speed= v
Time= T

1.USIprevious = Change in displacement previous
USIcurrent = Change in displacement current

2. If(USIcurrent-USIprevious) > Car width ,
then start measuring carspeed and time.
Distance= Distance + V times change in T.

3. If USIcurrent < Car width, then stop measuring.

This equations are basically saying that when the car is traveling in a street, and the distance from the side of the car becomes larger than the width of the car (which means there is some parking space probably), then from that moment the sensor will start recording the speed of the car and time. But when the sensor detects that the distance from the side of the car became smaller than the width of the car (the parking space has ended), it stops recording the speed and time and it calculates the distance by using average velocity times time.

I am a bit confused how to write a code based on this information, so any help would be greatly appreciated.

As well I hope you will understand what I wrote :)

I think I understand what you want, but I disagree with one thing: I don't think you necessarily need (USIcurrent - USIprevious) > car width to determine that there's a potential parking space.

Say, for instance, USIprevious = .5x (where x = car width), and suddenly USIcurrent becomes just greater than 1x, giving the car room to park. The way you've stated it, this wouldn't count as a measurable opportunity, because USIcurrent - USIprevious would only be just over .5x, though there would be room to park the car.

Expand on this point, and I'll get back with some suggestions.

I want to write the following equations in C++:

Note:
USI: UltraSonicSensorInput
USIwidth: UltraSonicSensorInput distance
x: distance travelled by car
V: velocity

So the equations are:

1. If (USIwidth > car width) , then record x=0 and current V and use formula:

x=0.5*(v1+v2)*dt

dt is time interval between the two tick's of the sensor( I dont know the exact time interval and the tick's happen a lot of times)

Then save the x and repeat the same on each tick and then add each tick's x.
So if on the first tick we get x, then add that x with x1 for the second tick, and x2 for the third tick etc.
At the end it looks like x+x1+x2+x3... etc.
Basically summing up the results of each tick.

2. If (USIwidth < car width) , then record V the last time and use the formula
x=0.5*(v1+v2)*dt the last time and stop recording.

After that, sum all x's which will result in the total distance traveled by the car.

If anyone could help me write this into a code I would be very grateful! :)

Thanks!

well

the space we will need to park our car of a fixed size never changes, so, we can assume that if the sensor detects `int carwidth = width_of_car()` amount of space, that there could be a potential parking space. This is important, becase although the amount of space available over time may change, the amount of space we need, will not.

now, to calculate the distance, we will use BASIC algebra. speed = distance/time, right? So, distance = speed * time.

`distance = speed_of_car() * dt // (for each tick...) `

now, with that in mind, mabey you can construct your own algorithm. Hint: I would use the methode mentioned above with 1 slight modification, for each tick, the distance traveled during that tick is added to a double until `space < width_of_car()`.

However, I need to use the formula I mentioned above, as the velocity is changing all the time.

Anyone have other suggestions? :)

Thanks
Topic archived. No new replies allowed.