log and fly class

All discussions related to the Loco Positioning system
Post Reply
newcuriousstudent
Beginner
Posts: 15
Joined: Fri Jul 31, 2020 1:45 am

log and fly class

Post by newcuriousstudent »

Hello,

In the questions I have asked in the previous post I made I received help in trying to log and fly at the same time. This works great!
I was able to make many drones fly at the same time and log their data.

But the next thing I was trying to do is to set up the code from:
https://www.bitcraze.io/documentation/r ... commander/

in a class.

But when I put everything in:
if __name__ == '__main__':

in a class __init__() part of a class

and all the function declarations as part of the class

I kept running into the issue that since those function are passed and not directly declared I could not get it to work.
I would have to add self. in front of the param_deck_flow for example

scf.cf.param.add_update_callback(group='deck', name='bcFlow2',
cb=self.param_deck_flow)

but that leads me to get the issue that the program says I need to pass the argument of the function

Would anyone know how to work around this?
Does my question make sense?
newcuriousstudent
Beginner
Posts: 15
Joined: Fri Jul 31, 2020 1:45 am

Re: log and fly class

Post by newcuriousstudent »

or even more simply:
the only way I was able to get the class to work was with this:

import logging
import time

import threading
from threading import Thread
from threading import Timer

import cflib.crtp # noqa
from cflib.crazyflie import Crazyflie
from cflib.crazyflie.log import LogConfig

import matplotlib.pyplot as plt
import numpy as np
from cflib.crazyflie.syncCrazyflie import SyncCrazyflie
from cflib.positioning.motion_commander import MotionCommander
# Only output errors from the logging framework





#is_deck_attached = False




def param_deck_flow(name, value):
global is_deck_attached
print(value)
if value:
is_deck_attached = True
print('Deck is attached!')
else:
is_deck_attached = False
print('Deck is NOT attached!')

def log_pos_callback(timestamp, data, logconf):
print(data)
def take_off_simple(scf):
with MotionCommander(scf) as mc:
mc.start_linear_motion(0, 0, .1)
#time.sleep(2)
time.sleep(3)
def move_linear_simple(scf):
with MotionCommander(scf, default_height=DEFAULT_HEIGHT) as mc:
time.sleep(1)
mc.forward(0.5)
time.sleep(1)
mc.back(0.5)
time.sleep(1)

class flight():
def __init__(self,log_pos_callback,take_off_simple,move_linear_simple):
self.is_deck_attached = False
URI = 'radio://0/75/2M/E7E7E7E702'
logging.basicConfig(level=logging.ERROR)
cflib.crtp.init_drivers(enable_debug_driver=False)

with SyncCrazyflie(URI, cf=Crazyflie(rw_cache='./cache')) as scf:

scf.cf.param.add_update_callback(group='deck', name='bcFlow2',
cb=param_deck_flow)
time.sleep(1)
logconf = LogConfig(name='Position', period_in_ms=10)
logconf.add_variable('stateEstimate.x', 'float')
logconf.add_variable('stateEstimate.y', 'float')
cf = scf.cf
cf.log.add_config(logconf)
logconf.data_received_cb.add_callback(log_pos_callback)

if self.is_deck_attached:
logconf.start()

take_off_simple(scf)

logconf.stop()


'''def param_deck_flow(name, value):
self.is_deck_attached
print(value)
if value:
self.is_deck_attached = True
print('Deck is attached!')
else:
self.is_deck_attached = False
print('Deck is NOT attached!')'''





#def takeoff():


x = flight(log_pos_callback,take_off_simple,move_linear_simple)

##########################
but the issue is the functions I don't know how to write them inside the class because if I just straight put them in they don't work
but if I pass them through like above (which works) I cannot access the class variables like is_deck_attached from the functions
newcuriousstudent
Beginner
Posts: 15
Joined: Fri Jul 31, 2020 1:45 am

Re: log and fly class

Post by newcuriousstudent »

Hey,
I just figured it out.
please disregard those messages.
kimberly
Bitcraze
Posts: 700
Joined: Fri Jul 06, 2018 11:13 am

Re: log and fly class

Post by kimberly »

Great! Could you specify a bit what solved it to make this forum thread more whole ? :)
Post Reply