Files
@ 72dd61ca5365
Branch filter:
Location: HydroBot/hydrobot-software/hydrobot.py - annotation
72dd61ca5365
12.8 KiB
text/x-python
Started refactoring with the networking and modules
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 | 6b61f8a36144 6b61f8a36144 d1142fbbace4 6b61f8a36144 1aa7eed26cdb 1aa7eed26cdb 22b6999f8bdd 22b6999f8bdd 653da092aa96 a5b830d92afd d1142fbbace4 d1142fbbace4 4d9b047f8436 1aa7eed26cdb 514bbf7bb55e a5b830d92afd da6947ec7d99 18ae0de640b9 4ccedc2f8509 4ccedc2f8509 a5b830d92afd ce32edfd2399 ce32edfd2399 ce32edfd2399 ce32edfd2399 ce32edfd2399 a5b830d92afd 981a1d0baca8 4ccedc2f8509 ce32edfd2399 d1142fbbace4 4ccedc2f8509 22b6999f8bdd a5b830d92afd 4ccedc2f8509 4ccedc2f8509 4ccedc2f8509 4ccedc2f8509 4ccedc2f8509 4ccedc2f8509 d1142fbbace4 4ccedc2f8509 4ccedc2f8509 4ccedc2f8509 4ccedc2f8509 4ccedc2f8509 4ccedc2f8509 6b61f8a36144 6b61f8a36144 4ccedc2f8509 514bbf7bb55e 514bbf7bb55e 514bbf7bb55e 514bbf7bb55e 514bbf7bb55e 514bbf7bb55e 514bbf7bb55e 514bbf7bb55e 514bbf7bb55e 514bbf7bb55e 514bbf7bb55e 514bbf7bb55e 514bbf7bb55e 514bbf7bb55e 514bbf7bb55e 514bbf7bb55e 514bbf7bb55e 514bbf7bb55e 514bbf7bb55e daaed5044a8c daaed5044a8c daaed5044a8c daaed5044a8c daaed5044a8c daaed5044a8c daaed5044a8c daaed5044a8c 22b6999f8bdd 1dd55863f684 1dd55863f684 1dd55863f684 1dd55863f684 1dd55863f684 1dd55863f684 514bbf7bb55e 514bbf7bb55e 514bbf7bb55e daaed5044a8c daaed5044a8c 1dd55863f684 1dd55863f684 1dd55863f684 1dd55863f684 1dd55863f684 1dd55863f684 1dd55863f684 1dd55863f684 1dd55863f684 1dd55863f684 1dd55863f684 1dd55863f684 1dd55863f684 1dd55863f684 1dd55863f684 1dd55863f684 daaed5044a8c daaed5044a8c d1142fbbace4 6b61f8a36144 6b61f8a36144 6b61f8a36144 6b61f8a36144 6b61f8a36144 6b61f8a36144 daaed5044a8c 6b61f8a36144 4d9b047f8436 6b61f8a36144 daaed5044a8c daaed5044a8c daaed5044a8c daaed5044a8c daaed5044a8c daaed5044a8c daaed5044a8c daaed5044a8c 4d9b047f8436 daaed5044a8c daaed5044a8c daaed5044a8c daaed5044a8c 4d9b047f8436 4d9b047f8436 4d9b047f8436 4d9b047f8436 4d9b047f8436 4d9b047f8436 1aa7eed26cdb daaed5044a8c daaed5044a8c a5b830d92afd a5b830d92afd a5b830d92afd a5b830d92afd a5b830d92afd a5b830d92afd a5b830d92afd 4ccedc2f8509 4d9b047f8436 a5b830d92afd a5b830d92afd 4ccedc2f8509 22b6999f8bdd 22b6999f8bdd 4d9b047f8436 653da092aa96 981a1d0baca8 ef97024d8644 ef97024d8644 ef97024d8644 ef97024d8644 981a1d0baca8 ef97024d8644 ef97024d8644 ef97024d8644 ef97024d8644 981a1d0baca8 ef97024d8644 ef97024d8644 ef97024d8644 ef97024d8644 981a1d0baca8 ef97024d8644 ef97024d8644 ef97024d8644 ef97024d8644 981a1d0baca8 514bbf7bb55e 653da092aa96 653da092aa96 653da092aa96 653da092aa96 653da092aa96 18ae0de640b9 653da092aa96 653da092aa96 653da092aa96 653da092aa96 653da092aa96 653da092aa96 653da092aa96 653da092aa96 653da092aa96 653da092aa96 653da092aa96 653da092aa96 653da092aa96 653da092aa96 653da092aa96 653da092aa96 653da092aa96 653da092aa96 653da092aa96 653da092aa96 653da092aa96 653da092aa96 653da092aa96 da6947ec7d99 da6947ec7d99 18ae0de640b9 da6947ec7d99 da6947ec7d99 da6947ec7d99 6ad70cd126b7 6ad70cd126b7 da6947ec7d99 653da092aa96 653da092aa96 653da092aa96 653da092aa96 6ad70cd126b7 da6947ec7d99 da6947ec7d99 da6947ec7d99 da6947ec7d99 da6947ec7d99 da6947ec7d99 da6947ec7d99 da6947ec7d99 da6947ec7d99 da6947ec7d99 da6947ec7d99 da6947ec7d99 da6947ec7d99 da6947ec7d99 da6947ec7d99 da6947ec7d99 da6947ec7d99 da6947ec7d99 da6947ec7d99 da6947ec7d99 da6947ec7d99 da6947ec7d99 da6947ec7d99 da6947ec7d99 da6947ec7d99 da6947ec7d99 da6947ec7d99 da6947ec7d99 da6947ec7d99 da6947ec7d99 da6947ec7d99 653da092aa96 653da092aa96 653da092aa96 653da092aa96 653da092aa96 4ccedc2f8509 653da092aa96 653da092aa96 653da092aa96 653da092aa96 653da092aa96 653da092aa96 4ccedc2f8509 da6947ec7d99 da6947ec7d99 da6947ec7d99 da6947ec7d99 da6947ec7d99 da6947ec7d99 da6947ec7d99 da6947ec7d99 da6947ec7d99 da6947ec7d99 514bbf7bb55e 653da092aa96 653da092aa96 653da092aa96 1aa7eed26cdb 6b61f8a36144 6b61f8a36144 6b61f8a36144 6b61f8a36144 6b61f8a36144 6b61f8a36144 daaed5044a8c 981a1d0baca8 ce32edfd2399 ce32edfd2399 ce32edfd2399 ce32edfd2399 a5b830d92afd ce32edfd2399 ce32edfd2399 ce32edfd2399 ce32edfd2399 ce32edfd2399 ce32edfd2399 ce32edfd2399 ce32edfd2399 ce32edfd2399 d1142fbbace4 1aa7eed26cdb da6947ec7d99 1aa7eed26cdb 4ccedc2f8509 4ccedc2f8509 4ccedc2f8509 4ccedc2f8509 4ccedc2f8509 1aa7eed26cdb 1aa7eed26cdb 6b61f8a36144 | #!/usr/bin/env python
import sys
import os
import time
import _thread
import ast
import configparser
import datetime
from canard import can, messaging
from canard.hw import socketcan
from canard.file import jsondb
from canard.utils import queue
from influxdb import InfluxDBClient
from influxdb import SeriesHelper
from apscheduler.schedulers.background import BackgroundScheduler
import PID
from pytz import timezone
import logging
import signal
import network
import network_interface
from message import HydroBotMessage
import module
#TODO
#fix temperature offsets
#add periodic output refresh
#add output cleanup on shutdown
# load config file
config = configparser.ConfigParser(allow_no_value = True)
config.read("hydrobot.conf")
# set up logger
logger = logging.getLogger('hydrobot')
log_level = config.get("system", "log_level")
levels = {"CRITICAL" : 50, "ERROR" : 40, "WARNING" : 30, "INFO" : 20, "DEBUG" : 10, "NOTSET" : 0}
logger.setLevel(levels[log_level])
fh = logging.FileHandler('hydrobot.log')
fh.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
logger.addHandler(fh)
logger.info("Starting HydroBot!")
class MySeriesHelper(SeriesHelper):
# Meta class stores time series helper configuration.
class Meta:
# The client should be an instance of InfluxDBClient.
#client = myclient
# The series name must be a string. Add dependent fields/tags in curly brackets.
series_name = '{measurement}'
# Defines all the fields in this time series.
fields = ['value']
# Defines all the tags for the series.
tags = ['measurement']
# Defines the number of data points to store prior to writing on the wire.
bulk_size = 5
# autocommit must be set to True when using bulk_size
autocommit = True
class Database:
def __init__(self):
host = config.get("database", "host")
port = config.get("database", "port")
username = config.get("database", "username")
password = config.get("database", "password")
database = config.get("database", "database")
self.name = config.get("system", "name")
try:
self.client = InfluxDBClient(host, port, username, password, database)
MySeriesHelper.Meta.client = self.client
MySeriesHelper.Meta.series_name = self.name + '.{measurement}'
except:
logger.error("Could not connect to database")
# To manually submit data points which are not yet written, call commit:
#MySeriesHelper.commit()
def log_data(self, msgdb, message):
try:
if message == msgdb.AirSense:
MySeriesHelper(measurement='air_temp', value=(float)(message.Temperature.value))
MySeriesHelper(measurement='air_humidity', value=(float)(message.Humidity.value))
MySeriesHelper(measurement='air_pressure', value=(float)(message.Pressure.value))
if message == msgdb.RelayDriveIn:
MySeriesHelper(measurement='water_flow_rate', value=(float)(message.FlowRate.value))
MySeriesHelper(measurement='input_1', value=(float)(message.Input1.value))
MySeriesHelper(measurement='input_2', value=(float)(message.Input2.value))
MySeriesHelper(measurement='input_3', value=(float)(message.Input3.value))
MySeriesHelper(measurement='input_4', value=(float)(message.Input4.value))
if message == msgdb.WaterSense:
MySeriesHelper(measurement='water_level', value=(float)(message.PercentFull.value))
MySeriesHelper(measurement='water_temp', value=(float)(message.Temperature.value))
except:
logger.error("Could not connect to database")
class CanBus:
def __init__(self, database, interface):
# Bring up CAN interface (maybe do this in a systemd service file)
# Passing random arguments to sudo is super dangerous
os.system("sudo ip link set " + interface + " up type can bitrate 500000")
self.database = database
self.dev = socketcan.SocketCanDev(interface)
self.queue = queue.CanQueue(self.dev)
parser = jsondb.JsonDbParser()
self.msgdb = parser.parse('hydrobot_can.json')
self.temp_msg = self.msgdb.AirSense
self.relay_msg = self.msgdb.RelayDriveIn
self.relay_send_msg = self.msgdb.RelayDriveOut
def start(self):
self.queue.start()
_thread.start_new_thread(self.process_can, ())
def process_can(self):
while True:
frame = self.queue.recv()
if frame != None:
message = self.msgdb.decode(frame)
if message:
logger.debug("Received CAN message! ID: " + hex(message.id))
self.database.log_data(self.msgdb, message)
def send_can(self):
self.relay_send_msg.Nothing.value = 0
if self.relay_send_msg.Output1.value == 0:
self.relay_send_msg.Output1.value = 1
else:
self.relay_send_msg.Output1.value = 0
self.relay_send_msg.Output2.value = 1
self.relay_send_msg.Output3.value = 1
self.relay_send_msg.Output4.value = 1
logger.debug("Send CAN message! ID: " + hex(self.relay_send_msg.id) + " Data: " + str(self.relay_send_msg.data))
self.queue.send(self.relay_send_msg.encode())
def set_output(self, module, output, state):
logger.info("Output! " + module + " " + output + " " + str(state))
msg = self.msgdb.lookup_message(module)
msg.lookup_signal(output).value = state
self.queue.send(msg.encode())
if msg.lookup_signal(output) == msg.Output1:
try:
MySeriesHelper(measurement='output_1', value=state)
except:
logger.error("Could not connect to database")
if msg.lookup_signal(output) == msg.Output2:
try:
MySeriesHelper(measurement='output_2', value=state)
except:
logger.error("Could not connect to database")
if msg.lookup_signal(output) == msg.Output3:
try:
MySeriesHelper(measurement='output_3', value=state)
except:
logger.error("Could not connect to database")
if msg.lookup_signal(output) == msg.Output4:
try:
MySeriesHelper(measurement='output_4', value=state)
except:
logger.error("Could not connect to database")
class Scheduler:
def __init__(self, canbus):
self.canbus = canbus
self.apscheduler = BackgroundScheduler()
self.apscheduler.configure(timezone=timezone('US/Eastern'))
def start(self):
self.apscheduler.start()
for section in config.sections():
if "timer" in section:
items = config.items(section)
for item in items:
if item[0] == 'trigger':
trigger = item[1]
if item[0] == 'module':
module = item[1]
if item[0] == 'output':
output = item[1]
if item[0] == 'on_time':
on_time = ast.literal_eval(item[1])
if item[0] == 'off_time':
off_time = ast.literal_eval(item[1])
if item[0] == 'on_duration':
on_duration = ast.literal_eval(item[1])
if item[0] == 'off_duration':
off_duration = ast.literal_eval(item[1])
if trigger == 'cron':
on_job = self.apscheduler.add_job(self.canbus.set_output, trigger, [module, output, 1], day = on_time[0], day_of_week = on_time[1], hour = on_time[2], minute = on_time[3], second = on_time[4])
off_job = self.apscheduler.add_job(self.canbus.set_output, trigger, [module, output, 0], day = off_time[0], day_of_week = off_time[1], hour = off_time[2], minute = off_time[3], second = off_time[4])
#evalute the current state of the cron trigger and set output on if needed
if on_job.next_run_time > off_job.next_run_time:
self.canbus.set_output(module, output, 1)
else:
self.canbus.set_output(module, output, 0)
if trigger == 'interval':
self.apscheduler.add_job(self.interval_off, trigger, [module, output, section + "_off", section + "_on", on_duration], id = section + "_off", weeks = off_duration[0], days = off_duration[1], hours = off_duration[2], minutes = off_duration[3], seconds = off_duration[4])
timer = self.apscheduler.add_job(self.interval_on, trigger, [module, output, section + "_off", section + "_on", off_duration], id = section + "_on", weeks = on_duration[0], days = on_duration[1], hours = on_duration[2], minutes = on_duration[3], seconds = on_duration[4])
timer.pause()
self.interval_off(module, output, section + "_off", section + "_on", on_duration)
if "pid" in section:
items = config.items(section)
for item in items:
if item[0] == 'module_out':
module_out = item[1]
if item[0] == 'output':
signal_out = item[1]
if item[0] == 'module_in':
module_in = item[1]
if item[0] == 'input':
signal_in = item[1]
if item[0] == 'kp':
kp = item[1]
if item[0] == 'ki':
ki = item[1]
if item[0] == 'kd':
kd = item[1]
if item[0] == 'rate':
rate = item[1]
if item[0] == 'setpoint':
setpoint = item[1]
pid = PID.PID(float(setpoint))
pid.SetKp(kp)
pid.SetKi(ki)
pid.SetKd(kd)
pid.Reset()
self.apscheduler.add_job(self.process_PID, "interval", [pid, module_out, signal_out, module_in, signal_in], seconds = int(rate))
def interval_off(self, module, output, timer_off, timer_on, on_duration):
self.canbus.set_output(module, output, 1)
self.apscheduler.get_job(timer_on).reschedule("interval", weeks = on_duration[0], days = on_duration[1], hours = on_duration[2], minutes = on_duration[3], seconds = on_duration[4])
self.apscheduler.get_job(timer_on).resume()
self.apscheduler.get_job(timer_off).pause()
logger.info("Turning " + timer_on + "!")
def interval_on(self, module, output, timer_off, timer_on, off_duration):
self.canbus.set_output(module, output, 0)
self.apscheduler.get_job(timer_off).reschedule("interval", weeks = off_duration[0], days = off_duration[1], hours = off_duration[2], minutes = off_duration[3], seconds = off_duration[4])
self.apscheduler.get_job(timer_off).resume()
self.apscheduler.get_job(timer_on).pause()
logger.info("Turning " + timer_off + "!")
def process_PID(self, pid, module_out, signal_out, module_in, signal_in):
msg = self.canbus.msgdb.lookup_message(module_in)
input_reading = msg.lookup_signal(signal_in).value
input_reading = 19
pid_out = pid.Update(input_reading)
if pid_out > 0:
self.canbus.set_output(module_out, signal_out, 1)
run_time = datetime.datetime.now() + datetime.timedelta(milliseconds=pid_out)
self.apscheduler.add_job(self.canbus.set_output, "date", run_date=run_time, args=[module_out, signal_out, 0])
def func():
print(1)
def main(argv):
if len(argv) < 1:
print("Error: please specify a can interface")
return 1
database = Database()
module_network = network.Network(logger)
module_network.add_interface(network_interface.CanBusNetworkInterface(module_network, argv[0], logger))
module_network.start_all_interfaces()
message = HydroBotMessage(0x0001, 0x81, 0x0007, 0x0001, 0x0111)
module_network.send_message(message)
#canbus = CanBus(database, argv[0])
#canbus.start()
#scheduler = Scheduler(canbus)
#scheduler.start()
while True:
time.sleep(0.001)
def close_program(signum, frame):
logger.info("Closing now!")
sys.exit(0)
signal.signal(signal.SIGINT, close_program)
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))
|