Battery Logging with Example Script

Discussions about all things Bitcraze
cds12
Beginner
Posts: 1
Joined: Sun Nov 07, 2021 2:56 am

Battery Logging with Example Script

Post by cds12 »

I am trying to implement a way to log battery levels for a swarm. I was trying to reference the basiclog.py and how they implement it with a stabilizer. Is this possible if so is there an example script on how to log battery levels for a swarm?
jonasdn
Expert
Posts: 132
Joined: Mon Mar 01, 2021 3:13 pm

Re: Battery Logging with Example Script

Post by jonasdn »

Hi!

I am not sure there is a example, but you should be able to do it with the Swarm class, with something like (untestedd, might not work):


Code: Select all


uris = [
  uri1,
  uri2,
  ...,
  uriN
]

cflib.crtp.init_drivers()

def setup_logging(scf):
    config = LogConfig(name='pm', period_in_ms=500)
    config.add_variable('pm.vbat', 'float')
    scf.cf.log.add_config(config)

    def callback(timestamp, data, config):
        print(f'[{scf.cf.link_uri}: {timestamp}][{logconf.name}]: ', end='')
      	for name, value in data.items():
            print(f'{name}: {value:3.3f} ', end='')
        print()
        
    config.data.received_cb.add_callback(callback)

factory = CachedCfFactory(rw_cache='./cache')
with Swarm(uris, factory=factory) as swarm:
    self.parallell_safe(setup_logging)
    time.sleep(100)
Regards
Jonas
undays
Beginner
Posts: 12
Joined: Sat Oct 30, 2021 1:20 am

Re: Battery Logging with Example Script

Post by undays »

Hi I tried writing a variation of that code you posted to get a swarm to ready battery levels but it was unable to do so. I am getting a weird python syntax error (new to python) so if you could help me out that would be cool

My code is shared https://pastebin.com/1rmBeLxr as I got banned from posting before because of the length of my code I think.

Basically my issue is that my method when I call swarm.parallel it would not recognize the method I defined. I used the method you created above. Thanks in advance.
jonasdn
Expert
Posts: 132
Joined: Mon Mar 01, 2021 3:13 pm

Re: Battery Logging with Example Script

Post by jonasdn »

Hi, could you please paste the error you receive?
undays
Beginner
Posts: 12
Joined: Sat Oct 30, 2021 1:20 am

Re: Battery Logging with Example Script

Post by undays »

Image

This is the error I am getting when trying to run the code for my two crazyflie drones. I feel like this is a really simple python syntax thing that I am not getting. I don't really understand why when I call swarm.parallel_safe(setup_logging) it is unable to find the method that we defined already in the LoggingExample class. Any clarification would be grateful to debug this problem. Thanks.
Last edited by undays on Thu Nov 11, 2021 4:04 am, edited 1 time in total.
undays
Beginner
Posts: 12
Joined: Sat Oct 30, 2021 1:20 am

Re: Battery Logging with Example Script

Post by undays »

https://imgur.com/gallery/GUmEO0C sorry this is the link I don't know why the image does not load.
jonasdn
Expert
Posts: 132
Joined: Mon Mar 01, 2021 3:13 pm

Re: Battery Logging with Example Script

Post by jonasdn »

Hi,

It should probably be le.setup_logging.
undays
Beginner
Posts: 12
Joined: Sat Oct 30, 2021 1:20 am

Re: Battery Logging with Example Script

Post by undays »

I am trying to print simultaneously the battery levels of my 2 drones. The code I have and am using is linked here https://pastebin.com/NiMuAZkP

I am trying to work through my GetBattery method which would display the battery however to my attempts, it seems to only display the first URI0 CrazyFlie's values. Any idea on how I can approach this to print out the battery level for my swarm? I am using the basiclog.py LoggingExample class to add variables which I will also include in this link here https://pastebin.com/4uYEs4sJ

Thanks.
jonasdn
Expert
Posts: 132
Joined: Mon Mar 01, 2021 3:13 pm

Re: Battery Logging with Example Script

Post by jonasdn »

This is more of a Python question unfortunately, you will have to keep learning and dig at it!

But, I would say, maybe a better approach would be to set up asynchronous logging callbacks (see this guide: https://www.bitcraze.io/documentation/r ... log_param/) for each of your Crazyflies and handle the battery displaying in those callbacks?

Good luck!
undays
Beginner
Posts: 12
Joined: Sat Oct 30, 2021 1:20 am

Re: Battery Logging with Example Script

Post by undays »

ah yes, it finally worked out thanks for your guidance. I also am curious about the value I am recording, they are in intervals of 10%? Is the CrazyFlie limitation only able to read the battery level in output intervals of 10%? I am currently using pm.batteryLevel to retrieve the data regarding battery life. Thanks.
Post Reply