Skip to content

Update control.py#7

Open
limbotempo wants to merge 65 commits intopH_v1from
control
Open

Update control.py#7
limbotempo wants to merge 65 commits intopH_v1from
control

Conversation

@limbotempo
Copy link
Collaborator

No description provided.

@limbotempo limbotempo requested a review from cbrrrry March 12, 2021 16:24
Copy link
Collaborator

@cbrrrry cbrrrry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like what you've done here!
A few general comments -

  1. Please add more descriptive comments to each of your functions and modules.
  2. Please also remove the unnecessary components from each object (for the pH component, please remove the leak sensor stuff, etc).
  3. 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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does the variable self.x represent?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the ph_control() object need to have the leak sensor defined? Can this be removed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a placeholder function right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be self.desired_pH?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Getting the pH could be done in a cleaner way by calling self.read_pH()

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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--------------------------------------------------#
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this Template description! Would you please be able to add all of the example code together at the end of it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure

cycle = cycle + 0.5
print ('5 Second Thread cycle+1.0 - ')

#Create Class
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So test_wl(self) is the control loop for the water level?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes exactly!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So will we be able to use the threading approach in the main loop of Piponic.py without blocking things up?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The water level sensor does not use the ADC. Please review the recently merged water_level.py on the main branch for code.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure!

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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have we considered what a more complex control system could look like here? Maybe at least a PI system?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants