Basically, after the connectSetupFinished callback is called, I create a log file and start a thread for each log I'm building. In this case I have three: motor, accelerometer, and gyro:
Code: Select all
Thread(target = self.motor_log).start()
Thread(target = self.acc_log).start()
Thread(target = self.gyro_log).start()
Each thread calls a function which sets up the log config and starts logging. The accelerometer function looks like this:
Code: Select all
def acc_log(self):
acc_log_file = open(self.acc_log_filename, 'wb')
self.acc_writer = csv.writer(acc_log_file)
self.acc_writer.writerow(['time','acc.x','acc.y','acc.z'])
acc_log_config = LogConfig('acc', 1000/log_freq)
acc_log_config.addVariable(LogVariable('acc.x', 'float'))
acc_log_config.addVariable(LogVariable('acc.y', 'float'))
acc_log_config.addVariable(LogVariable('acc.z', 'float'))
self.acc_log = self.crazyflie.log.create_log_packet(acc_log_config)
if (self.acc_log is not None):
self.acc_log.data_received.add_callback(self.acc_data)
self.acc_log.error.add_callback(self.logging_error)
self.acc_log.start()
else:
logger.warning("Could not setup logconfiguration after connection!")
def acc_data(self, data):
self.acc_writer.writerow([time.time(), data['acc.x'], data['acc.y'], data['acc.z']])
The other functions have the same format but obviously log different variables.
After logging is running, I start yet another thread for the vehicle commands depending on which flight profile I'm using:
Code: Select all
if args.thrust_profile == 'increasing_step':
Thread(target=self.increasing_step).start()
if args.thrust_profile == 'hover':
Thread(target=self.hover).start()
if args.thrust_profile == 'prbs':
Thread(target=self.prbs).start()
My full source code is here:
https://github.com/andykee/uclathesis/t ... est_flight