Getting motion sensor datas during drive_ramp function

Hello everybody,

I am trying to get motion sensor datas during my activity bot is moving forward.
To move forward , i am using drive_ramp function but when this function is called , it seem's that it's a loop and the program stays in the loop , i can't get my datas during this loop (fprintf for accelerometer is never working)

My program :
int main()                                    
    imu_init(10, 9, 6, 5);
    sd_mount(DO, CLK, DI, CS);
    FILE* fp = fopen("test1.txt", "a+");
    drive_getTicks(&distLeft[i], &distRight[i]);
    //print("distLeft[%d] = %d, distRight[%d] = %d\n", i, distLeft[i], i, distRight[i]);  // START
    drive_setRampStep(10);                      // 10 ticks/sec / 20 ms

    drive_ramp(128, 128);                       // Forward 2 RPS

    imu_readAccelCalculated(&__imuX, &__imuY, &__imuZ);
    ax = (int)(100.0 * __imuX);
    ay = (int)(100.0 * __imuY);
    az = (int)(100.0 * __imuZ);

   fprintf(fp,"Accelerometer : Ax=%d Ay=%d Az=%d\n", ax, ay, az);
    while(ping_cm(8) >= 20) pause(5);           // Wait until object in range
    drive_getTicks(&distLeft[i+1], &distRight[i+1]);
    fprintf(fp,"\n distLeft[%d] = %d, distRight[%d] = %d\n", j+1, distLeft[i+1], j+1, distRight[i+1]); // Distance traveled
    drive_ramp(0, 0);                           // Then stop
    drive_speed(-32, 32);                     // rotate left

    // Keep turning while object is in view
    while(ping_cm(8) < 20); 

Someone could help me ?
Thanks in advance


  • 6 Comments sorted by Date Added Votes
  • For starters you should move the calls to imu_init and sd_mount out of the while loop. I would also suggest moving the fopen out of the loop as well. Replace the fclose with an fflush, and that should guarantee that your data get written out to the SD card.
  • alex93alex93 Posts: 11
    edited October 5 Vote Up0Vote Down
    Thank you for your advices Dave Hein , it seem's to work.
    However the writing cycle is too large , i timed and in 1 minutes of moving forward , the program has written only 3 times into the SDcard (I have only 3 fprintf for the acceleration in the text file).

    Do you have an idea of how i can change that ? I need a fprintf each seconds more or less.

    Thank you very much for your help

  • When do you want acceleration measured and printed to file?
    The way the code is written drive_ramp is executed (and the program essentially waits until the ramp has reached 128 ticks.) Then acceleration is measured and written.
    The distance traveled per second will be approx 41 cm.

    Then the program waits until the bot has closed to less than 20cm with an object.
    Then it writes the number of ticks, ramps to zero, and starts turning left. It continues turning left while the distance to an object is less than 20 cm.

    Then it begins the primary 'while' loop again (ramp to 128, get & record acceleration, wait until d<20, etc.)

    So the number of times acc or d or ticks is written depends on how long it takes to get close to an object and then turn away from the object. Is that what you want?

  • alex93alex93 Posts: 11
    edited October 6 Vote Up0Vote Down
    Thank you for your reply Tom , yes that's the working that i want.

    I changed the ramp value for drive_ramp(32, 32) , we know that we need 64 ticks for 1 rotation of the wheel.
    The lower the ramp value is , and the more i should got acceleration samples during a straight line and a defined time , right ?

    So if tried to go only straight without any osbtacles during 5 minutes ( I timed) , I should get a lot of acceleration samples.

    My text file should looks like this :

    Accelerometer : Ax=12 Ay=43 Az=5
    Accelerometer : Ax=13 Ay=42 Az=5
    Accelerometer : Ax=12 Ay=42 Az=6
    Accelerometer : Ax=13 Ay=45 Az=6
    Accelerometer : Ax=12 Ay=42 Az=3
    Accelerometer : Ax=13 Ay=44 Az=5
    Accelerometer : Ax=12 Ay=43 Az=5
    Accelerometer : Ax=13 Ay=42 Az=5
    Accelerometer : Ax=12 Ay=42 Az=6
    Accelerometer : Ax=13 Ay=45 Az=6
    Accelerometer : Ax=12 Ay=42 Az=3
    Accelerometer : Ax=13 Ay=44 Az=5

    But for the moment i get only 6 samples in 5 minutes

    With a lower value for the ramp , the program should have time to write several times into the SD card during a straight line I think

    Do you see what I mean ? I don't know if i am clear

  • When I look at the code you posted in your original post, I don't see any looping if there is no obstacles to detect and this line is always true:
    while(ping_cm(8) >= 20) pause(5);

    For testing without any obstacles comment out that line, and the program should drop through to the end of the while(1) loop and repeat. There shouldn't be any change in speed. You should get an acceleration measurement written each loop. (You should also move the
    line out of the while(1) loop.)

    Please also post your current code so I can make sure I'm properly interpreting it. Once you get this working you might want to capture acceleration readings during the ramp up & down. You can do that using another cog, but try getting the basics working first

  • Thanks for your advices Tom , it is ok I solved the problem by writting in a different way my loop.

    Thanks again
Sign In or Register to comment.