some code question

Discussions about all things Bitcraze
arix
Member
Posts: 47
Joined: Fri Sep 01, 2017 1:45 am

some code question

Post by arix »

Hello!
I find the controller_mellinger.c pptraj.c and planner.c in the new firmware. (Minimum snap trajectory generation and control for quadrotors)

when i read controller_mellinger, i found it rewrite a pid controller (stateController). And i can't find pptraj.c and planner.c where to use them .

Can tell me these three file are use for what ? And how to use it ?

On ther hand, i want to use raspberry zero w to control crazyflie by usart. what i need to do ? and the python lib can use for the usart?

Thank you very much !
arnaud
Bitcraze
Posts: 2538
Joined: Tue Feb 06, 2007 12:36 pm

Re: some code question

Post by arnaud »

Hi,

This controller comes from Crazyswarm, you can find some info on the PR: https://github.com/bitcraze/crazyflie-firmware/pull/284. The two file you named have been added as part as the high-level commander: https://github.com/bitcraze/crazyflie-firmware/pull/299.

The merge of Crazyswarm is still very much work in progress, you might find more info about how these modules are used in the Crazyswarm documentation and code.

The python lib does not have a working UART link, there used to be one but I think it has been removed. The link driver are modular both on the python lib and on Crazyflie side, so you should be able to make a UART link without touching the code architecture, just by adding a driver for it.

If you can use the Raspberry pi USB port, there is USB support in the crazyflie lib already so this should work out of the box (at the very least it is a good way to debug and start working until you have a working UART link).
arix
Member
Posts: 47
Joined: Fri Sep 01, 2017 1:45 am

Re: some code question

Post by arix »

arnaud wrote: Mon Mar 26, 2018 10:45 am Hi,

This controller comes from Crazyswarm, you can find some info on the PR: https://github.com/bitcraze/crazyflie-firmware/pull/284. The two file you named have been added as part as the high-level commander: https://github.com/bitcraze/crazyflie-firmware/pull/299.

The merge of Crazyswarm is still very much work in progress, you might find more info about how these modules are used in the Crazyswarm documentation and code.

The python lib does not have a working UART link, there used to be one but I think it has been removed. The link driver are modular both on the python lib and on Crazyflie side, so you should be able to make a UART link without touching the code architecture, just by adding a driver for it.

If you can use the Raspberry pi USB port, there is USB support in the crazyflie lib already so this should work out of the box (at the very least it is a good way to debug and start working until you have a working UART link).
Thank you for your answer !
Now i try to send message from uart2 . I create a task and only use uart2Putchar(); But the crazyflie can't work, I don't know what's wrong with my code .
void uartTxTask (void *param)
{

while(1)
{
uart2Putchar(123);
}
}

void uart2linkInit(void)
{
xTaskCreate(uartTxTask,"UART2TX",SYSLINK_TASK_STACKSIZE,NULL,4,NULL);
}
Only these code on a new file.
I hope you help me ! Thank you !
arix
Member
Posts: 47
Joined: Fri Sep 01, 2017 1:45 am

Re: some code question

Post by arix »

I have solved uart problem. And now i want send data to pi. I want to how to get data from LOG or structs. For example i want get attitude datas , position datas . What need I do to get this data.

Thank you!
arnaud
Bitcraze
Posts: 2538
Joined: Tue Feb 06, 2007 12:36 pm

Re: some code question

Post by arnaud »

How are you planning to use the UART, do you want to implement a CRTP link as I suggested or something custom?

If you implement a CRTP link, you will get the full python lib working and you will be able to log any values the same way it is done using the radio. There is a couple of examples shown in the crazyflie python lib example folder. Tell me if you want to do that and I can give you some pointers of where to add/modify the code.

If you want to implement something custom, you can read log variables from your C code in the Crazyflie. This is done in the ledring driver for example: https://github.com/bitcraze/crazyflie-f ... #L327-L333.
arix
Member
Posts: 47
Joined: Fri Sep 01, 2017 1:45 am

Re: some code question

Post by arix »

arnaud wrote: Wed Mar 28, 2018 9:24 am How are you planning to use the UART, do you want to implement a CRTP link as I suggested or something custom?

If you implement a CRTP link, you will get the full python lib working and you will be able to log any values the same way it is done using the radio. There is a couple of examples shown in the crazyflie python lib example folder. Tell me if you want to do that and I can give you some pointers of where to add/modify the code.

If you want to implement something custom, you can read log variables from your C code in the Crazyflie. This is done in the ledring driver for example: https://github.com/bitcraze/crazyflie-f ... #L327-L333.
Thank you !
My idea is that i want to autonomous flight by crazyflie with pi .
so I want get position and attitude data.Then i will use pi to send target position to crazyflie as using radio .
First i want to use uart set a CRTP link , i write code as usblink and syslink , but i failed i can't get any data from crazyflie. Second i want only get position and attitude data. But i don't know how to get data. now i will try you example, and can you give me some advice to achieve my idea or there have some example like my idea.
I have writed my code. But i face some problem i don't know how to solve it . If datauartTask PRI is high, crazyflie can not work. if PRI is low, I can't get any data. Maybe my code also have problem, can you help me ? Thank you!

Code: Select all

static bool isInit;
#define  BYTE0(dwTemp)       ( *( (uint8_t *)(&dwTemp)	)  )
#define  BYTE1(dwTemp)       ( *( (uint8_t *)(&dwTemp) + 1) )

static void attitudeget(uint8_t buffer[6])
{
    static int pitchid, rollid, yawid;
    static bool isInitialized = false;

    if (!isInitialized)
    {
        pitchid = logGetVarId("stabilizer","pitch");
        rollid  = logGetVarId("stabilizer","roll");
        yawid   = logGetVarId("stabilizer","yaw");
        isInitialized = true;
    }

    float pitch = logGetFloat(pitchid);
    float roll  = logGetFloat(rollid);
    float yaw   = logGetFloat(yawid);

    int16_t pitch16 = (int)(pitch*100);
    int16_t roll16  = (int)(roll*100);
    int16_t yaw16   = (int)(yaw*100);

    buffer[0] = BYTE1(pitch16);
    buffer[1] = BYTE0(pitch16);
    buffer[2] = BYTE1(roll16);
    buffer[3] = BYTE0(roll16);
    buffer[4] = BYTE1(yaw16);
    buffer[5] = BYTE0(yaw16);
}

void datauartTask(void *param)
{
    uint8_t sendbuffer[6];
    int c = 1;
    while(1)
    {
        attitudeget(sendbuffer);
        uart2SendData(sizeof(sendbuffer),sendbuffer);
        uart1Putchar(c);
    }
}

void datauartInit()
{
    if(isInit)
    {
      return;
    }
    uart1Init(115200);
    uart2Init(115200);
    xTaskCreate(datauartTask, "DATAUART", configMINIMAL_STACK_SIZE, NULL, 3, NULL);
    isInit = true;
}

bool datauartTest(void)
{
    return true;
}


static const DeckDriver data_uart = {
  .vid = 0,
  .pid = 0,
  .name = "datauart",
  .usedGpio = 0,

  .init = datauartInit,
  .test = datauartTest,
};
DECK_DRIVER(data_uart);
percy.jaiswal
Beginner
Posts: 14
Joined: Sun Apr 01, 2018 4:23 am

Re: some code question

Post by percy.jaiswal »

I too had some doubt in planner.h file. In this file, I see functions like plan_init, plan_stop, plan_takeoff etc. Can this functions be used to fly autonomously using Crazyflie firmware?

I have seen a demo you had posted in one of forum threads, in which you are using functions => setHoverSetpoint(&setpoint, 0, 0, 0.2, 0) and commanderSetSetpoint(&setpoint, 3);

Which functions are better for developing autonomous flight in crazyflie firmware?
arnaud
Bitcraze
Posts: 2538
Joined: Tue Feb 06, 2007 12:36 pm

Re: some code question

Post by arnaud »

@percy.jaiswal this thread has diverged on another topic, to reduces noise can you create a new topic with your question?

@arix, my sugestion would indeed have been to implement something similar to radioLink/USBLink. Maybe you can get something custom working first and when you get the UART to work both ways you can use your uart code to make a working link. When you make the link do not hesitate to post here if you have any problems.

Your problem with blocking the Crazyflie is that you do not have any sleep or any pause in your code. So if you are high priority you will take all the CPU time. You need to add a sleep (with the function vTaskDelay).

Unfortunatly the uart1 and uart2 drivers are very primitive, they pool register flags in a busy loop, which contribute to this problem: the performance would be better with an interrupt or DMA driven UART driver.
arix
Member
Posts: 47
Joined: Fri Sep 01, 2017 1:45 am

Re: some code question

Post by arix »

arnaud wrote: Thu Apr 05, 2018 12:25 pm @percy.jaiswal this thread has diverged on another topic, to reduces noise can you create a new topic with your question?

@arix, my sugestion would indeed have been to implement something similar to radioLink/USBLink. Maybe you can get something custom working first and when you get the UART to work both ways you can use your uart code to make a working link. When you make the link do not hesitate to post here if you have any problems.

Your problem with blocking the Crazyflie is that you do not have any sleep or any pause in your code. So if you are high priority you will take all the CPU time. You need to add a sleep (with the function vTaskDelay).

Unfortunatly the uart1 and uart2 drivers are very primitive, they pool register flags in a busy loop, which contribute to this problem: the performance would be better with an interrupt or DMA driven UART driver.
Thank you !
Recently, I have tried python-lib and i found usb radio usart driver,and used usb and radio to control crazyflie or get data. By some test, i found usb link can
interrupt radio link ,but radio link can't interrupt usb link. And then i read crazyflie-firmware i have some question .
in the usb.c

Code: Select all

static uint8_t usbd_cf_Setup(void *pdev , USB_SETUP_REQ  *req)
{
  command = req->wIndex;
  if (command == 0x01) {
    crtpSetLink(usblinkGetLink());
  } else {
    crtpSetLink(radiolinkGetLink());
  }

  return USBD_OK;
}
in the comm.c

Code: Select all

#ifdef USE_RADIOLINK_CRTP
  crtpSetLink(radiolinkGetLink());
#elif defined(USE_ESKYLINK)
  crtpSetLink(eskylinkGetLink());
#else
  crtpSetLink(nrf24linkGetLink());
#endif
I have 3 question
1、I want creat usartlinkgetlink,should i do some judgment like usbd_cf_Setup ? or i only need to crtpsetlink usart in the comm.c ? and i found you use flow control for dma . if i use usart2 dma, need i use it ? if i not use it, it will cause some problem?
2、I use python-lib to control crazyflie,at same time i want to get data. now i use break to jump loop. but i found is not good for real-time
function,can you give me some advice.
3、if i want to create my own commander want i need to change code . when i read crtp_commander_generic.c i can't understand. i don't know where to get commander data from radio or usb. and i don't know the when get commander the crazyflie how to use it .
Looking forward you help! Thank you!
arnaud
Bitcraze
Posts: 2538
Joined: Tue Feb 06, 2007 12:36 pm

Re: some code question

Post by arnaud »

Hi,

1. I think the USBlink is a good example, if you do things that way you will be able to enable the serial link using a UART message. Though, as a first approach, I would just create something like serialGetLink and modify comm.c to use it by default instead of the radioLink. I think DMA will eventually be required, but it should work without. So I would see that as a separate things: you can implement the link without DMA and add DMA support after to gain more performance. As a side note: just implementing the UART with interrupt will already help a lot and is much simpler than using DMA.
2. I am not sure to fully understand the questions. The lib has API that uses callbacks and that should allow you to log and control the crazyflie in real time.
3. It depends what you want to do exactly. Do you want to be able to send raw packet to your code or do you want to make a new setpoint packet?
Post Reply