Darwin-River / Robotics

0 stars 0 forks source link

Allow for multiple state machines that can start and stop one another #6

Open darozak opened 6 months ago

darozak commented 6 months ago

If I allow state machines to be paused, resumed, and reset, it will be possible to have multiple state machines working side-by-side in a robot. For example, this would allow me to have one process working in the background to monitor the audio and start and stop the robot whenever it hears a loud noise.

I could also add a go to function that permits external control of what state a state machine is in.

I'll be making these modifications to the StateMachine class.

darozak commented 6 months ago

Here is what I've accomplished so far:

231203 - Added start, stop, and resume functions to the StateMachine class. I started modifying the [[Line Racer Sketch]] so that it adds a state machine to control how long the robot runs for. However, I just realized that for this to work I'll need to create the action and event handles outside of the StateMachine class by using the bit() function to assign a bit to each action or event handle. At the same time, I need to allow the user to define actions that the state machine will perform upon being stopped. This won't be hard to implement.

231205 - I modified the StateMachine class to have start and stop commands which will activate or deactivate the state machine using a set of predefined start and stop actions. Next I need to switch to using the bit() function with action and event handles and then test the code to make sure it works.

231206 - Set up a Wokwi project to troubleshoot the new state machine code.

231209 - Debugged part of the code. But still trying to get the Listen SM to start and stop the Follow SM.

darozak commented 6 months ago

I took some time to regroup and organize my ino and helper files in the Code Library directory (https://github.com/Darwin-River/Robotics/commit/7e7576b6bb855cdc7dd736c7b3d9d54ca9a5e6f4). I had a number of different versions of the Line Racer code and classes floating around in different places and I wanted to make sure I was working with the most current version.

From now on, I'm going to keep the code separate from the Markdown documents that describe their use.

darozak commented 6 months ago

I needed to further consolidate the Line Racer code into a LineRacer directory so that I can access it via the Arduino GUI (https://github.com/Darwin-River/Robotics/commit/245d2c40ca00dee5a497a43eee8a1e278dbf1bdc). Everything should be ready now for me to start coding. Fingers crossed.

darozak commented 6 months ago

I had to correct an error in which I accidently duplicated the StateMachine helper file in the ino file (https://github.com/Darwin-River/Robotics/commit/f174c63fdd71f611a3bc62e0d24806fd5d91bd26). Now the code compile correctly in Arduino GUI. I just need to see how it runs.

darozak commented 6 months ago

I found a number of issues.

First, the listen SM was never getting out of state 0 because I was registering the LTA with the follow SM and not the listen SM. I corrected this here (https://github.com/Darwin-River/Robotics/commit/1a95eb98e3a98d3fb285cbc402718287ebb96182).

The second, and more important issue is that the current setup requires me to log events with both SMs. It would be much more efficient to create a compound event that I can share with all of the state machines.

It would also be helpful to compile action requests from all SMs in one output so I don't need to write action requests for each SM.

The best way to handle this may be to create a single SM object that is capable of handling the actions of multiple SMs. That way, this SM object could handle the events and actions for all of the SMs, including assigning event and action handles.

Start coding a multi-SM SM Engine Class.

darozak commented 6 months ago

I replaced the old StateMachnine Class with a new StateMachines Class and recoded it to handle multiple state machines (https://github.com/Darwin-River/Robotics/commit/48351926522ce67906a4fd67912033f4ddb0c757). Next, I need to update the main sketch to work with the new class and then test it out.

darozak commented 6 months ago

I renamed the StateMachines Class to StateEngine and implemented the code in the LineRacer sketch (https://github.com/Darwin-River/Robotics/commit/e4fb7eff3612421c62778d6c31db2514870a8751). Everything compiles correctly. However, the robot isn't performing as expected. I need to add a trouble shooting function the the StateEngine Class.

darozak commented 6 months ago

I created a simple debugging routine in which the user can turn a debug report off and on for a particular state machine by calling engine.debug(MACHINE, true); (https://github.com/Darwin-River/Robotics/commit/4e544327372a7bddfb20e6dcc70cacadac1d76f8).

I'll use this to figure out why the current setup isn't working as expected.

darozak commented 6 months ago

I fixed the StateEngine class so that it's working correctly (https://github.com/Darwin-River/Robotics/commit/d3f7da17e10b4d6605131aff549ad3a758a20b9b). I did this by using or's instead of and's to concatenate state action requests and using >= instead of > for the do loop in the evaluate function.

Now I need to document the new class in the figure out the best way to document this update on GitHub and in a video.

darozak commented 5 months ago

I started drafting a README.md file in the LineRacer code library directory that tells how how the Line Racer sketch is designed (https://github.com/Darwin-River/Robotics/commit/a18a5d55a3612a3d74432086d5a0c50b0b9d9d19). I'll develop this further until I can use it as a guide for my video update and fully captures the design of the main sketch and all supporting classes.

darozak commented 5 months ago

I've begun describing the two Line Racer state machines (https://github.com/Darwin-River/Robotics/commit/65ca47ae0400a0abdc660dc397856ba5c389d80f). In the processes, I realized that I need to come up with a strategy for diagramming each state machine's start and stop actions.

darozak commented 5 months ago

I started detailing the StateEngine Class in a Markdown file (https://github.com/Darwin-River/Robotics/commit/855a8b7bae09580f63f3b4a28128d90bf07914b2) and renamed the README.md to LineRacer.md (https://github.com/Darwin-River/Robotics/commit/a2fe89da33cd6aa3b627f686c2287ce252e00ccb). I will continue using this file to document the LineRacer.ino sketch. Finally I moved all folders out of the LineRacer directory and into the general Code_Library directory (https://github.com/Darwin-River/Robotics/commit/946dca95049a872efca11931d8ea2ff51defc4bc). I've decided to use the Code_library directory to hold general robot classes as well as specific sketches for the different challenge robots that I'm developing. Each of these sketch and Class files will be accompanied by a Markdown file that describes their use. This will be a more generalizable approach to structuring the code library.

Next, I need to figure out a good way to document the start and stop actions for the state machines and write this into the Markdown files.

darozak commented 5 months ago

I figured out a way to diagram state machine start and stop actions with Mermaid (https://github.com/Darwin-River/Robotics/commit/2819d17de44d8a6b796e6d203c0d34060db61fb6). The solution is to surround the state machine with a box and have start and stop arrows leading into and out of the box with annotated actions.

Now I can continue to document the initial Line Racer code using Markdown files.

darozak commented 5 months ago

Finished diagramming the Listen and Follow state machines (https://github.com/Darwin-River/Robotics/commit/9278df3741b30ae718f566953602b9657acea295) in LineRacer.md. This Markdown file refers to the Line Racer sketch as well as Markdown files that describe each of the Classes that are used by the sketch. Next, I need to document each of the Classes.

darozak commented 5 months ago

I finished documenting the StateEngine Class (https://github.com/Darwin-River/Robotics/commit/c492bfdb408f07323aed9f4a779affe9ba4e49ca). Now I'd like to do the same for the Drive, Sensor, and Timer Classes.

darozak commented 5 months ago

Started adding comments to the code to serve as instructions for using the various classes (https://github.com/Darwin-River/Robotics/commit/0e2d83a59d483b1dcd83d1c0b11c1bd888ece613).