How to use the breakout board

Firmware/software/electronics/mechanics
matthieuleonie
Beginner
Posts: 20
Joined: Mon Nov 13, 2017 11:54 am

How to use the breakout board

Post by matthieuleonie »

Hi,

We try to use the breakout board to pin a ultrasonic sensor (HC-SR04). In order to generate the ultrasound we need to set the Trig on a High State for 10 µs. We know how to do that on a Arduino board but not on the Crazyflie. We saw a similar topic about sending "HIGH" or "LOW" through the IO : viewtopic.php?f=5&t=1364&p=7333
However, we can't access the code :https://github.com/bitcraze/crazyflie-f ... /exptest.c in today's firmware. So can you tell us how to send a "HIGH" or "LOW" with a certain frequency with the IO pins?

Thanks a lot :)
arnaud
Bitcraze
Posts: 2538
Joined: Tue Feb 06, 2007 12:36 pm

Re: How to use the breakout board

Post by arnaud »

Hi,

You need to make a new deck driver, there is a short howto on the wiki to get you started: https://wiki.bitcraze.io/doc:crazyflie: ... mware:deck.

You can look at existing deck drivers for example on how to make tasks. There is an easy-to-use GPIO API that looks like arduino: https://github.com/bitcraze/crazyflie-f ... _digital.h. For example it is used there: https://github.com/bitcraze/crazyflie-f ... .c#L67-L68.

The exptest.c deck driver is used in factory to test Crazyflie, it is not such a good example since it initializes GPIOs without using the Arduino-like API.

A side point: you will have to create a task to run your code while the firmware is running. Pulsing the pin should be similar as with Arduino but measuring the time it takes for the echo will have to be a bit different: there is no ready-made pulsein() function in Crazyfie and you cannot make a while loop to measure the echo pin since you will use 100% of the CPU while doing that and trigger the watchdog (you can try by adding a sleep in the loop though but then you will have a measurement granularity of 1ms). You most likely will need to use one of the STM32 timer to measure the pulse. The closest example I can think about is this implementation that measures pulse start and pulse width: https://github.com/bitcraze/crazyflie-f ... #L258-L270.
matthieuleonie
Beginner
Posts: 20
Joined: Mon Nov 13, 2017 11:54 am

Re: How to use the breakout board

Post by matthieuleonie »

Hi,

Thank you for your answer, it helps us so much!! :)

We have written a code and we want to see if it works. However when we want to compile the code (through the command "Make CLOAD"), the console send us this message:

Code: Select all

make all DEBUG=0 CLOAD=1
  CLEAN_VERSION
  VTMPL version.c
  CC    version.o
  LD    cf2.elf
bin/locodeck.o:(.data.algorithmsList+0x10): undefined reference to `uwbTdoaTagAlgorithm'
collect2: error: ld returned 1 exit status
make: *** [cf2.elf] Error 1
We didn't touched the locodeck code, so we don't understand why it doesn't work...
Can you help us? Thank you :)
arnaud
Bitcraze
Posts: 2538
Joined: Tue Feb 06, 2007 12:36 pm

Re: How to use the breakout board

Post by arnaud »

have you tried to make clean and make again? Sometime, when touching the Makefile, it is required to clean the built files with 'make clean'.
matthieuleonie
Beginner
Posts: 20
Joined: Mon Nov 13, 2017 11:54 am

Re: How to use the breakout board

Post by matthieuleonie »

Yes I also tried that but it changed anything...
arnaud
Bitcraze
Posts: 2538
Joined: Tue Feb 06, 2007 12:36 pm

Re: How to use the breakout board

Post by arnaud »

You must have changed something to the UWB building, maybe removed a file from the Makefile configuration.

You can type "git diff" in the console in the project folder to inspect your changes and see if you have changed anything related to the LPS.
matthieuleonie
Beginner
Posts: 20
Joined: Mon Nov 13, 2017 11:54 am

Re: How to use the breakout board

Post by matthieuleonie »

Hi,

It finally works. Thank you! :)

We try to set a step by affecting a value to a variable during a period of time so it goes back to 0 as soon as it reaches the reload value. To achieve that, we used the microprocessor as a clock and we set the counter to have 1Hz. We used the variable range_last to print the graph in the client. However, the problem is that range_last stays at 5 and never changes. Can you take a look at the code and tell us the reasons why it's not working?

Code: Select all

void timerInit()
{
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

    TIM_TimeBaseInitTypeDef timerInitStructure;
    timerInitStructure.TIM_Prescaler = 50000;
    timerInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
    timerInitStructure.TIM_Period = 20; //Periode de autoreload
    timerInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    timerInitStructure.TIM_RepetitionCounter = 0;
    TIM_TimeBaseInit(TIM2, &timerInitStructure);
    TIM_Cmd(TIM2, ENABLE);
    // Enable the NVIC if you need to generate the update interrupt
}

bool ultrasonReadRange(altitude_t* zAltitude, const uint32_t tick)
{
	bool updated = false;
	int timerValue = TIM_GetCounter(TIM2);

	while(1)
	    {
	        if (timerValue == 5) { //up
	        	DEBUG_PRINT("5s");
	        	range_last = 5;
	    }
	        else if (timerValue  == 10) { //down
	        	DEBUG_PRINT("10s");
	        	range_last = 0;
	        }
	        updated = true;
	        return updated;
	    }
}
Thank you
arnaud
Bitcraze
Posts: 2538
Joined: Tue Feb 06, 2007 12:36 pm

Re: How to use the breakout board

Post by arnaud »

I am not completly sure of what you are trying to achieve. Who is calling the ultrasonReadRange function and at what rate?

For sure though you are not using the right timer. Searching in the code shows that timer2 is used for the motor PWM: https://github.com/bitcraze/crazyflie-f ... _cf2.c#L28. There is unfortunatly no documentation so find out what times is used where, so the only way is to do a search in the code (works well directly in github).

In the example I sent you earlier (https://github.com/bitcraze/crazyflie-f ... #L258-L270) the timer used is TIM5, this timer is only used by expansion port driver so it is a good fit for you.
matthieuleonie
Beginner
Posts: 20
Joined: Mon Nov 13, 2017 11:54 am

Re: How to use the breakout board

Post by matthieuleonie »

Hi,
for now, we are only trying to define the pulse that will be sent to the ultrasonic sensor.
The ultrasonReadRange function is called by sensors_cf2.c in the sensorsAcquire function.

Actually we have other questions:

1. Once we will have managed to make this work we would like to change the height kept by the drone when height mode is on. Could you tell us where to do that in the code please ?

2. The goal of our work is to make the drone land automatically. In order to transition from manual to automatic mode, we are wondering whether we should use the same system as the z-ranger in the cf client or directly implement a command in the firmware. What would you advise us ?

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

Re: How to use the breakout board

Post by arnaud »

Is the length of the generated pulse important? Intuitively I would have generated the pulse with a simple delay (ie. "vTaskDelay(M2T(time_in_ms))" ) and used the timer only to measure the width of the received pulse. Doing it that way should make the code much simpler.

1. Once you push the current height into the estimator the same way the zranger deck is doing it, you will be able to send setpoints with absolute height. This can be done either from the firmware or from the ground.

2. I would advise sending the setpoint from the python client. There is not ready mechanism (yet) to handle this kind of high-level commands in the Crazyflie yet and since the client already have the required funtionality for sending the height-hold setpoint you will need and you will have to modify the client anyway, I would do the landing sequencing in the client.
Post Reply