acceleration to velocity code debugging?

hey guys

i have a basic code laid out in easyC to convert the acceleration values from the accelerometer into velocity, shown below:

void main ( void )
{
      int accelerometery[6] = {0}; 
      int timestep = 1000; 
      int i = 0; 
      int velocity[6] = {0}; 

      InitAccelerometer ( 2 ) ; // initializes the accelerometer sensor
      Wait ( 1000 ) ;
      StartAccelerometer ( 2 ) ; // starts the sensor
      while ( 1 )
      {
            i = i + 1 ;
            accelerometery* = GetAcceleration ( 2 ) ; // reads feedback from the sensor
            velocity* = accelerometery* * timestep + velocity*  ;
            PrintToScreen ( "Accelerometery = %d mgs\n" , accelerometery[6] ) ; // displays the acceleration in milig's
            PrintToScreen ( "Velocity = %d\n" , velocity[6] ) ;
            Wait ( timestep  ) ;
      }
}

when i run the code to test, and keep the accelerometer absolutely still, i am getting these values:

https://lh3.googleusercontent.com/-SFVIqfudyq4/ToS3QeZZ7DI/AAAAAAAAAFE/0sDf6YWn3zY/s512/jumbo.JPG***

any idea / advice as to what is wrong or what i should fix? :confused:

thanks in advance.*

Well start by thinking about happens on the seventh time around the loop, your arrays overflow. Let me look a little more, not sure why you are even using arrays.

OK, I’m not going to write the code for you, you are on the right track. I see you are trying to implement this…

velocity = previous_velocity + (acceleration * delta_time )

Think about the following.

You have a large delta time, 1000, you should probably be thinking in terms of 10mS. With such a large delta time the integration will not be very accurate.

There is no need to use an array.

What units do you want the final velocity to be in? perhaps meters per second. So what units should the acceleration be in perhaps m/s^2. If so then convert from milli G into m/s^2 first.

Will integers be able to handle the units for acceleration and velocity, perhaps think about using double.

here’s perhaps some psuedo code.

while( 1 )
{
read acceleration
convert acceleration to correct units
multiply acceleration by delta time (0.01) then add to current value of velocity

wait 10mS

Every hundred times around the loop print results.
}

I don’t really know anything about easyC, but I can point you to a ROBOTC page that might help. The language is different, but the logic should be the same. Here’s the link: ROBOTC
I hope that you get it working.

The example uses tasks, which is probably the best way to do it, the closest thing to tasks in EasyC is the RegisterRepeatingTimer function.

Quoting from another thread
“RegisterRepeatingTimer(RepeatTimeMS, FunctionToBeCalled); uses a low priority timer based interrupt service routine. You can have upto 4 of these running at a time.”

There seems to be more questions about programming in EasyC than RobotC, or am I wrong. Anyone know what percentage of users are on which dev environment?

That’s an interesting question. I’m going to go make a poll for it now.

so from what i’ve gathered, the RegisterRepeatingTimer is the best way to go about this instead of using an array to store values?
By using an array (since it stores previous values), I wanted to have a few consistent negative velocity readings before the robot is told to do anything further. Is this still achievable by using RegisterRepeatingTimer?

for example, if the robot reads a negative velocity for x amount of milliseconds, it would run some code to correct for the negative velocity ie, drive motors forward.

I cant seem to find it in the function blocks, do I just add it in via the code method?

Take a look at this posted by the EasyC guru.
https://vexforum.com/showpost.php?p=140155&postcount=2