Help with writing a code

Hello

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!
How are the values provided ?
Is it in an array ?
Since it looks like embedded systems, will the function be called by an interrupt/timer or something like that ?
I think its an array.

Values for velocity are provided from the speedometer of the car, which will be connected to a small processor. Sensor is connected to the processor as well.
The sensor checks the velocity on each tick (there are many ticks a second), and the time interval between ticks is lets say 0.1s. So the function will be called every 0.1s.

I hope I wrote it clearly :)

Thanks!
If the function is called every tick, then values are not provided in a array.

I have a few questions:
- is x global variable ?
- is dt a fixed value or can it change ?
- you are averaging between 2 speed values. The first time a speed value is provided, what value should be used for the other value ?
- what do you mean by "start/stop recording" ?
-x is a global variable.
-dt is a fixed value, it doesn't change.
-the first time a speed value is provided, zero should be used for the other speed value and as well to time, so when it multiplies, it should equal to x=0.
-By start recording, I mean the whole program (algorithm) should start.
The whole program should stop when the width changes.

In other words, If (USIwidth > car width) , then the each tick's info should be saved and formula used.
But If (USIwidth < car width) , then saving the info from the tick's and using the formula should stop.

Thanks! :)
Since everything seems clear to you, why can't you code it ?
I never coded before, and I'm learning it now.
But my deadline is tomorrow, and I believe I don't have time (bad organisation) to code it myself, so I thought someone would help and do the first code for me.
As well I learn best from the already complete code :)


This should be close to what you want to do :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
void UpdateDistance( const float USIwidth, const float speed )
{
    // x, CarWidth and dt are assumed to be global variables of type float
    static float V1;
    static bool isRecording = false;
    
    if( USIwidth >= CarWidth )
    {
        if( isRecording == false )
        {
            // Recording for the 1st time. Just initialize the variables.
            x = 0;
            V1 = speed;
            isRecording = true;
        }
        else
        {
            x += 0.5f*(V1+speed)*dt; // "speed" is V2
            V1 = speed;
        }
    }
    else
    {
        if( isRecording == true )
        {
            // Recording for the last time.
            x += 0.5f*(V1+speed)*dt;
            isRecording = false;
        }
    }
}
Topic archived. No new replies allowed.