Changeset - a5b830d92afd
[Not reviewed]
default
1 1 1
matthewreed - 9 years ago 2016-07-17 20:01:28

Added some scheduling and other updates
2 files changed with 57 insertions and 13 deletions:
0 comments (0 inline, 0 general)
hydrobot.py
Show inline comments
 
import sys
 
import time
 
import _thread
 
from canard import can, bus
 
from canard import can, messaging
 
from canard.hw import socketcan
 
from canard.file import jsondb
 
from influxdb import InfluxDBClient
 
import configparser
 
from apscheduler.schedulers.background import BackgroundScheduler
 
from datetime import datetime
 
import os
 

	
 
#TODO
 
#fix temperature offset
 
#database serial thing
 
#database time interval logging
 
#add system name to database
 

	
 
config = configparser.ConfigParser()
 
config.read("config.txt")
 
config.read("hydrobot.conf")
 
DEBUG_CAN = config.getboolean("debug", "can")
 
DEBUG_CAN_DETAIL = config.getboolean("debug", "can_detail")
 

	
 
class Database:
 
    
 
    def __init__(self):
 
@@ -98,42 +107,60 @@ class CanBus:
 

	
 
    def process_can(self):
 
        while True:
 
            frame = self.dev.recv()
 
            message = self.msgdb.decode(frame)
 
            if message:
 
                if DEBUG_CAN:
 
                    print("Received CAN message! ID: " + hex(message.id))
 
                for s in message._signals.values():
 
                    if DEBUG_CAN:
 
                        print("Received CAN message! ID: " + hex(message.id))
 
                    if DEBUG_CAN_DETAIL:
 
                        print(s)
 
                        print(s.value)
 
                    self.database.log_data(self.msgdb, message)
 
                
 
    def send_can(self):
 
        self.relay_send_msg.Nothing.value = 0
 
        self.relay_send_msg.Output1.value = 0
 
        self.relay_send_msg.Output2.value = 0
 
        self.relay_send_msg.Output3.value = 0
 
        self.relay_send_msg.Output4.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
 
        if DEBUG_CAN:
 
            print("Send CAN message! ID: " + hex(self.relay_send_msg.id))
 
        if DEBUG_CAN_DETAIL:
 
            print(self.relay_send_msg)
 
        self.dev.send(self.relay_send_msg.encode())
 
    
 
        
 
    def set_output(self, module, output, state):
 
        print(self.msgdb)
 
        print(module)
 
        #msg = self.msgdb.module
 
        print(msg)
 

	
 
def main():
 
    
 
    database = Database()
 
    canbus = CanBus(database)
 
    canbus.start()
 
    canbus.start_receive()
 
    
 
    scheduler = BackgroundScheduler()
 
    #scheduler.add_job(canbus.send_can, 'interval', seconds=10)
 
    trigger = config.get("timer1", "trigger")
 
    scheduler.add_job(canbus.send_can, trigger, second = 0)
 
    trigger = config.get("timer2", "trigger")
 
    scheduler.add_job(canbus.send_can, trigger, seconds = 10)
 
    scheduler.start()
 

	
 
    while True:
 
        module = config.get("timer1", "module")
 
        output = config.get("timer1", "output")
 
        #canbus.set_output(module, output, 1)
 
        
 
        canbus.send_can()
 
        time.sleep(1)
 
        
 

	
 
if __name__ == "__main__":
 
    main()
hydrobot_example.conf
Show inline comments
 
file renamed from config_example.txt to hydrobot_example.conf
 
[system]
 
name: my-system
 

	
 
[database]
 
host: www.protofusion.org
 
host: www.example.org
 
port: 8086
 
username: username
 
password: password
 
username: admin
 
password: admin
 
database: hydrobot
 

	
 
[debug]
 
can: True
 
can_detail: False
 

	
 
[timer1]
 
module: RelayDriveOut
 
output: Output1
 
trigger: cron
 
on_time: second = 0
 
off_time: second = 30
 

	
 
[timer2]
 
module: RelayDriveOut
 
output: Output2
 
trigger: interval
 
on_duration: seconds = 30
 
off_duration: seconds = 30s
0 comments (0 inline, 0 general)