Adding random util function

Hey there,

For a project I want to inject some white noise into my controller. From my research there isn’t really a random number generator on the ODrive and I wanted to create one similar to the our_arm_cos_f32 function that I can call throughout the rest of the code. My implementation is quite simple:

#include <board.h>
#include <cstdlib>

float noise()
{
    std::srand(static_cast<unsigned int>(std::time(nullptr)));

    float rand_val = -1.0 + 2.0 * static_cast<double>(std::rand() / RAND_MAX);

    return (rand_val);
}

And I just placed the function next to the cos ad sin function:

extern "C" {
float our_arm_sin_f32(float x);
float our_arm_cos_f32(float x);
float noise();
}

When I build this I receive an error undefined reference ‘noise’. Am I missing something? Why doesn’t this work?

I think you have to wrap the definition of noise() with the extern “C” as well.

Why are you extern’ing it?

I am externing it because I thought it would make my code cleaner. But I think I can also just define it in the controller so I just gonna try that next because I am not sure what wraping means.

Thanks

Btw, this:

will always produce 0 because it’s integer division.
You also want to avoid calculations with double because the cpu on the ODrive cannot do those in hardware and they are done in software as a fallback.