8 years, 10 months ago.

Printf stops working

Basically I run the following program:

#include "mbed.h"
#include "AvailableMemory.h"

struct MMgait_t {
    int time_steps; //number of time steps per step
    float peak_time; //time of peak hip angle
    float walking_angle; //double stance hip angle
    float end_angle; //hip angle at the end of step
    float max_angle; //max hip flexion
};
MMgait_t mm_gait_params;
Serial pc(USBTX,USBRX);
DigitalOut myled(LED1);
const float time_steps=900;

bool swing_calculate(int time, float &value, float start, float end, float blend_steps)
{
    float _slope=(end-start)/time_steps;
    if (time<=blend_steps) {
        value=_slope*time+start;
    } else if (time <= mm_gait_params.peak_time) {
        value=(mm_gait_params.end_angle-mm_gait_params.max_angle)*pow(time/mm_gait_params.peak_time-1,2)+mm_gait_params.max_angle;
    } else {
        value=(mm_gait_params.walking_angle-mm_gait_params.max_angle)*pow((time-mm_gait_params.peak_time)/(mm_gait_params.time_steps-mm_gait_params.peak_time),2)+mm_gait_params.max_angle;
    }
    if(time>mm_gait_params.time_steps) {
        return 1;
    } else {
        return 0;
    }
};

bool stance_calculate(int time, float &value, float start, float end, float blend_steps)
{
    float _slope=(end-start)/time_steps;
    if (time<=blend_steps) {
        value=_slope*time+start;
    } else {
        value = (-mm_gait_params.walking_angle+mm_gait_params.end_angle)/(mm_gait_params.time_steps-1)*time+mm_gait_params.walking_angle;
    }
    if(time>mm_gait_params.time_steps) {
        return 1;
    } else {
        return 0;
    }

};

int main()
{
    pc.baud(921600);
    myled=0;
    mm_gait_params.time_steps=900;
    mm_gait_params.peak_time=416;
    mm_gait_params.walking_angle=10;
    mm_gait_params.end_angle=-22;
    mm_gait_params.max_angle=30;

    float value=0;
    for(int i=0; i<mm_gait_params.time_steps; i++) {
        swing_calculate(i,value,-30,0,100);
        pc.printf("%d, %.2f \r\n",i, value);
    }
    for(int i=0; i<mm_gait_params.time_steps; i++) {
        stance_calculate(i,value,15,6,100);
        pc.printf("%d, %.2f \r\n",i, value);
    }
    for(int i=0; i<mm_gait_params.time_steps; i++) {
        swing_calculate(i,value,-30,0,100);
        pc.printf("%d, %.2f \r\n",i, value);
    }
    for(int i=0; i<mm_gait_params.time_steps; i++) {
        stance_calculate(i,value,15,6,100);
        pc.printf("%d, %.2f \r\n",i, value);
    }
    myled=1;
}

And the first time I run it, it works and prints everything out. The second time, if I restart with Tera-Term (Alt-B), then it fails and stops printing midway through. However, if I push the reset button on the mbed, then it succeeds again.

This is a problem because when I run the mbed on external power for my robot then it encounters this error regardless. It seems like the mbed is crashing during a print statement, but I don't know why. There seems to be plenty of available memory.

This is a really strange bug. I'm not sure at all what the problem might be. I started with a base class and a couple derived classes, then I simplified to just two classes. Now I've simplified to just two functions, and it still doesn't work.

1 Answer

8 years, 10 months ago.

I have run your program using the mbed LPC1768. What i found, try using value_ in place of value as this var is used in the mbed lib, also time_steps is declared twice, once as a const float and also an int, i know one is in a struct, but it can cause confusion. You dont have a return in main. Last but not least, baud rate 921600 is i expect too high for a pc, running windows, there were gaps in the print out, but program runs to the end without error, try 460800 it appeared to work ok.

Accepted Answer