Page 1 of 2
Battery Logging with Example Script
Posted: Sun Nov 07, 2021 3:02 am
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?
Re: Battery Logging with Example Script
Posted: Mon Nov 08, 2021 6:19 am
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
Re: Battery Logging with Example Script
Posted: Tue Nov 09, 2021 1:50 am
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.
Re: Battery Logging with Example Script
Posted: Tue Nov 09, 2021 5:25 am
by jonasdn
Hi, could you please paste the error you receive?
Re: Battery Logging with Example Script
Posted: Thu Nov 11, 2021 3:58 am
by undays
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.
Re: Battery Logging with Example Script
Posted: Thu Nov 11, 2021 3:58 am
by undays
https://imgur.com/gallery/GUmEO0C sorry this is the link I don't know why the image does not load.
Re: Battery Logging with Example Script
Posted: Thu Nov 11, 2021 12:21 pm
by jonasdn
Hi,
It should probably be le.setup_logging.
Re: Battery Logging with Example Script
Posted: Fri Nov 12, 2021 7:08 pm
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.
Re: Battery Logging with Example Script
Posted: Mon Nov 15, 2021 5:37 am
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!
Re: Battery Logging with Example Script
Posted: Tue Nov 16, 2021 1:25 am
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.