dragonfly.implementations.pid_loop module¶
Some template vars¶
Members: PidController Functions: Classes:
Implementation of a PID control loop
Summary¶
-
class
dragonfly.implementations.pid_loop.PidController(input_channel, output_channel, check_channel, status_channel, payload_field='value_cal', tolerance=0.01, target_value=110, proportional=0.0, integral=0.0, differential=0.0, maximum_out=1.0, minimum_out=1.0, delta_out_min=0.001, enable_offset_term=True, minimum_elapsed_time=0, **kwargs)¶ Bases:
dragonfly.implementations.pid_loop.PidControllerImplementation of a PID control loop with constant offset. That is, the PID equation is used to compute the change to the value of some channel and not the value itself. In the case of temperature control, this makes sense if the loop is working against some fixed load (such as a cryocooler).
The input sensor can be anything which broadcasts regular values on the alerts exchange (using the standard sensor_value.<name> routing key format). Usually this would be a temperature sensor, but it could be anything. Similarly, the output is anything that can be set to a float value, though a current output is probably most common. After setting the new value of current, this value is checked to be within a range around the desired value.
NOTE The “exchange” and “keys” arguments list below come from the Service class but are not valid for this class. Any value provided will be ignored
Keyword Arguments: - input_channel (str) – name of the logging sensor to use as input to PID (this will override any provided values for keys)
- output_channel (str) – name of the endpoint to be set() based on PID
- check_channel (str) – name of the endpoint to be checked() after a set()
- status_channel (str) – name of the endpoint which controls the status of the heater (enabled/disabled output)
- payload_field (str) – name of the field in the payload when the sensor logs (default is ‘value_cal’ and ‘value_raw’ is the only other expected value)
- target_value (float) – numerical value to which the loop will try to lock the input_channel
- proportional (float) – coefficient for the P term in the PID equation
- integral (float) – coefficient for the I term in the PID equation
- differential (float) – coefficient for the D term in the PID equation
- maximum_out (float) – max value to which the output_channel may be set; if the PID equation gives a larger value this value is used instead
- delta_out_min (float) – minimum value by which to change the output_channel; if the PID equation gives a smaller change, the value is left unchanged (no set is attempted)
- tolerance (float) – acceptable difference between the set and get values (default: 0.01)
- minimum_elapsed_time (float) – minimum time interval to perform PID calculation over
- exchange (str) – (overrides Service default)
- keys (list|str) – (overrides Service default)
- broker (str) – The AMQP url to connect with
- exchange – Name of the AMQP exchange to connect to
- keys – binding key or list of binding keys to use listen against
- name (str) – name for the amqp queue, automatically generated if None (this behavior supplements the Endpoint arg of the same name)
- setup_calls (list of dicts) – each element is a dictionary describing a method to call. Valid keys are target, method, args, and kwargs, which will be called as service.endpoints[target].method(*args,**kwargs). Note that on_set can be used to assign values to attributes in this syntax.
- name – unique identifier across all dripline services (used to determine routing key)
- calibration (str||dict) – string use to process raw get result (with .format(raw)) or a dict to use for the same purpose where raw must be a key
- get_on_set (bool) – flag to toggle running ‘on_get’ after each ‘on_set’
input_channel (str): name of the logging sensor to use as input to PID (this will override any provided values for keys) output_channel (str): name of the endpoint to be set() based on PID check_channel (str): name of the endpoint to be checked() after a set() status_channel (str): name of the endpoint which controls the status of the heater (enabled/disabled output) payload_field (str): name of the field in the payload when the sensor logs (default is ‘value_cal’ and ‘value_raw’ is the only other expected value) target_value (float): numerical value to which the loop will try to lock the input_channel proportional (float): coefficient for the P term in the PID equation integral (float): coefficient for the I term in the PID equation differential (float): coefficient for the D term in the PID equation maximum_out (float): max value to which the output_channel may be set; if the PID equation gives a larger value this value is used instead delta_out_min (float): minimum value by which to change the output_channel; if the PID equation gives a smaller change, the value is left unchanged (no set is attempted) tolerance (float): acceptable difference between the set and get values (default: 0.01) minimum_elapsed_time (float): minimum time interval to perform PID calculation over
-
EXCHANGE_TYPE= 'topic'¶
-
acknowledge_message(delivery_tag)¶ Acknowledge the message delivery from RabbitMQ by sending a Basic.Ack RPC method for the delivery tag.
Parameters: delivery_tag (int) – The delivery tag from the Basic.Deliver frame
-
add_endpoint(endpoint)¶
-
add_on_cancel_callback()¶ Add a callback that will be invoked if RabbitMQ cancels the consumer for some reason. If RabbitMQ does cancel the consumer, on_consumer_cancelled will be invoked by pika.
-
add_on_channel_close_callback()¶ This method tells pika to call the on_channel_closed method if RabbitMQ unexpectedly closes the channel.
-
add_on_connection_close_callback()¶ This method adds an on close callback that will be invoked by pika when RabbitMQ closes the connection to the publisher unexpectedly.
-
close_channel()¶ Call to close the channel with RabbitMQ cleanly by issuing the Channel.Close RPC command.
-
close_connection()¶ This method closes the connection to RabbitMQ.
-
cmd(target, method_name, value=[], payload={}, lockout_key=False, timeout=None, ignore_retcode=False)¶
-
connect()¶ This method connects to RabbitMQ, returning the connection handle. When the connection is established, the on_connection_open method will be invoked by pika.
Return type: pika.SelectConnection
-
endpoint(endpoint)¶
-
endpoint_names¶
-
endpoints¶
-
get(target, timeout=None, ignore_retcode=False)¶
-
handle_request(channel, method, properties, request)¶
-
is_locked¶
-
keys¶
-
list_endpoints()¶
-
lock(*args, **kwargs)¶
-
lockout_key¶
-
logging_status¶
-
on_alert_message(channel, method, properties, message)¶
-
on_any_message(**kwargs)¶
-
on_bindok(unused_frame)¶ Invoked by pika when the Queue.Bind method has completed. At this point we will start consuming messages by calling start_consuming which will invoke the needed RPC commands to start the process.
Parameters: unused_frame (pika.frame.Method) – The Queue.BindOk response frame
-
on_cancelok(unused_frame)¶ This method is invoked by pika when RabbitMQ acknowledges the cancellation of a consumer. At this point we will close the channel. This will invoke the on_channel_closed method once the channel has been closed, which will in-turn close the connection.
Parameters: unused_frame (pika.frame.Method) – The Basic.CancelOk frame
-
on_channel_closed(channel, reply_code, reply_text)¶ Invoked by pika when RabbitMQ unexpectedly closes the channel. Channels are usually closed if you attempt to do something that violates the protocol, such as re-declare an exchange or queue with different parameters. In this case, we’ll close the connection to shutdown the object.
Parameters: - pika.channel.Channel – The closed channel
- reply_code (int) – The numeric reason the channel was closed
- reply_text (str) – The text reason the channel was closed
-
on_channel_open(channel)¶ This method is invoked by pika when the channel has been opened. The channel object is passed in so we can make use of it.
Since the channel is now open, we’ll declare the exchange to use.
Parameters: channel (pika.channel.Channel) – The channel object
-
on_cmd(*args, **kwargs)¶ WARNING! if you override this method, you must ensure you deal with lockout properly
-
on_config(attribute, value=None)¶ configure a property again
WARNING! if you override this method, you must ensure you deal with lockout properly
-
on_connection_closed(connection, reply_code, reply_text)¶ This method is invoked by pika when the connection to RabbitMQ is closed unexpectedly. Since it is unexpected, we will reconnect to RabbitMQ if it disconnects.
Parameters: - connection (pika.connection.Connection) – The closed connection obj
- reply_code (int) – The server provided reply_code if given
- reply_text (str) – The server provided reply_text if given
-
on_connection_open(unused_connection)¶ This method is called by pika once the connection to RabbitMQ has been established. It passes the handle to the connection object in case we need it, but in this case, we’ll just mark it unused.
-
on_consumer_cancelled(method_frame)¶ Invoked by pika when RabbitMQ sends a Basic.Cancel for a consumer receiving messages.
Parameters: method_frame (pika.frame.Method) – The Basic.Cancel frame
-
on_exchange_declareok(unused_frame)¶ Invoked by pika when RabbitMQ has finished the Exchange.Declare RPC command.
Parameters: unused_frame (pika.Frame.Method) – Exchange.DeclareOk response frame
-
on_message(unused_channel, basic_deliver, properties, body)¶ Invoked by pika when a message is delivered from RabbitMQ. The channel is passed for your convenience. The basic_deliver object that is passed in carries the exchange, routing key, delivery tag and a redelivered flag for the message. The properties passed in is an instance of BasicProperties with the message properties and the body is the message that was sent.
Parameters: - unused_channel (pika.channel.Channel) – The channel object
- pika.Spec.Basic.Deliver – basic_deliver method
- pika.Spec.BasicProperties – properties
- body (str|unicode) – The message body
-
on_queue_declareok(method_frame)¶ Method invoked by pika when the Queue.Declare RPC call made in setup_queue has completed. In this method we will bind the queue and exchange together with the routing key by issuing the Queue.Bind RPC command. When this command is complete, the on_bindok method will be invoked by pika.
Parameters: method_frame (pika.frame.Method) – The Queue.DeclareOk frame
-
on_reply_message(**kwargs)¶
-
on_request_message(channel, method, header, body)¶
-
on_send(*commands)¶
-
on_set(*args, **kwargs)¶
-
open_channel()¶ Open a new channel with RabbitMQ by issuing the Channel.Open RPC command. When RabbitMQ responds that the channel is open, the on_channel_open callback will be invoked by pika.
-
ping(*args, **kwargs)¶ ignore all details and respond with an empty message
-
process_new_value(value, timestamp)¶
-
reconnect()¶ Will be invoked by the IOLoop timer if the connection is closed. See the on_connection_closed method.
-
run()¶ Run the example consumer by connecting to RabbitMQ and then starting the IOLoop to block and allow the SelectConnection to operate.
-
schedule_status¶
-
send_alert(alert, severity)¶
-
send_message(target, message, return_queue=None, properties=None, exchange=None, return_connection=False, ensure_delivery=True)¶
-
send_reply(properties, reply)¶
-
send_request(target, request, timeout=10, multi_reply=False)¶ It seems like there should be a way to do this with the existing SelectConnection. The problem is that the message handler needs to send a request and then be called not block the reply from being processed, and needs to get the reply from that processed response. The non-blocking part seems tricky. I’m sure there exists a good solution for this, maybe within asyncio and/or asyncore, but I don’t know where it is. This seems to work.
-
send_status_message(alert, severity)¶
-
set(target, value, lockout_key=False, timeout=None, ignore_retcode=False)¶
-
set_condition(*args, **kwargs)¶ To actually do something inside a class, the set_condition should be implemented in the class itself.
-
set_current(value)¶
-
setup_exchange(exchange_name)¶ Setup the exchange on RabbitMQ by invoking the Exchange.Declare RPC command. When it is complete, the on_exchange_declareok method will be invoked by pika.
Parameters: exchange_name (str|unicode) – The name of the exchange to declare
-
setup_queue(queue_name)¶ Setup the queue on RabbitMQ by invoking the Queue.Declare RPC command. When it is complete, the on_queue_declareok method will be invoked by pika.
Parameters: queue_name (str|unicode) – The name of the queue to declare.
-
start()¶ Begin consuming by calling self.run (from Spimescape)
-
start_consuming()¶ This method sets up the consumer by first calling add_on_cancel_callback so that the object is notified if RabbitMQ cancels the consumer. It then issues the Basic.Consume RPC command which returns the consumer tag that is used to uniquely identify the consumer with RabbitMQ. We keep the value to use it when we want to cancel consuming. The on_message method is passed in as a callback pika will invoke when a message is fully received.
-
start_event_loop()¶ Call self.run with controlled stop
-
stop()¶ Cleanly shutdown the connection to RabbitMQ by stopping the consumer with RabbitMQ. When RabbitMQ confirms the cancellation, on_cancelok will be invoked by pika, which will then closing the channel and connection. The IOLoop is started again because this method is invoked when CTRL-C is pressed raising a KeyboardInterrupt exception. This exception stops the IOLoop which needs to be running for pika to communicate with RabbitMQ. All of the commands issued prior to starting the IOLoop will be buffered but not processed.
-
stop_consuming()¶ Tell RabbitMQ that you would like to stop consuming by sending the Basic.Cancel RPC command.
-
target_value¶
-
this_consume(message, method)¶
-
unlock(*args, **kwargs)¶
End of modules condition
Data:
End of data condition