Access to tick from another source file.

Discussions and questions about the Crazyflie Nano Quadcopter
Post Reply
imranmomtaz
Member
Posts: 36
Joined: Tue Mar 17, 2020 7:01 pm

Access to tick from another source file.

Post by imranmomtaz »

Hello,
I am trying to access live tick value from another source file (connected with a task priority 2). So far, I have wrote the following:

in stabilier.c file:

Code: Select all

uint32_t tick;
uint32_t *pTickPTR = &tick;
in other source file (lets assume filename: cf.c)

Code: Select all

extern uint32_t *pTickPTR;
uint32_t tick;

while(1){
...
tick1 = (*pTickPTR);
...
}
...
LOG_GROUP_START(KFChk1)
  PARAM_ADD(LOG_UINT32, tick, &tick1)
LOG_GROUP_STOP(KFChk1)
The output of the this program is given below:

Code: Select all

stabilier.tick = 2, KFChk1.tick = 0
.
.
.
stabilier.tick = 3102, KFChk1.tick = 1869
stabilier.tick = 3112, KFChk1.tick = 1869
stabilier.tick = 3122, KFChk1.tick = 1869
stabilier.tick = 3132, KFChk1.tick = 1869
.
.
.
Please let me know what I am doing wrong and suggest how I can access this. Thanks a lot.
wydmynd
Member
Posts: 87
Joined: Thu Dec 06, 2018 9:25 am

Re: Access to tick from another source file.

Post by wydmynd »

first, you are using PARAM_ADD where LOG_ADD should be
second, you can use log variables to internally transfer data between tasks, also RTOS queues are used
I didn't use pointers so will be also interested to hear if this is supposed to work
imranmomtaz
Member
Posts: 36
Joined: Tue Mar 17, 2020 7:01 pm

Re: Access to tick from another source file.

Post by imranmomtaz »

Thanks. Can you show an example about how log variable can be used to transfer data between tasks? Thanks a lot!
arnaud
Bitcraze
Posts: 2538
Joined: Tue Feb 06, 2007 12:36 pm

Re: Access to tick from another source file.

Post by arnaud »

When transfering a single word of data from one thread to another you could just declare it as extern, without needing to use a pointer, log or anything else:

Code: Select all

// In stablizer.c:
uint32_t my_tick;


// in another file
extern uint32_t my_tick;

// From here, accessing my_tick does access the my_tick variable form stabilizer.c
If a variable is to be used only in one file, it should be declared as "static", otherwise it is shared with all the files. In your original message you would have a problem with the tick variable that is declared as non-static in two different files.

There is also a way to declare and access log variable from within the firmware, depending of what you are trying to achieve it might be overkill though, sharing a variable between two files is handled by C itself. An example of how to read a log variable from within the firmware can be found in the app examples: https://github.com/bitcraze/crazyflie-f ... aram_api.c

As a last note, if you want to share more than a single 32bit variable between thread, you will need some kind of synchronization, either a mutex or a queue. There is a lot of code in the Crazyflie that you can look at that already uses FreeRTOS queues and semaphores/mutexes. The FreeRTOS doc is also a good place to look at to find more information.
imranmomtaz
Member
Posts: 36
Joined: Tue Mar 17, 2020 7:01 pm

Re: Access to tick from another source file.

Post by imranmomtaz »

Thank you.
Post Reply