Created
July 17, 2018 16:02
-
-
Save mh1412/2594a57e25a4cfe7d89b775d2ecb8fde to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import PID | |
import time | |
import os.path | |
from OmegaExpansion import AdcExp | |
from OmegaExpansion import pwmExp | |
pwmExp.setVerbosity(-1) | |
pwmExp.driverInit() | |
adc = AdcExp.AdcExp() | |
targetT = 35 | |
P = 10 | |
I = 1 | |
D = 1 | |
pid = PID.PID(P, I, D) | |
pid.SetPoint = targetT | |
pid.setSampleTime(1) | |
def readConfig (): | |
global targetT | |
with open ('/tmp/pid.conf', 'r') as f: | |
config = f.readline().split(',') | |
pid.SetPoint = float(config[0]) | |
targetT = pid.SetPoint | |
pid.setKp (float(config[1])) | |
pid.setKi (float(config[2])) | |
pid.setKd (float(config[3])) | |
def createConfig (): | |
if not os.path.isfile('/tmp/pid.conf'): | |
with open ('/tmp/pid.conf', 'w') as f: | |
f.write('%s,%s,%s,%s'%(targetT,P,I,D)) | |
createConfig() | |
while 1: | |
readConfig() | |
#read temperature data | |
a0 = adc.read_voltage(0) | |
temperature = (a0 - 0.5) * 100 | |
pid.update(temperature) | |
targetPwm = pid.output | |
targetPwm = max(min( int(targetPwm), 100 ),0) | |
print "Target: %.1f C | Current: %.1f C | PWM: %s %%"%(targetT, temperature, targetPwm) | |
# Set PWM expansion channel 0 to the target setting | |
pwmExp.setupDriver(0, targetPwm, 0) | |
time.sleep(0.5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment