pyrobot.brewery is being developed as a way to brew beer. pyrobot.brewery provides a collection of tanks, and the tanks hold devices. The devices respond to messages from the pyrobot.notification system. For this example I'm going to make a system that only has one tank, and that tank has four devices - a thermometer, a valve (represents a solenoid valve that is used to fill the tank), a burner, and a level indictator (represents a device that monitors how much liquid is in the tank).
>>> from pyrobot.brewery.devices import Thermometer, LevelIndicator, ValveSwitch, Burner
>>> from pyrobot.brewery.tests.mock_driver import MockDriver
The MockDriver class is a fake i/o device, but for a real brewery it would be a python class that follows the api defined by pyrobot.drivers.interfaces.IDriver. This project has drivers defined for an Arduino, and a velleman k8055. Currently I am using an Arduino Mega for all hardware interaction. If you want to use something else, all you have to do is write your own.
The easiest way to define your setup is with a dictionary. Here is the one for this example.
>>> definition = {
... 'io_device': {
... 'class': MockDriver,
... 'kw' : {},
... },
... 'tanks' : {
... 'my_tank' : {
... 'input_devices': {
... 'thermometer': {
... 'class': Thermometer,
... 'channel': 0,
... 'calibration_data': None,
... },
... 'level_indicator' : {
... 'class': LevelIndicator,
... 'channel' : 1,
... 'calibration_data' : [[0, 0.0], [255, 12.0]],
... },
... },
... 'output_devices' : {
... 'fill_valve' : {
... 'class': ValveSwitch,
... 'channel' : 2,
... },
... 'burner' : {
... 'class': Burner,
... 'channel' : 3,
... },
... },
... },
... }
... }
... }
Now, just pass the definition to the brewery factory.
>>> from pyrobot.brewery import Factory
>>> factory = Factory(brewery_def = definition)
>>> brewery = factory.brewery
Since this brewery uses a fake io device, we must manipulate it by hand to simulate real temperature changes, etc. The brewery is callable, and the __call__ method takes a method as its argument. So I will make a simple method that fills the tank with 10.0 liters of water, then heats the water to 80 degrees C.
>>> my_method = [
... ('fill my_tank', {'volume': 10.0}),
... ('wait', {'message': 'my_tank target volume reached'}),
... ('heat my_tank', {'temperature': 80.0}),
... ]
A method is just a list of messages. The devices respond to the messages. Each tank in the system must have a unique name in order for the devices of the tank to respond to the messages for the tank.
I'll make sure the fill valve isn't on
>>> print brewery.tanks['my_tank'].devices['fill_valve'].state
False
Now, start the method:
>>> brewery(my_method)
Since the first part of the method fills the tank, then that device must be on.
>>> print brewery.tanks['my_tank'].devices['fill_valve'].state
True
Now I will simulate water going into the tank.
>>> volume = 0.0
>>> for i in xrange(11):
... volume += 1.0
... state={'my_tank':{'level_indicator':volume}}
... brewery.notification_center.post_message(message = 'new brew cycle', state=state)
>>> print '%.01f' % brewery.tanks['my_tank'].devices['level_indicator'].value
10.0
>>> print brewery.tanks['my_tank'].devices['fill_valve'].state
False
The tank now has 10.0 liters in it. The method says that it should now be heating the tank up:
>>> print brewery.tanks['my_tank'].devices['burner'].state
True
Once again, I must fake the heating of the tank:
>>> temperature = 70.0
>>> for i in xrange(80):
... temperature += 1.0
... state={'my_tank':{thermometer':temperature}}
... brewery.notification_center.post_message(message = 'new brew cycle', state=state)
>>> print '%.01f' % brewery.tanks['my_tank'].devices['thermometer'].value
80.0
>>> print brewery.tanks['my_tank'].devices['burner'].state
False
Hooray! The tank is now filled and heated. A complete brew system would contain more tanks and a brew method would do more stuff. See pyrobot.brewery.definitions.default for the default setup, and see pyrobot.brewery.methods.brew for the default brew method.
>>> brewery.stop()
Product's homepage
Requirements:
· Python
What's New in This Release: [ read full changelog ]
· A complete rewrite based on zmq communication.