2
\$\begingroup\$

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[0] > 150 
 or self.ps_val[7] > 150):
  left = -self.MAX_SPEED/2   #turn left

 elif(self.ps_val[2] < 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()
\$\endgroup\$
  • \$\begingroup\$ Could you fix indentation, please? Also, what is PyController? Include all the imports. \$\endgroup\$ – Georgy Dec 27 '17 at 21:32
  • \$\begingroup\$ my mistake, i believe i fixed the indentation. The pyController is just the name of my class. The only import i have is " from controller import*" . The controller import just allows me to use things like LEDs Distance sensors etc for programming the robot i'm using(E-Puck robot). My problem is not so much a bug issue or anything like that. just wondering how i would go about making my code look similar to the pseudo code above. if that would make my code simpler. my class said you don't need python experience but i would still like to see if i can improve my code anyway possible \$\endgroup\$ – Christopher White Dec 27 '17 at 22:45
1
\$\begingroup\$

I'd suggest that you "translate" the comparisons into meaningful names:

if self.BlockedInFront():
    left = -self.MAX_SPEED/2

if self.BlockedRight():
    right = -self.MAX_SPEED/2
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.