Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@sansal54
Forked from mh1412/omegaPID.py
Created January 24, 2019 20:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sansal54/3832f53b8a2a5e27d378b45218282cd5 to your computer and use it in GitHub Desktop.
Save sansal54/3832f53b8a2a5e27d378b45218282cd5 to your computer and use it in GitHub Desktop.
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