Search code examples
pythonclasscyclepid-controller

How to perform different tasks from different classes in every cycle iteration?


I've some code which basically change speed of a motor controller and records the data, however I would like to add now another class to include a pid controller, I don't know (since I'm new with python programming), how to call this every iteration at the same time of recording data and set different values for the motor, in orther words, how to combine two parts of code: Could anyone please help me with this? Thanks


Solution

  • Instead of passing TESTER.onTimer to the PeriodicTimer constructor, pass a function that calls both TESTER.onTimer and PID.update(). You'll have to have an instance of PID just like TESTER is an instance of Tester.

    Also, I recommend doing if __name__ == "__main__": like this (note main function and lowercase local variables):

    def main():
        csv_file = open('DataRecorded.csv', 'wb')
        writer = csv.writer(csv_file)    
        writer.writerow(['Time', 'Current[mA]', 'Voltage[V]', 'Position', 
        'Period', 'CurrentAct[mA]', 'VoltageAct[V]' ])     
        tester = Tester(5) #Change number to 
        timer = PeriodicTimer(0.05, tester.onTimer)
        timer.start()
        raw_input('Monitoring Current and Voltage') # Use input() in Python 3
        IPCON.disconnect()
        csv_file.close()
    
    if __name__ == "__main__":
        main()
    

    You should probably move the Birckd connection section inside main() but that's way off topic.