PDA

View Full Version : State machine for DIY controller


gbru316
11/04/2014, 06:36 PM
Anyone use their Arduino as a state-machine?. Being that aquarium controllers are predominantly event driven (when time = xxxx, do this, if switch = 1, turn on, etc) and are not used to do much actual processing, it seems that a state machine may be a better route.

The state machine allows processes to run simultaneously instead of requiring one process to finish before starting the next. For instance, it may not be uncommon for the ATO to be running while a dose is scheduled. Or the heater to turn on while dosing. Or any combination of tasks.

I know there isn't a ton of people using DIY controllers, but I thought I'd pose the question anyway.

87ford
11/04/2014, 06:46 PM
Hello may I ask the reason for a state machine doesn't a state machine involve algorithms and loops? So you would go through so many times till it finds the value it wants and then go to the next loop my guess would be for dosing?

Tom

gbru316
11/04/2014, 06:53 PM
Hello may I ask the reason for a state machine doesn't a state machine involve algorithms and loops? So you would go through so many times till it finds the value it wants and then go to the next loop my guess would be for dosing?

Tom

What you describe is how conventional code works. The loop runs constantly looking for the first initial condition to be satisfied. It then is "stuck" in this condition until the exit condition is satisfied. It cannot check for other conditions if it is already handling one.

For instance, the main loop is running. Halfway through, it sees that the ATO condition is satisfied. It enters the statement controlling the ATO. While the ATO is dosing, the clock indicates it is time to dose. Since the processor is still handling the top-off condition, it cannot dose. By the time the top-off is complete, it is past the dosing time and that condition is no longer satisfied so that dose is skipped.

From my reading, a state-machine is able to execute the ATO condition while still monitoring and executing other conditions, such as dosing.

dartier
11/04/2014, 06:56 PM
It is an interesting idea. I favour, and use, python running on Raspberry Pi and Beaglebone Black's for my controllers. A a lot of my peripherals use I2C for communication, so I am always concerned about collisions on the I2C bus. Using a finite state machine may help to avoid the possibility.

Dennis

87ford
11/04/2014, 07:00 PM
hmmm i see what your saying but for your condition wouldn't it make sense to check the time related things first in your main loop considering everything else is prop related to a sensor which will always have a reading regardless of time? i understand the problem though and thank you for guiding me on what is a state machine! i would have no clue on how to go about on creating one.

Tom

gbru316
11/04/2014, 07:03 PM
Here's what I'm researching at the moment.

QP for arduino (http://www.state-machine.com/arduino/)

I'm in the early stages of a controller. While I'm waiting for dosing pumps and ATO switches, I have it monitoring temp and dosing based on time. Simple enough, but once I add a controllable power strip for all tank devices, datalogging, dosing and switch-based ATO, the code will become much more complex and timing will be very critical. It seems like a state machine may be a way to avoid that (and it has been mentioned as a better solution on the arduino forums regarding aquarium controllers).

87ford
11/04/2014, 07:14 PM
very nice i assume you have the uno? i have the uno but was limited on the inputs to do much at all. it looks like there is a way to use C++ if what I'm seeing is correct instead of the traditional language arduino uses which i believe is pyhton is the software free or cost money? you got me interested in it now lol

gbru316
11/04/2014, 07:17 PM
very nice i assume you have the uno? i have the uno but was limited on the inputs to do much at all. it looks like there is a way to use C++ if what I'm seeing is correct instead of the traditional language arduino uses which i believe is pyhton is the software free or cost money? you got me interested in it now lol

Arduino is basically C/C++. I do have an uno. If I needed more IO ports, I'd use an I2C expander and/or multiplexor IC, or convert some of the analog ports to digital using an ADC. Even so, it has all the ports I need for dosing, top off, control of heater, lights, pumps, temp and ph monitoring and an LCD display. I'm using the I2C bus for the display and clock and 1 digital port for a 1 wire bus. That leaves 13 digital and 4 analog ports to control everything else. 2 DIO ports for ATO, 3 for dosing, and 6 for power control. I'm left with 2 DIO and 4 analog ports. And that's not including any expansion via ADC/DAC, mux or I2C. You could probably control just about every "on/off" device with a few analog ports using ADC and some op-amps.

the Arduino language is merely a set of C/C++ functions that can be called from your code. Your sketch undergoes minor changes (e.g. automatic generation of function prototypes) and then is passed directly to a C/C++ compiler (avr-g++). All standard C and C++ constructs supported by avr-g++ should work in Arduino.

The software I posted is free to use.

87ford
11/04/2014, 07:28 PM
very nice i wanted to put a display and monitor things for mine but didn't have much inputs left. thanks for the info so basically the compiler is C/C++ and the IDE that arduino gave me that i use is pyhton based and is then converted into the C/C++ and stored directly on the arduino ? is that correct?

gbru316
11/04/2014, 07:32 PM
very nice i wanted to put a display and monitor things for mine but didn't have much inputs left. thanks for the info so basically the compiler is C/C++ and the IDE that arduino gave me that i use is pyhton based and is then converted into the C/C++ and stored directly on the arduino ? is that correct?


Hm, I'm not sure. I've never knowingly used python with arduino.

Regarding the display, there are I2C displays available. They only use 2 pins for data: SDA and SCL (which correspond to analogs 4 and 5 on the uno). I paid $20 for mine from adafruit. And for temp monitoring, I'm using a ds18b20 that communicates via 1-wire protocol, which as it's name implies, only uses 1 wire/port.

BeanAnimal
11/05/2014, 11:08 AM
What you describe is how conventional code works. The loop runs constantly looking for the first initial condition to be satisfied. It then is "stuck" in this condition until the exit condition is satisfied. It cannot check for other conditions if it is already handling one. I think you are conflating several different programming paradigms and cpu/tasking paradigms.

A state machine, or more properly a Finite State Machine is used to describe program flow based on finite states, or easier stated, sequential logic. How those states are determined is not really relevant to the concept. The state of the machine at any given moment is a product of the previous state and the result of the current event (polled for, timed, or the result of computation) that the state machine encounters.

The machine may, or may not be able to track multiple processes, but regardless the logic is "sequential". Furthermore, there is no logical law the prohibits a finite state machine from using sub finite state logic to derive the overall state.

For instance, the main loop is running. Halfway through, it sees that the ATO condition is satisfied. It enters the statement controlling the ATO. While the ATO is dosing, the clock indicates it is time to dose. Since the processor is still handling the top-off condition, it cannot dose. By the time the top-off is complete, it is past the dosing time and that condition is no longer satisfied so that dose is skipped.

From my reading, a state-machine is able to execute the ATO condition while still monitoring and executing other conditions, such as dosing. The behavior you describe could be implemented in a finite state machine, but could also be implemented via a behavior tree or other programming paradigms.

In context to the uC, my preference for implementation of an FSM is to use a game loop to determine and switch between states. You can find some basic examples on my site...

http://www.beananimal.com/projects/automated-rodi-water-filtration-system-part-2.aspx

In this case, the entire project is state driven, including hardware function, menu system and display. The page shows the game loop that is used to poll for state changes, the LCD and MENU state machine and the state machine that governs the actual operation of the RODI system.

Hope that helps a bit...

gbru316
11/05/2014, 11:24 AM
I think you are conflating several different programming paradigms and cpu/tasking paradigms.

A state machine, or more properly a Finite State Machine is used to describe program flow based on finite states, or easier stated, sequential logic. How those states are determined is not really relevant to the concept. The state of the machine at any given moment is a product of the previous state and the result of the current event (polled for, timed, or the result of computation) that the state machine encounters.

The machine may, or may not be able to track multiple processes, but regardless the logic is "sequential". Furthermore, there is no logical law the prohibits a finite state machine from using sub finite state logic to derive the overall state.

The behavior you describe could be implemented in a finite state machine, but could also be implemented via a behavior tree or other programming paradigms.

In context to the uC, my preference for implementation of an FSM is to use a game loop to determine and switch between states. You can find some basic examples on my site...

http://www.beananimal.com/projects/automated-rodi-water-filtration-system-part-2.aspx

In this case, the entire project is state driven, including hardware function, menu system and display. The page shows the game loop that is used to poll for state changes, the LCD and MENU state machine and the state machine that governs the actual operation of the RODI system.

Hope that helps a bit...

It does, thanks. My background in programming is limited to a required basic C++ class. I'm more of a hardware guy (but that does require some knowledge of the interface between the two, doesn't it?)

Looking into the link I posted, it appears that there are multiple state machines running (called active objects) under a framework. Not really sure what type of structure this is.

Thanks also for the link to your site. I'll be perusing that when I have some time as I'm sure there is a ton of good info there for a fellow DIY'er.

disc1
11/05/2014, 12:07 PM
very nice i wanted to put a display and monitor things for mine but didn't have much inputs left. thanks for the info so basically the compiler is C/C++ and the IDE that arduino gave me that i use is pyhton based and is then converted into the C/C++ and stored directly on the arduino ? is that correct?
Nope, it is all C++. The IDE itself is written in Java, but everything that has to do with the code you write in it is C++. Python is not involved in any way shape form or fashion.

87ford
11/11/2014, 11:59 AM
Nope, it is all C++. The IDE itself is written in Java, but everything that has to do with the code you write in it is C++. Python is not involved in any way shape form or fashion.

yes your right i just opened the IDE its been a year since needed to use it lol

jimmcwibb
11/11/2014, 05:47 PM
Having done a bit of game programming I've thought about running my controller like a game loop for a while. But, right now I don't have enough tasks to make it worthwhile. The ATO can wait until the dose has finished, for example. Important events can use interrupts if need be.

TimmyD16
11/11/2014, 07:40 PM
Have you looked at the Netduino? it is a .NET based duino that is programed with C#

Take a look at this thread:http://www.reefcentral.com/forums/showthread.php?t=2250389

Here is the Netduino page: http://netduino.com/