Creating custom ports for CRTP

Firmware/software/electronics
Post Reply
rbresnahan
Beginner
Posts: 7
Joined: Mon Oct 14, 2019 5:46 pm

Creating custom ports for CRTP

Post by rbresnahan »

Weird problem and possibly easy solution:

I've been looking to implement a new CRTPPort (calling it RELAY = 0x09 in the crtpstack.py) and create a callback handler within the firmware to play with P2P capabilities. Yet I've already run into an issue where setting the header of a new CRTPPacket gives me the error: AttributeError: type object 'CRTPPort' has no attribute 'RELAY'.

Am I missing an addition somewhere other than crtpstack.py and my test file on the python library side of things?
Thanks to any that can point out what I am missing!
arnaud
Bitcraze
Posts: 2538
Joined: Tue Feb 06, 2007 12:36 pm

Re: Creating custom ports for CRTP

Post by arnaud »

Without seeing the code, it is quite hard to understand what is happening.

One good example of how to make a new port is the localization port. It both includes sending and receiving packet from the Crazyflie.

The python side is implemented there: https://github.com/bitcraze/crazyflie-l ... ization.py

The Crazyflie side is implemented there: https://github.com/bitcraze/crazyflie-f ... ice.c#L102

On either side the implementation is similar: to receive packet from a port you need to add a port callback function, the function is called when a packet is received on that port. To send a packet on a port there is a function that will send the packet to a specific port/channel defined in the packet.
rbresnahan
Beginner
Posts: 7
Joined: Mon Oct 14, 2019 5:46 pm

Re: Creating custom ports for CRTP

Post by rbresnahan »

Thank you for the references, I will try my hand at it again today!

I am having some issues following the process after the crtp packets are received as well.
I've followed them to the point of being queued through the RTOS but can't seem to locate where they are redirected to their appropriate modules by port number. My apologies if what I'm asking is unclear!

I will keep looking in the meantime and thank you in advance
arnaud
Bitcraze
Posts: 2538
Joined: Tue Feb 06, 2007 12:36 pm

Re: Creating custom ports for CRTP

Post by arnaud »

To implement a new port, all you need is to setup a callback for the port you want to implement and it will be called each time a packet for this port is received. You do not need to work with anything else down the stack.

If you are curious about the implementation it might be simpler to start from the top, from the crtpRegisterPortCB() function. The packet dispatching is done in crtp.c, the callback you setup will be called there: https://github.com/bitcraze/crazyflie-f ... #L190-L193.
arnaud
Bitcraze
Posts: 2538
Joined: Tue Feb 06, 2007 12:36 pm

Re: Creating custom ports for CRTP

Post by arnaud »

A quick edit, now that I looked at crtp.c, there is also a way to setup a queue for the port and to read packet synchronously from a task. That allows to implement communication in a loop instead of handling callback and creating a queue yourself. An example of this way of doing is the info port: https://github.com/bitcraze/crazyflie-f ... info.c#L80. Note that the queues are 16 packets long and the Crazyflie will assert (crash) if the queue becomes full so it is very important that packets are handled quickly when using a queue.
rbresnahan
Beginner
Posts: 7
Joined: Mon Oct 14, 2019 5:46 pm

Re: Creating custom ports for CRTP

Post by rbresnahan »

I think I have narrowed down the reason but am not sure how to fix it. Does the python-lib side of things need to be re-compiled somehow for changes to take effect? I ask because adding CUSTOM = 0x09 to the CRTPPort class and doing a simple print(CRTPPort.CUSTOM) still throws the error " type object 'CRTPPort' has no attribute 'CUSTOM' ".

Edit: Am I missing a step to finalize the python-lib for testing?
arnaud
Bitcraze
Posts: 2538
Joined: Tue Feb 06, 2007 12:36 pm

Re: Creating custom ports for CRTP

Post by arnaud »

Ho, yes I see, this is likely due to the fact that you installed the lib from pip and not in edit mode from your local folder. Pip can install libs in edit mode which means that it will use your local folder instead of a version pulled from the internet.

To install your lib in edit mode, uninstall it (a couple of time to make sure) and install it with the -e flag:

Code: Select all

python3 -m pip uninstall cflib
python3 -m pip uninstall cflib
cd crazyflie-lib-python
python3 -m pip install -e .
rbresnahan
Beginner
Posts: 7
Joined: Mon Oct 14, 2019 5:46 pm

Re: Creating custom ports for CRTP

Post by rbresnahan »

Thank you so much! I will try this as soon as I get home
Post Reply