Hi everyone!
I’ve been working on hacking my roomba in HA and basically I have it 95% working, running off a Wemos D1 Mini. I could stop here but there’s this one other thing I’m trying to do and it’s got me scratching my head.
For most of the operations on a Roomba, I can imply use Serial.write()
and it works fine, but when I need to read data back (battery, operation state, etc), there’s no super straightforward way of doing it so I’m leaning on the the Roomba library for Arduino by Mike McCauley, which makes getting the sensor data out pretty straight forward since it sets up all the structures you need for this. However, in order to do this, I have to initialize a new Roomba object every time I want to make a sensor call, which works, but the initialization process also resets some states I’m trying to preserve, which makes it less than ideal.
So my question: Is it possible for me to initialize a object as a global, rather that in the lambda context?
Some sample code here that is working.
First, I initialize some stuff in the boot loop. This declaration is only valid during this specific lambda’s context but it works because it gets the hardware ready to do its thing.
...
includes:
- includes/Roomba.h
- includes/Roomba.cpp
on_boot:
then:
- lambda: |-
Roomba myRoomba(&Serial, Roomba::Baud115200);
myRoomba.start();
...
However, when I need to do a read operation later, I have to declare a new object. This causes a couple of things to be reinitialized on the hardware that I don’t want.
...
- id: read_sensors
then:
- lambda: |-
Roomba myRoomba(&Serial, Roomba::Baud115200);
myRoomba.start();
...
Since I don’t want to reset internal states just to read sensor data, is it possible to declare a global pointer, and have the boot initialized object be pass off to that so it persists across all the lambdas? If I could do that, I’d probably move a lot of my calls from direct Serial.write()
s all over to library calls for consistency and readability. Looking for something relatively simple and elegant and hoping there’s an obvious way to do this that I just haven’t stumbled upon in the documentation.
Apologies if all my terminology is all over the place and I hope that makes sense. I’m a bit of an amateur at this.