Micro Project for Python for Robotics

Hi, I am currently stucked at the Micro Project for Python for Robotics. The following is the code I have done so far. Please help me to spot the mistakes or provide any suggestions. Thanks.

from robot_control_class import RobotControl

rc = RobotControl()


decoy = 1

for i in range(20):
    counter = 0
    max_value = 0
    max_direction = 0
    l = rc.get_laser_full()
    for value in l:
        if value > max_value:
            max_value = value
            max_direction = counter
        counter = counter + 1
    correction = float((360 - max_direction)/50)

if correction < 0 :
    direction = "counterclockwise"
elif correction > 0:
    direction = "clockwise"

if abs(correction) < 3:
    rot_speed = 0

elif abs(correction) > 3:
    rot_speed = correction



if 1< rc.get_laser(360) < 2:
    move_speed = 0.4
elif rc.get_laser(360) > 3:
    move_speed = 0.6
elif rc.get_laser(360) < 1:
    rc.stop_robot()

    rc.turn(direction, rot_speed , correction )
    rc.move_straight_time("forward",move_speed,0.1)

Hi @leejy911,
First of all welcome to the community :slight_smile: !
When posting questions, please try to give more information.

  1. what are you trying to do
  2. what you have done (this you did by posting your code, although the textbox in this forum allows for code formatting, which can help a lot in readability). Also when posting python code, please include indentation, as it directly affects how the code is executed
  3. what is the error or problem (post error messages if you get any)

A few things in your code that could be done more easily:

Instead of:

 counter = 0
 max_value = 0
 max_direction = 0
 l = rc.get_laser_full()
 for value in l:
    if value > max_value:
        max_value = value
        max_direction = counter
        counter = counter + 1

you can do this:

 l = rc.get_laser_full()
 max_value = max(l)
 max_direction = l.Index(max_value)
1 Like