how to print the position with the autonomousSequence.py

Firmware/software/electronics/mechanics
Post Reply
gwiily
Beginner
Posts: 20
Joined: Wed Dec 02, 2015 2:12 am

how to print the position with the autonomousSequence.py

Post 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!
kristoffer
Bitcraze
Posts: 630
Joined: Tue Jun 30, 2015 7:47 am

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

Post 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
gwiily
Beginner
Posts: 20
Joined: Wed Dec 02, 2015 2:12 am

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

Post 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?
arnaud
Bitcraze
Posts: 2538
Joined: Tue Feb 06, 2007 12:36 pm

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

Post 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.
Post Reply