Page 1 of 1

firmware makefile

Posted: Thu Oct 22, 2020 5:21 pm
by Maximiliandio
hello, I am trying to implement a MPC controller but my knowledge of the firmware and the Makefile is only limited. I have auto generated code for The MPC in a folder cmpc with a corresponding makefile (I can not alter this)

Code: Select all

CC = gcc 
FLAGS = -Os -Wall -Wstrict-prototypes -pedantic
OPT = -O3 -funroll-loops
STD = -std=c89

all: libcmpc mpc_const.o mpc.o mpc_ref.o mpc_inc.o mpc_stc.o mtx_ops.o fip_ops.o

libcmpc: mtx_ops.o fip_ops.o mpc_const.o mpc.o mpc_ref.o mpc_stc.o mpc_inc.o
	ar rcs libcmpc.a mtx_ops.o fip_ops.o mpc_const.o mpc.o mpc_ref.o mpc_inc.o mpc_stc.o

mpc_const.o: mpc_const.c
	$(CC) $(FLAGS) $(OPT) $(STD) -I./include -c mpc_const.c

mtx_ops.o: mtx_ops.c
	$(CC) $(FLAGS) $(OPT) $(STD) -I./include -c mtx_ops.c
	
fip_ops.o: fip_ops.c
	$(CC) $(FLAGS) $(OPT) $(STD) -I./include -c fip_ops.c

mpc.o: mpc.c
	$(CC) $(FLAGS) $(OPT) $(STD) -I./include -c mpc.c

mpc_ref.o: mpc_ref.c
	$(CC) $(FLAGS) $(OPT) $(STD) -I./include -c mpc_ref.c

mpc_inc.o: mpc_inc.c
	$(CC) $(FLAGS) $(OPT) $(STD) -I./include -c mpc_inc.c

mpc_stc.o: mpc_stc.c
	$(CC) $(FLAGS) $(OPT) $(STD) -I./include -c mpc_stc.c

clean:
	rm *.o libcmpc.a
I want to link the files. The example folder of the MPC code generator hands a makefile as an example

Code: Select all

CC = gcc
FLAGS = -Os -Wall -Wstrict-prototypes -g
OPT = -O3 -funroll-loops
STD = -std=c89

all: lcmpc motor

lcmpc:
	make -C cmpc

motor: main_motor.c lcmpc
	$(CC) $(FLAGS) $(OPT) $(STD) main_motor.c -Lcmpc -lcmpc -o motor

clean:
	rm -rf cmpc python __pycache__ motor
What would be the best way to link the MPC code from above

Re: firmware makefile

Posted: Fri Oct 23, 2020 7:55 am
by kimberly
hi!

So we need a bit more information what you actually want to achieve.

Were is the code generated from and do you want to implement it as one of the controllers currently available (like mellinger indi and pid)? Why can't you add these files to our Makefile instead? You might need to rewrite some parts to it to be able to input the state estimates and the output properly.

In general for embedded programming I generally have bad experience with auto generated code since that is mostly optimized for working on the computer and not on an embedded system like an STM. Maybe for an app layer application but so integral as an controller is tricky.

Re: firmware makefile

Posted: Fri Oct 23, 2020 9:14 am
by Maximiliandio
I want to create a new controller in a new file

The MPC code is particularly designed for embedded systems: http://ifatwww.et.uni-magdeburg.de/syst/muAO-MPC/
What I tried so far is to add the .h files in the interface folder and the .c files in the normal modules folder and add the files in the Makefile like
PROJ_OBJ += mpc.o ....
I can include the library but I can not access the functions defined in the mpc files I assume that something is not working with the linking.
I also thought of creating the library as an archive (using the first Makefile) but I don't know how to link the library using the MAkefile from the firmware

Re: firmware makefile

Posted: Mon Oct 26, 2020 10:00 am
by arnaud
Hi,

Generating an archive is the best approach I think. This way you can link all files at once.

I have already done that in the firmware in order to compile the ARM math lib with different optimisation flags as the rest of the fimrware, so you should be able to follow the same procedure to add your lib to the Crazyflie.

The lib can be added to PROJ_OBJ the same way objects files are added:

Code: Select all

# Libs
PROJ_OBJ += libarm_math.a
And there is a rule to make the lib:

Code: Select all

libarm_math.a:
	+$(MAKE) -C $(CRAZYFLIE_BASE)/tools/make/cmsis_dsp/ CRAZYFLIE_BASE=$(abspath $(CRAZYFLIE_BASE)) PROJ_ROOT=$(CURDIR) V=$(V) CROSS_COMPILE=$(CROSS_COMPILE)
If you add your lib and a rule to make it (as a first approach you could just copy the prebuilt lib in one of the VPATH folder), all the object files will be linked in and so you should be able to compile your code fine (as long as you are not calling functions we do not have from your lib, for example we do not support "printf" yet in the firmware).