Conversation
cbrrrry
left a comment
There was a problem hiding this comment.
I like what you've done here!
A few general comments -
- Please add more descriptive comments to each of your functions and modules.
- Please also remove the unnecessary components from each object (for the pH component, please remove the leak sensor stuff, etc).
- Can this code run when there are separately running sensor modules? For example, if the piponic.py loop is reading the pH, is there no problem running both? For both threading and duplicate sensor instantiation reasons.
Before we merge, it's probably a good idea to rebase this branch to main. We can meet to talk about what this looks like once you've addressed my comments. Thanks :)
src/control.py
Outdated
| self.init_leak() | ||
| self.pH_sensor = 0 | ||
| self.init_pH() | ||
| self.x = 4.7 |
There was a problem hiding this comment.
What does the variable self.x represent?
There was a problem hiding this comment.
It should be a debugging variable, I will remove this.
src/control.py
Outdated
| def __init__(self): | ||
| self.ads=0 | ||
| self.init_i2c() | ||
| self.leak_sensor = 0 |
There was a problem hiding this comment.
Does the ph_control() object need to have the leak sensor defined? Can this be removed?
There was a problem hiding this comment.
Yes, I believe this can be removed. I will remove it for now and comment on the code what's removed, since the threading sometimes getting wired in init functions.
src/control.py
Outdated
|
|
||
|
|
||
| def calibrate(self): | ||
| x = read |
There was a problem hiding this comment.
This is a placeholder function right?
src/control.py
Outdated
| self.pH_sensor= AnalogIn(self.ads,ADS.P2) | ||
| pH_voltage = self.pH_sensor.voltage | ||
| pH = 4.7 +(pH_voltage-1.65)*(-3.3) #test self.x here | ||
| if (pH<=8.2): #test self.desired_ph here |
There was a problem hiding this comment.
Shouldn't this be self.desired_pH?
There was a problem hiding this comment.
Yes! This value was used for Lynes testing, I will fix it.
src/control.py
Outdated
| time.sleep(2) | ||
| print('loop start') | ||
| self.pH_sensor= AnalogIn(self.ads,ADS.P2) | ||
| pH_voltage = self.pH_sensor.voltage |
There was a problem hiding this comment.
Getting the pH could be done in a cleaner way by calling self.read_pH()
There was a problem hiding this comment.
I 've edited it! correct me if I'm wrong
| # sensors.read_pH() | ||
| # Reference provided at: https://learn.adafruit.com/adafruit-4-channel-adc-breakouts/python-circuitpython | ||
|
|
||
| #----------------------------------------------Template--------------------------------------------------# |
There was a problem hiding this comment.
I like this Template description! Would you please be able to add all of the example code together at the end of it?
| cycle = cycle + 0.5 | ||
| print ('5 Second Thread cycle+1.0 - ') | ||
|
|
||
| #Create Class |
There was a problem hiding this comment.
Cool! I like this threading approach! Is there a way to modularize the delay? For example, so that the user can control how often the system is polling the sensors? How quickly do you guys think the pH control system needs to update/operate for good response?
There was a problem hiding this comment.
I believe our client mentioned this in meeting, and the pH controlling system should operate every 24hrs. The delays here are used for testing right now
| pH = 7.7 +(pH_voltage-1.65)*(-3.3) | ||
| return pH | ||
|
|
||
| def test_wl(self): |
There was a problem hiding this comment.
So test_wl(self) is the control loop for the water level?
There was a problem hiding this comment.
Great! If you can just add a brief description, so that future users know what this module's function is.
| #Start Thread | ||
| FiveSecondThread.start() | ||
|
|
||
| #Create Class |
There was a problem hiding this comment.
So will we be able to use the threading approach in the main loop of Piponic.py without blocking things up?
There was a problem hiding this comment.
Yes, but not in test.py. The one that's working should be in control.py in control branch.
| def init_leak(self): | ||
| self.leak_sensor= AnalogIn(self.ads,ADS.P0) | ||
|
|
||
| def water_level_init(self): |
There was a problem hiding this comment.
The water level sensor does not use the ADC. Please review the recently merged water_level.py on the main branch for code.
src/control.py
Outdated
| self.pH_sensor= AnalogIn(self.ads,ADS.P2) | ||
| pH_voltage = self.pH_sensor.voltage | ||
| pH = 4.7 +(pH_voltage-1.65)*(-3.3) #test self.x here | ||
| if (pH<=8.2): #test self.desired_ph here |
There was a problem hiding this comment.
Have we considered what a more complex control system could look like here? Maybe at least a PI system?
There was a problem hiding this comment.
I'm not sure what do you mean by the Pi system. However, I do play to optimize this equation here. Also, I think the users cannot avoid adjusting the pH system themselves.
There was a problem hiding this comment.
What I meant by "PI" system is a "proportional-integral" control system. This is basically a controller that turns the pump on more if the current pH is far away from the desired pH, and less if it is close to the desired pH.
There was a problem hiding this comment.
I think what you suggest are possible, I will try to work on this!
Version after reviewing Carson's comments
| # Notice that the FUNCTION_NAME should be the function in the class (defined before), and is the one that you would like it to iterate | ||
| # 4.3. Start the thread, for example: NAME_OF_THREAD.start() | ||
| #-----------------------------------------------CODE COMPLETE---------------------------------------------# | ||
| # class ph_control(object): |
There was a problem hiding this comment.
Isn't ph_control() already defined as a class? Correct me if I'm wrong, but don't we need to instantiate an object of this class similar to you do below. I'm guessing an example might look like this:
import src.control as control pH_controller = control.ph_control() pH_thread = Thread(target = pH_controller.test()) pH_thread.start()
I like that you've made your example more general than mine, I just wanted to try and show the most important use case.
Do threads only work for single functions?
There was a problem hiding this comment.
Here the aim of define ph_control(object) should just be an example for the users indicate that they should create a class. I will edit and try to make my sample code more obvious.
For your last question, I think yes, I test before and the threads can only work for one single function within one class
No description provided.