using SyncLogger with an array of configurations

Discussions and questions about the Crazyflie Nano Quadcopter
Post Reply
cafeciaojoe
Member
Posts: 50
Joined: Mon Jun 18, 2018 2:37 am

using SyncLogger with an array of configurations

Post by cafeciaojoe »

Hi.

I am trying to use SyncLogger with an array and don't exactly understand how ther person in this forum post did it viewtopic.php?f=11&t=4450&p=20329&hilit ... ger#p20329. Alot of their code is commented out... do you need to use two for loops for the two configurations? I get two prints of data_1, data_2 but then I get a key error for
'stateEstimate.x'

Code: Select all

    def flight_logger(self):
        logger.info('Starting flight logger thread')

        log_angle = LogConfig(name='lighthouse', period_in_ms=100)
        log_angle.add_variable('lighthouse.rawAngle0x', 'float')
        log_angle.add_variable('lighthouse.rawAngle0y', 'float')
        log_angle.add_variable('lighthouse.rawAngle1x', 'float')
        log_angle.add_variable('lighthouse.rawAngle1y', 'float')

        log_position = LogConfig(name='Position', period_in_ms=100)
        log_position.add_variable('stateEstimate.x', 'float')
        log_position.add_variable('stateEstimate.y', 'float')
        log_position.add_variable('stateEstimate.z', 'float')

        rawAngle0x = [0, 0]
        rawAngle0y = [0, 0]
        rawAngle1x = [0, 0]
        rawAngle1y = [0, 0]

        state_estimate = [0, 0, 0]

        with SyncLogger(self._cf, [log_angle,log_position]) as log:
            for log_entry_1 in log:
                for log_entry_2 in log:
                    data_1 = log_entry_1[1]
                    data_2 = log_entry_2[1]

                    print(data_1, data_2)

                    rawAngle0x.append(data_1['lighthouse.rawAngle0x'])
                    rawAngle0x.pop(0)
                    rawAngle0y.append(data_1['lighthouse.rawAngle0y'])
                    rawAngle0y.pop(0)
                    rawAngle1x.append(data_1['lighthouse.rawAngle1x'])
                    rawAngle1x.pop(0)
                    rawAngle1y.append(data_1['lighthouse.rawAngle1y'])
                    rawAngle1y.pop(0)

                    state_estimate[0] = data_2['stateEstimate.x']
                    state_estimate[1] = data_2['stateEstimate.y']
                    state_estimate[2] = data_2['stateEstimate.z']

                    if rawAngle0x[0] == rawAngle0x[1] and rawAngle0y[0] == rawAngle0y[1] and rawAngle1x[0] == rawAngle1x[1] and rawAngle1y[0] == rawAngle1y[1]:
                        self.cf_pos = Position(float('nan'), float('nan'), float('nan'))
                        # print(self.cf_pos)
                    else:
                        self.cf_pos = Position(state_estimate[0], state_estimate[1], state_estimate[2])
                        # print(self.cf_pos)
PhD Student
RMIT University
School of Design
http://www.cafeciaojoe.com
Image
kristoffer
Bitcraze
Posts: 369
Joined: Tue Jun 30, 2015 7:47 am

Re: using SyncLogger with an array of configurations

Post by kristoffer »

No, you only need one for loop, but in each iteration you get either log data for log_angle or log_position, but not both. You have to check that the key actually exists in the dictionary before reading

Code: Select all

with SyncLogger(self._cf, [log_angle,log_position]) as log:
            for log_entry_1 in log:
                    data_1 = log_entry_1[1]

                    if 'stateEstimate.x' in data_1:
                        x = data_1['stateEstimate.x']
                        # Use x...
Post Reply