I'm taking an AI Class and I managed to get my algorithm to work it's not complex since the robot is surrounded and placed by the wall. However, in my opinion, the code doesn't look so clean and I would like to have multiple functions in my
Wall_Following() to make it look nicer. I've never programmed in Python before so I don't have much experience. I would like my function to look something like this:
def Wall_Follow(self) forward() if( front sensor > 150) # turns left if the front hits a wall Turn_left() elif(right sensor < 150) # turns right if too far away from wall Turn_Right() return left, right
This is my original code. Any feedback would be most appreciated on whether I'm just making this too complicated for myself.
def Wall_Follow(self): # default left/right values: left = self.MAX_SPEED * 0.8 right = self.MAX_SPEED * 0.8 if(self.ps_val > 150 or self.ps_val > 150): left = -self.MAX_SPEED/2 #turn left elif(self.ps_val < 150): right = -self.MAX_SPEED/2 #turn right return left,right def run(self): # Main loop while True: # Perform a simulation step of 64 milliseconds # and leave the loop when the simulation is over if self.step(self.TIME_STEP) == -1: break # functions to read sensor data self.read_sensors() # Process sensor data here. left, right = self.Wall_Follow() # Enter here functions to send actuator commands self.setSpeed(left, right) controller = PyController() controller.run()