1

I just took over a project which has many contributors over the years. The project consists of a robot with distance sensors that collects and processes data and sends it to a server for plotting the area. The robot runs FreeRTOS on a nrf52832 chip and I think it has some weird solution to global variables. In main() it starts several tasks, but the most relevant ones are, sensor-tower-task, estimator-task and controller-task which has their own source and header file.

Each header files only contains the declaration of the respective tasks, like:

taskX.h

void vSensorTowerTask(void *pvParameters);

Each sourcefile for the three mentioned tasks contains the following declarations for the global variables:

taskX.c

extern int16_t gX_hat;
extern int16_t gY_hat;
extern float gTheta_hat;
// +several lines with global variables

main.c

int16_t gX_hat = 0;
int16_t gY_hat = 0;
float gTheta_hat = 0;
// +several more

It looks like the gTheta_hat in main() gets updated when the estimator-task updates it, but not in the controller and sensor-tower tasks. If a global and local variable has the same name, shouldn't that give some kind of warning from the compiler. Is there any reason at all the declaration of the globals are done in this manner? It should be noted that reading and writing to these globals are protected with mutex.

cybStar
  • 11
  • 1
  • One compilation unit has to define the global variables, all the other units just declare it so they can access the same variables. – Barmar Mar 03 '20 at 17:30
  • *It should be noted that reading and writing to these globals are protected with mutex.* Good call. You may be able to simplify things a bit with C++'s [`std::atomic`](https://en.cppreference.com/w/cpp/atomic/atomic) or C's [`_Atomic`](https://en.cppreference.com/w/c/atomic) – user4581301 Mar 03 '20 at 17:31
  • 1
    I think you need to look into the meaning of the keyword `extern`: https://stackoverflow.com/questions/10422034/when-to-use-extern-in-c – Bart Mar 03 '20 at 17:31

0 Answers0