I am trying to clean up my code but I am new to python so I'm pretty sure my syntax is wrong. I wrote a simple controller to have a variable message_lx_setPoint
approach the value of a set point message_lx_setPoint
. I am using robot operating system so the script is being looped by that. As it is, it works fine, the value of message_lx_current
approaches the value of the setPoint. Below is the code I made for that:
#defs
increment = 0.1
#setPoint values
message_lx_setPoint = 5.0 #value set by some controller(?)
message_ly_setPoint = 5.0
#current values
message_lx_current = 0.0 #value set by some sensor(?)
message_ly_current = 0.0
def approach_lx_setPoint():
global message_lx_current, message_lx_setPoint, increment
if message_lx_current < message_lx_setPoint:
print("current is less than set point")
message_lx_current += increment
elif message_lx_current == message_lx_setPoint:
print("current is equal to set point")
message_lx_current = 0.0
elif message_lx_current > message_lx_setPoint:
print("current is greater than set point")
message_lx_current -= increment
return message_lx_current
def approach_ly_setPoint():
global message_ly_current, message_ly_setPoint, increment
if message_ly_current < message_ly_setPoint:
print("current is less than set point")
message_ly_current += increment
elif message_ly_current == message_ly_setPoint:
print("current is equal to set point")
message_ly_current = 0.0
elif message_ly_current > message_ly_setPoint:
print("current is greater than set point")
message_ly_current -= increment
return message_ly_current
def main():
approach_lx_setPoint()
approach_ly_setPoint()
print(message_lx_current)
print(message_ly_current)
I have to do this for 6 different variables, so my code start to get bloated; I have to make 6 methods in total to control all 6 variables. I want to make it so that I only make one single method, I pass the variable to be controlled and the variable to be the set point and it executes from there. I am thinking something like this:
#same variables as before
def approachSetPoint(current, target):
global increment
if current < target:
print("current is less than set point")
current += increment
elif current == target:
print("current is equal to set point")
current = 0.0
elif current > target:
print("current is greater than set point")
current -= increment
return current
def main():
approachSetPoint(message_lx_current, message_lx_setPoint)
approachSetPoint(message_ly_current, message_ly_setPoint)
print(message_lx_current)
print(message_ly_current)
I am hoping that the result of this will be that rather than having one approachSetPoint
def for each variable to be controlled, I will only have one approachSetPoint def, and all I have to do is call it in main with the proper variables and it'll do the thing.
I tried this and I got no errors or anything, however, the values were NOT being updated(?). I'm assuming I'm just not properly linking the variable I'm passing as an argument to the one that is modified in the method. Any idea how to approach a behavior like I describes? Thanks in advance!
You need to store the results outside the function between runs:
message_lx_current = y_current = 0.0
message_lx_setpoint = message_ly_setpoint = 5.0
def approach_setpoint(current, setpoint, increment=0.1):
if current < setpoint:
print("current is less than set point")
current += increment
elif current == setpoint:
print("current is equal to set point")
current = 0.0
elif current > setpoint:
print("current is greater than set point")
current -= increment
return current
def main():
message_lx_current = approach_setpoint(message_lx_current, message_lx_setpoint)
message_ly_current = approach_setpoint(message_ly_current, message_ly_setpoint)
print(message_lx_current)
print(message_ly_current)