Page 1 of 1

how to print the position with the autonomousSequence.py

Posted: Sun Mar 26, 2017 2:02 pm
by gwiily
https://github.com/bitcraze/crazyflie-l ... equence.py

I have successfully completed the task of automatic flight using the autonomousSequence.py, but now I want to print the crazyflie's position.

I tried to change the code, but still failed.Here is the code I added

Code: Select all

def print_position(scf):
    print('here need to print position')

    log_config = LogConfig(name='Kalman Position', period_in_ms=500)
    
    log_config.add_variable('kalman.varX', 'float')
    log_config.add_variable('kalman.varY', 'float')
    log_config.add_variable('kalman.varZ', 'float')
    with SyncLogger(scf, log_config) as logger:
        for log_entry in logger:
            data = log_entry[1]

            kalman_x = data['kalman.varX']
            kalman_y = data['kalman.varY']
            kalman_z = data['kalman.varZ']
            print('position is: x: %f y: %f z: %f'%(kalman_x,kalman_y,kalman_z))
This is definitely wrong, someone can help me?I think a lot of people need this function.

thanks!

Re: how to print the position with the autonomousSequence.py

Posted: Mon Mar 27, 2017 2:23 pm
by kristoffer
Congratulations to you autonomous flight!

You are pretty close! The thing is that the point of the synchronous parts of the library is to do one thing at the time without having to care about callbacks. What you want to do is to do two things at the same time; running the sequence and logging. Luckily it is possible to mix and match the synchronous and asynchronous parts of the library as needed. You simply have to set up the logging and register a callback before you start the sequence.

Something like this

Code: Select all

def position_callback(timestamp, data, logconf):
    x = data['kalman.stateX']
    y = data['kalman.stateY']
    z = data['kalman.stateZ']
    print('pos: ({}, {}, {})'.format(x, y, z))


def start_position_printing(scf):
    log_conf = LogConfig(name='Position', period_in_ms=500)
    log_conf.add_variable('kalman.stateX', 'float')
    log_conf.add_variable('kalman.stateY', 'float')
    log_conf.add_variable('kalman.stateZ', 'float')

    scf.cf.log.add_config(log_conf)
    log_conf.data_received_cb.add_callback(position_callback)
    log_conf.start()

if __name__ == '__main__':
    cflib.crtp.init_drivers(enable_debug_driver=False)

    with SyncCrazyflie(uri) as scf:
        reset_estimator(scf)
        start_position_printing(scf)
        run_sequence(scf, sequence)
I also added it to the (slightly updated) example in github https://github.com/bitcraze/crazyflie-l ... /issues/43

Re: how to print the position with the autonomousSequence.py

Posted: Tue Mar 28, 2017 12:17 am
by gwiily
kristoffer wrote:Congratulations to you autonomous flight!

You are pretty close! The thing is that the point of the synchronous parts of the library is to do one thing at the time without having to care about callbacks. What you want to do is to do two things at the same time; running the sequence and logging. Luckily it is possible to mix and match the synchronous and asynchronous parts of the library as needed. You simply have to set up the logging and register a callback before you start the sequence.

Something like this

Code: Select all

def position_callback(timestamp, data, logconf):
    x = data['kalman.stateX']
    y = data['kalman.stateY']
    z = data['kalman.stateZ']
    print('pos: ({}, {}, {})'.format(x, y, z))


def start_position_printing(scf):
    log_conf = LogConfig(name='Position', period_in_ms=500)
    log_conf.add_variable('kalman.stateX', 'float')
    log_conf.add_variable('kalman.stateY', 'float')
    log_conf.add_variable('kalman.stateZ', 'float')

    scf.cf.log.add_config(log_conf)
    log_conf.data_received_cb.add_callback(position_callback)
    log_conf.start()

if __name__ == '__main__':
    cflib.crtp.init_drivers(enable_debug_driver=False)

    with SyncCrazyflie(uri) as scf:
        reset_estimator(scf)
        start_position_printing(scf)
        run_sequence(scf, sequence)
I also added it to the (slightly updated) example in github https://github.com/bitcraze/crazyflie-l ... /issues/43
Thank you very much~If I want to print both crazyflie in using the swarmSequence.py, is it the same way?

Re: how to print the position with the autonomousSequence.py

Posted: Tue Mar 28, 2017 9:49 am
by arnaud
Yes it will be similar with multiple Crazyflie, you just have to configure the log block and add a callback function for each Crazyflie.