Code: Select all
import logging, time,sys,cflib.crtp
from cflib.crazyflie import Crazyflie
from cflib.crazyflie.log import LogConfig, Log, LogVariable
from cflib.crazyflie.syncCrazyflie import SyncCrazyflie
from cflib.crazyflie.syncLogger import SyncLogger
uris = ['radio://0/80/2M/E7E7E7E701','radio://0/80/2M/E7E7E7E702']
logging.basicConfig(level=logging.ERROR)
def callback0(timestamp,data,logconf):
print("0:",data,logconf) #GUI Label
def callback1(timestamp,data,logconf):
print("1:",data,logconf) #GUI Label
if __name__ == '__main__':
crazyflie = []
syncCrazyflie = []
kalmanLog = []
kalmanParams = [("kalman", "stateX", "float"),("kalman", "stateY", "float"),("kalman", "stateZ", "float")]
cflib.crtp.init_drivers(enable_debug_driver=False)
for y in range(0,2):
crazyflie.append(Crazyflie(rw_cache='./cache'))
syncCrazyflie.append(SyncCrazyflie(uris[y], cf=crazyflie[y]))
kalmanLog.append(LogConfig("GetKalmanPos", 100))
for param in kalmanParams:
kalmanLog[y].add_variable('{}.{}'.format(param[0], param[1]), param[2])
syncCrazyflie[y].open_link()
crazyflie[y].log.add_config(kalmanLog[y])
kalmanLog[0].data_received_cb.add_callback(callback0) #<-----
kalmanLog[0].start()
kalmanLog[1].data_received_cb.add_callback(callback1)#<-----
kalmanLog[1].start()
time.sleep(2)
kalmanLog[0].stop()
kalmanLog[1].stop()
For the 2 lines marked "<-----" i would need to create 2x functions for the callback, and the function will do something(print in this case as an example)
I need hep to reduce the 2x function into a 1x function or a loop function style. The purpose is that, there will be multiple crazyflies and i hope to conect/disconnect them dynamically. Upon connecting them, their kalmanLog should start as well.
The idea is what if i have 15x Crazyflie connected, I would have to crease 15x function of def callback0 all the way to def callback 14. Im sure there's a elegant solution.
Potential solutions that i have tried but doesnt work:
TRY 1:
Code: Select all
def callback(timestamp,data,logconf,y):
print("0:",data,logconf) #GUI Labe
for y in range(number_of_crazyflies):
kalmanLog[y].data_received_cb.add_callback(callback(y)) #<-----
kalmanLog[y].start()
Code: Select all
def callback(timestamp,data,logconf,y):
print("0:",data,logconf) #GUI Label
for y in range(number_of_crazyflies):
kalmanLog[y].data_received_cb.add_callback(partial(callback,y)) #<-----
kalmanLog[y].start()
Try 3:
Since the logconf will be different and unique everytime the Crazyflie is powered on.
i created a stored_logconf = [] to store the hex value upon connection of the crazyflie and using this callback style:
Code: Select all
def callback(timestamp,data,logconf):
for y in range(number_of_crazyflie):
while(str(logconf)) == stored_logconf[y]:
print(data)
for y in range(number_of_crazyflie):
kalmanLog[y].data_received_cb.add_callback(callback) #<-----
kalmanLog[y].start()
def(callback) expected 0 arguments but 3 was provided
def(callback) expected 3arguments but 4 was provided