Page 1 of 2
Learning the firmware
Posted: Thu May 08, 2014 2:39 am
by Matt-
Hi, I'm doing an honours project and using the Crazyflie as the target for a compiler. (Whiley bytecode to C on an embedded device)
As part of getting to grips with the Crazyflie code, I want to do a couple of very basic activities in C. Control the leds and control the motors.
This is purely to 'get something happening', not related to flying.
So far I have happily compiled to an image various attempts and flashed to the Crazyflie and recovered again, flashing original code. Its worth saying I'm very pleased your bootloader is separate. I now understand what 'bricking' is, but only because my supervisor went pale when I described flashing new main() methods. Gotta love these learning curves...
I am now working my way through the firmware code to try and figure out what I need to adapt to get leds to flash and motors to jump. I already have the driver code.
Any pointers would be greatly appreciated or a main() method and the #includes needed to play with the leds would be fine, I can extrapolate from there.
Re: Learning the firmware
Posted: Thu May 08, 2014 5:11 am
by Matt-
I'm surprised how difficult this was. Took virtually all day, with a lot of dead ends.
For any who follow, I created:
init/test_main.c
Code: Select all
#include "led.h"
int main(){
ledInit();
ledSet(LED_RED, 1);
ledSet(LED_GREEN, 1);
while(1);
return 0; // will not get here
}
To run this, open your Makefile and change line 58 to
From the command line, navigate to your crazyflie-firmware directory. Run 'make' and then flash your new cflie.bin file to your Crazyflie.
This makes the red and green leds glow -- woohoo!
Change your Makefile line 58 back to
Run 'make' and flash the new cflie.bin to your Crazyflie and you have flying again -- woohoo!
Next stop, doing the same for the motors.
Re: Learning the firmware
Posted: Thu May 08, 2014 7:03 am
by Matt-
Ok, got similar working for the motors, but I had to hack a method vTaskDelay(M2T(int)); replacing it with a while loop.
This is the main method:
init/test_main_motors.c
Code: Select all
#include "led.h"
#include "motors.h"
int main(){
ledInit();
motorsInit();
motorsTestTask(0);
return 0;
}
I added into motors.h for a more interesting looking test.
I have used ledSet(LED_RED, 1); as a debug printf statement to figure out when the code is getting to.
And added to drivers/src/motor.c from line 209
Code: Select all
ledSet(LED_RED, 1);
//vTaskDelay(M2T(1000));
int k = 0;
int on = 1;
int max = 1000000;
while(k++ < max){
if(k%500000 == 0){
ledSet(LED_RED, on);
on = on == 0 ? 1 : 0;
}
}
while(1)
{
//vTaskDelay(M2T(100));
k = 0;
while(k++ <= 50000){
if(k%50000 == 0){
ledSet(LED_RED, on);
on = on == 0 ? 1 : 0;
}
}
Remove the extra "while(1){" statement and two vTaskDelay()s, that are pushed down by inserting this code.
Ok, now how do I get vTaskDelay() working correctly? So I can remove the while loop hack.
Re: Learning the firmware
Posted: Thu May 08, 2014 7:18 am
by tobias
Cool, and yes it's a bit of a learning curve and we wish we would have better documentation, hopefully something that will come.
To start using the FreeRTOS functionality you must start the scheduler. I would recommend you start playing in
system.c instead. That is where everything is more or less started from the systemTask.
Re: Learning the firmware
Posted: Thu May 08, 2014 7:43 am
by Matt-
I spent a good while this morning looking at system.c, but got lost at the xTaskCreate() in systemLaunch().
I think... it sets up a task which runs systemTask(), giving it its own stack. It was at this point I wrote my first post here, looking for an easier way to just get started. Making leds flash shouldn't be that hard!
Is the way forward (re-doing my led and motors test) to insert a test method into systemTask() after the call to systemInit()?
Re: Learning the firmware
Posted: Wed May 14, 2014 8:24 am
by tobias
Sorry for a late reply. Since most of the modules are dependent on OS functionality the systemTask is the first thing started. This in turn starts everything else.
E.g. in systemInit the ledseqInit is called which initializes the LEDs. The stabilizerInit in stabalizer.c is the init that sets up the motor driver.
You should be able to comment most of the init functions out and only start what you need. E.g you could call ledInit and motorsInit only to just play with the leds and motors.
E.g make a new systemTask function and comment out the old one:
Code: Select all
#include "motors.h" // add motors include
...
void systemTask(void *arg)
{
ledInit();
motorsInit();
while(1)
{
vTaskDelay(M2T(1000)); // e.g. delay of 1000ms
motorsSetRatio(MOTOR_M1, 20000); // turn on motor m1 at 20000 of max 65535
vTaskDelay(M2T(100)); // e.g. delay of 100ms
motorsSetRatio(MOTOR_M1, 0); // turn off motor m1
}
}
I have not tested the code but hopefully it works. Is this making sense?
Re: Learning the firmware
Posted: Wed Feb 17, 2016 12:55 am
by cmcarroll
I know this is an older post, but I just read through these questions/suggestions, and I'm a little lost. I just started working with the CF1/CrazyRadio on the latest 0.6 VM and am trying to run some small codes (i.e. turn off the green and red LEDs, turn one motor on) just to get some type of movement on the crazyflie. All of my firmware is up-to-date.
I started with main.c in /home/bitcraze/Desktop/projects/crazyflie-firmware/init/main.c and started working mainly with:
Code: Select all
int main()
{
//Initialize the platform.
platformInit();
//Launch the system task that will initialize and start everything
systemLaunch();
//Start the FreeRTOS scheduler
vTaskStartScheduler();
//TODO: Move to platform launch failed
ledInit();
ledSet(0, 1);
ledSet(1, 1);
//Should never reach this point!
while(1);
return 0;
}
I commented out ledSet(0,1) and ledSet(1,1), assuming that just having ledInit() left at that point would turn off the red and green LEDs. However, nothing happened. I wanted to experiment with turning on a single motor too, but I can't even get a small LED test to work... I cleaned, compiled, and flashed the code as follows in the VM terminal:
Code: Select all
> cd ~/projects/crazyflie-firmware
> make clean
> make PLATFORM=CF1
> make cload PLATFORM=CF1
Am I editing in the wrong .c file? Am I flashing the edited firmware incorrectly? Am I misunderstanding how the LED library (and possibly the motor library) works?
Thanks!
Caitlin
Re: Learning the firmware
Posted: Wed Feb 17, 2016 6:21 am
by chad
cmcarroll wrote:Am I editing in the wrong .c file? Am I flashing the edited firmware incorrectly? Am I misunderstanding how the LED library (and possibly the motor library) works?
Hi (again) Caitlin,
That's the wrong place if you want to disable the LEDs. That's a very low level init. The LED sequence is being run through hal/src/ledseq.c
Try to edit that file. Comment out line 290 and see what happens when you restart in firmware mode:
Code: Select all
default: //The step is a LED action and a time
// ledSet(led, step->value);
if (step->action == 0)
Re: Learning the firmware
Posted: Wed Feb 17, 2016 4:58 pm
by cmcarroll
This looks promising. The only problem is that all of a sudden, I can't open the cfclient in the VM... when I try to manually open it through the command line, I get an error saying only Python 3+ is supported and I'm running on Python 2.7.8. I had this happen once, so I deleted and reinstalled the VM (which worked), but this time that doesnt solve the problem.
Re: Learning the firmware
Posted: Wed Feb 17, 2016 5:29 pm
by chad
cmcarroll wrote:This looks promising. The only problem is that all of a sudden, I can't open the cfclient in the VM... when I try to manually open it through the command line, I get an error saying only Python 3+ is supported and I'm running on Python 2.7.8. I had this happen once, so I deleted and reinstalled the VM (which worked), but this time that doesnt solve the problem.
Hi Caitlin,
It looks like the "master" branch has been updated to require Python 3 now. You'll have to update to Python 3 on the VM unless you want to checkout a previous tag - such as 2015.08:
Code: Select all
cd ~/projects/crazyflie-clients-python
gt checkout 2015.08