Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4516

MicroPython • IR sensor obstacle avoidance robot.

$
0
0
Hello,

Below is the code I've been working on for awhile. But the robot won't even run. I have it hooked up to a Raspberry Pi Pico along with a L293D 4 channel motor controller. I was wondering if there was something wrong with the code. Or how I have it set up? The skeleton of it i got off Google.

Note: the sensor is the blue flying fish sensor, setup by blueprint attached, just without led. The drive controller in the other picture was set up in that manner just without the fly time sensor. The sensor lights turn on and register obstacles.

Reference setup blue prints:
https://www.coderdojotc.org/micropython ... -base-bot/
https://diyprojectslab.com/ir-sensor-wi ... icropytho/

Code:

# Define the IR sensor pin ir_sensor_pin = 16 # Replace with your actual pin numberfrom machine import Pin, PWMfrom utime import sleep power_level= 35000# Define pinsir_sensor_pin = Pin(16, Pin.IN)  # Replace with your IR sensor pinmotor_left_forward = Pin(18, Pin.OUT)  # Replace with left motor forward pinmotor_left_backward = Pin(19, Pin.OUT) # Replace with left motor backward pinmotor_right_forward = Pin(20, Pin.OUT) # Replace with right motor forward pinmotor_right_backward = Pin(21, Pin.OUT) # Replace with right motor backward pinmotor_left_forward = PWM(Pin(motor_left_forward))motor_left_backward = PWM(Pin(motor_left_backward))motor_right_forward = PWM(Pin(motor_right_forward))motor_right_backwards = PWM(Pin(motor_right_backward))def turn_motor_on(pwm):    pwm.duty_u16(power_level)    def turn_motor_off(pwm):    pwn.duty_u16(0)def stop_motors():    motor_left_forward.value(0)    motor_left_backward.value(0)    motor_right_forward.value(0)    motor_right_backward.value(0)def move_forward():    motor_left_forward.value(1)    motor_left_backward.value(0)    motor_right_forward.value(1)    motor_right_backward.value(0)def turn_right():    motor_left_forward.value(1)    motor_left_backward.value(0)    motor_right_forward.value(0)    motor_right_backward.value(1)def turn_left():    motor_left_forward.value(0)    motor_left_backward.value(1)    motor_right_forward.value(1)    motor_right_backward.value(0)while True:    if ir_sensor_pin.value() == 0:  # Obstacle detected        stop_motors()        # Optional: Implement turning maneuver based on your robot design        # turn_right()          # time.sleep(0.5)      else:  # No obstacle        move_forward() time.sleep(0.1)  # Adjust delay as neededtime.sleep(0.1)  # Add a small delay for stability

Statistics: Posted by Jweld1117 — Thu Oct 17, 2024 7:28 pm — Replies 5 — Views 108



Viewing all articles
Browse latest Browse all 4516

Trending Articles