chetuk
Posts: 41
Joined: Mon Feb 20, 2017 7:27 am

line follow with obstacle

Mon Feb 12, 2018 3:40 am

Hi ,

Can some one help to club code for line follow with obstacle please.
Scenario:
Should stop on obstacle also follow line.
below is my code but seems not working

Code: Select all

#!/usr/bin/python
import RPi.GPIO as GPIO                    #Import GPIO library
import time
#Import time library
GPIO.setwarnings(False)
#set GPIO numbering mode and define output pins
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.OUT)
GPIO.setup(11,GPIO.OUT)
GPIO.setup(13,GPIO.OUT)
GPIO.setup(15,GPIO.OUT)
GPIO.setup(12,GPIO.IN)  #IR
GPIO.setup(16,GPIO.IN)  #IR
GPIO.setup(37,GPIO.OUT)  # Trigger
GPIO.setup(24,GPIO.IN)      # Echo
time.sleep(1)
print "Ultrasonic Measurement"
GPIO.output(37, False)

def measure():
  time.sleep(0.333)
  GPIO.output(37, True)
  time.sleep(0.00001)
  GPIO.output(37, False)
  start = time.time()

  while GPIO.input(24)==0:
    start = time.time()

  while GPIO.input(24)==1:
    stop = time.time()

  elapsed = stop-start
  distance = (elapsed * 34300)/2
  return distance
def stop():
                GPIO.output(7,False)
                GPIO.output(11,False)
                GPIO.output(13,False)
                GPIO.output(15,False)
def forward():
                GPIO.output(7,True)
                GPIO.output(11,False)
                GPIO.output(13,True)
                GPIO.output(15,False)
def back():
                GPIO.output(7,False)
                GPIO.output(11,True)
                GPIO.output(13,False)
                GPIO.output(15,True)
def left():
                GPIO.output(7,True)
                GPIO.output(11,False)
                GPIO.output(13,False)
                GPIO.output(15,True)
def right():
                GPIO.output(7,False)
                GPIO.output(11,True)
                GPIO.output(13,True)
                GPIO.output(15,False)
try:

  while True:

    distance = measure()
    print "Distance : %.1f" % distance
    time.sleep(0.0)
    if distance >= 130 :
            forward()
    elif GPIO.input(12):
            right()
    elif GPIO.input(16):
            left()
    elif GPIO.input(12) and GPIO.input(16)
            stop
    else:
            stop
except KeyboardInterrupt:
    #cleanup the GPIO pins before ending
    GPIO.cleanup()


scotty101
Posts: 2856
Joined: Fri Jun 08, 2012 6:03 pm

Re: line follow with obstacle

Mon Feb 12, 2018 9:29 am

Might be that you aren't actually calling the stop function and are missing a colon (:)

Your Code

Code: Select all

    elif GPIO.input(12) and GPIO.input(16)
        stop
    else:
        stop
Should be

Code: Select all

    elif GPIO.input(12) and GPIO.input(16):
        stop()
    else:
        stop()
Also have you considered assigning your GPIO pin number to variables? Would make your code much more readable. For example

Code: Select all

IR_LEFT = 12
IR_RIGHT = 16
TRIGGER = 37
ECHO = 24
GPIO.setup(IR_LEFT, GPIO.IN)  #IR
GPIO.setup(IR_RIGHT, GPIO.IN)  #IR
GPIO.setup(TRIGGER, GPIO.OUT)  # Trigger
GPIO.setup(ECHO, GPIO.IN)      # Echo
........
elif GPIO.input(IR_LEFT):
            right()
........            
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

chetuk
Posts: 41
Joined: Mon Feb 20, 2017 7:27 am

Re: line follow with obstacle

Mon Feb 12, 2018 11:07 am

Hi Scotty ,

Mistake in paste (:) inserted but not working.
I will try the other part variable one.

Thanks!

/Chetan S.

scotty101
Posts: 2856
Joined: Fri Jun 08, 2012 6:03 pm

Re: line follow with obstacle

Mon Feb 12, 2018 11:53 am

chetuk wrote:
Mon Feb 12, 2018 11:07 am
Mistake in paste (:) inserted but not working.
Did you add the missing () after stop?
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

User avatar
OutoftheBOTS
Posts: 602
Joined: Tue Aug 01, 2017 10:06 am

Re: line follow with obstacle

Mon Feb 12, 2018 9:30 pm

First as above you will need the () after calling stop

second llok at the order in which your testing for conditions as the program will test in that order and only take the first option that is True. At the moment your testing in this order : distance, left, right, left and right together. It will never get to left and right together because if left and right is true it will already take the earlier option of left or right. Also this will be the case with testing for distance, if distance is true then it will just move forward and never test lower options.

You will need to change order to this :

Code: Select all

    if distance >= 130 and GPIO.input(12) and GPIO.input(16):
	        stop()
    if distance >= 130 and GPIO.input(12):
	        right()
    if distance >= 130 and GPIO.input(16):
	        right()
    if distance >= 130:			
            forward()
    else:
            stop()

chetuk
Posts: 41
Joined: Mon Feb 20, 2017 7:27 am

Re: line follow with obstacle

Tue Feb 13, 2018 7:19 am

https://www.youtube.com/watch?v=vmkcG9g7X-E

this is what i want to achieve suggestion with code please as when i run separate it works line and obstacle.
but no clue to merge together. :?:

/Chetan S.

chetuk
Posts: 41
Joined: Mon Feb 20, 2017 7:27 am

Re: line follow with obstacle

Mon Feb 19, 2018 6:47 am

Below is my code but not working .. any advice?

Code: Select all

#!/usr/bin/python
import RPi.GPIO as GPIO                    #Import GPIO library
import time
#Import time library
GPIO.setwarnings(False)
#set GPIO numbering mode and define output pins
GPIO.setmode(GPIO.BOARD)
SPD=input("speed?? enter 20/40/60/100")
IR_1=12
IR_2=16
UTL_1=37
UTL_2=24
M1=7
M2=11
M3=13
M4=15
GPIO.setup(7,GPIO.OUT)
p1=GPIO.PWM(7,SPD)
p1.start(0)
GPIO.setup(11,GPIO.OUT)
p2=GPIO.PWM(11,SPD)
p2.start(0)
GPIO.setup(13,GPIO.OUT)
p3=GPIO.PWM(13,SPD)
p3.start(0)
GPIO.setup(15,GPIO.OUT)
p4=GPIO.PWM(15,SPD)
p4.start(0)
#IR Sensor
GPIO.setup(12,GPIO.IN)  #IR
GPIO.setup(16,GPIO.IN)  #IR
GPIO.setup(37,GPIO.OUT)  # Trigger
GPIO.setup(24,GPIO.IN)      # Echo
time.sleep(0.0)
print "Ultrasonic Measurement"
GPIO.output(37, False)

def measure():
  time.sleep(0.333)
  GPIO.output(37, True)
  time.sleep(0.00001)
  GPIO.output(37, False)
  start = time.time()

  while GPIO.input(24)==0:
    start = time.time()

  while GPIO.input(24)==1:
    stop = time.time()

  elapsed = stop-start
  distance = (elapsed * 34300)/2
  return distance

def stop():
                p1.start(0)
                p2.start(0)
                p3.start(0)
                p4.start(0)
def forward():
                p1.start(SPD)
                p2.start(0)
                p3.start(SPD)
                p4.start(0)
def back():
                p1.start(0)
                p2.start(SPD)
                p3.start(0)
                p4.start(SPD)
def left():
                p1.start(SPD)
                p2.start(0)
                p3.start(0)
                p4.start(SPD)
def right():
                p1.start(0)
                p2.start(SPD)
                p3.start(SPD)
                p4.start(0)
try:

  while True:

    distance = measure()
    print "Distance : %.1f" % distance
    time.sleep(0.0)
    if (GPIO.input(12) and GPIO.input(16)) or (distance <= 20):
            stop()
    elif GPIO.input(12):
            right()
    elif GPIO.input(16):
            left()
    else:
            stop()
except KeyboardInterrupt:
    #cleanup the GPIO pins before ending
    GPIO.cleanup()


pcmanbob
Posts: 3878
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: line follow with obstacle

Mon Feb 19, 2018 8:03 am

Try telling us which part of it is not working and why you think it's not working and include any error messages.
Please only ask questions in the forum I will not answer questions sent to me directly via PM
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

scotty101
Posts: 2856
Joined: Fri Jun 08, 2012 6:03 pm

Re: line follow with obstacle

Mon Feb 19, 2018 9:40 am

I can't run your code since I don't have a pi and all the required parts but this looks incorrect

Code: Select all

SPD=input("speed?? enter 20/40/60/100")

p1=GPIO.PWM(7,SPD)
Using the input command you read in a string containing 20,40,60 or 100. You then pass that string to the GPIO.PWM method. This will probably fail since GPIO.PWM expects a number not a string.
Also you don't do anything to validate that the user has entered only 20/40/60/100 or anything else like 'fish' or 'naughtycode'.

Below code will check that the user has entered a valid speed number and then convert it to an integer that GPIO.PWM can read.

Code: Select all

SPD_String = input("speed?? enter 20/40/60/100")
if SPD_String in ('20','40','60','100'):
    SPD = int(SPD_String)
else:
    SPD = 20
    print('Invalid speed entered. Defaulting to 20')
Another thing. I'm guessing you are using some kind of motor driver to control two motors and that each motor has an 'enable' and 'direction' pin. If you want to PWM the speed of the motor you only need to apply PWM to the enable pin not the direction pin. Your code suggests you are PWMing all 4 pins. However given that i can't see your robot to confirm what type of motor driver you are using this may not be accurate.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

chetuk
Posts: 41
Joined: Mon Feb 20, 2017 7:27 am

Re: line follow with obstacle

Mon Feb 19, 2018 12:52 pm

I am using l298n motor controller with two normal dc motor with PI 3
As mention yes i am doing for all pin is this wrong??
but when i run it works fine it runs slow.

chetuk
Posts: 41
Joined: Mon Feb 20, 2017 7:27 am

Re: line follow with obstacle

Mon Feb 19, 2018 1:06 pm

Mean i need to connect ENA & ENB to PI pin ??? rite now only connected to 4 pins on l298n.
Also will this two pins to be set as GPIO.OUT or GPIO.IN???
Attached is the current way using.



/Chetan S.
Attachments
$3FA2B72B01765C47.jpg
$3FA2B72B01765C47.jpg (40.57 KiB) Viewed 1137 times
Last edited by chetuk on Mon Feb 19, 2018 1:19 pm, edited 1 time in total.

scotty101
Posts: 2856
Joined: Fri Jun 08, 2012 6:03 pm

Re: line follow with obstacle

Mon Feb 19, 2018 1:13 pm

That seems to be the common way to control the L298N (I've never used it before but have used similar)

See http://howtomechatronics.com/tutorials/ ... -h-bridge/. The example Arduino code shows the PWM being applied to the ENA pin rather than the IN pins.

https://www.bluetin.io/python/gpio-pwm- ... r-control/ shows the same.
They should be set as outputs.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

chetuk
Posts: 41
Joined: Mon Feb 20, 2017 7:27 am

Re: line follow with obstacle

Mon Feb 19, 2018 2:22 pm

Does this make any sense XX & YY are pin yet to try which will connect ENA & ENB .

Code: Select all

#!/usr/bin/python
import RPi.GPIO as GPIO                    #Import GPIO library
from espeak import espeak
import time
#Import time library
GPIO.setwarnings(False)
#set GPIO numbering mode and define output pins
GPIO.setmode(GPIO.BOARD)
GPIO.setup(XX,GPIO.OUT) #ENA
GPIO.setup(YY,GPIO.OUT) #ENB
GPIO.setup(7,GPIO.OUT)
GPIO.setup(11,GPIO.OUT)
GPIO.setup(13,GPIO.OUT)
GPIO.setup(15,GPIO.OUT)
GPIO.setup(37,GPIO.OUT)  # Trigger
GPIO.setup(24,GPIO.IN)      # Echo
#Defining PWM
motor1GPIO = GPIO.PWM(XX, 100)
motor2GPIO = GPIO.PWM(YY, 100)


time.sleep(1)
print "Ultrasonic Measurement"
GPIO.output(37, False)

def measure():
  time.sleep(0.333)
  GPIO.output(37, True)
  time.sleep(0.00001)
  GPIO.output(37, False)
  start = time.time()

  while GPIO.input(24)==0:
    start = time.time()

  while GPIO.input(24)==1:
    stop = time.time()

  elapsed = stop-start
  distance = (elapsed * 34300)/2
  return distance
def stop():
                GPIO.output(7,False)
                GPIO.output(11,False)
                GPIO.output(13,False)
                GPIO.output(15,False)
def forward():
		motor1GPIO.start(50)
                GPIO.output(7,True)
                GPIO.output(11,False)
		motor2GPIO.start (50)
                GPIO.output(13,True)
                GPIO.output(15,False)
def back():
		motor1GPIO.start(50)
                GPIO.output(7,False)
                GPIO.output(11,True)
		motor2GPIO.start (50)
                GPIO.output(13,False)
                GPIO.output(15,True)
def left():
                GPIO.output(7,True)
                GPIO.output(11,False)
                GPIO.output(13,False)
                GPIO.output(15,True)
def right():
                GPIO.output(7,False)
                GPIO.output(11,True)
                GPIO.output(13,True)
                GPIO.output(15,False)
try:

  while True:

    distance = measure()
    print "Distance : %.1f" % distance
    time.sleep(0.0)
    if distance >= 130:
            forward()
    else:
            stop()
except KeyboardInterrupt:
    #cleanup the GPIO pins before ending
    GPIO.cleanup()

pcmanbob
Posts: 3878
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: line follow with obstacle

Mon Feb 19, 2018 2:36 pm

if you are going to use 50% speed all the time then you can just call the

Code: Select all

motor1GPIO.start(50)
motor2GPIO.start (50)
right at the start of your program you don't need to call it each time you change direction.

you only need to call these again if you want to change the speed.

Having to threads talking about the same thing only leads to confusion.
As I said in this thread viewtopic.php?f=37&t=203172&p=1272183#p1272183

you apply the PWM signal to the ENA and ENB pins on your motor board after removing the existing jumpers.

As you can see in this typical motor driver board a enable and b enable have jumpers fitter you have to remove them and connect the gpio outputs to the front pins in line with the other control pins.

Image
Please only ask questions in the forum I will not answer questions sent to me directly via PM
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

chetuk
Posts: 41
Joined: Mon Feb 20, 2017 7:27 am

Re: line follow with obstacle

Mon Feb 19, 2018 4:41 pm

Really appreciate for correcting me .. Now speed is fine but the ROBO not sensing line as well no error.
It should follow line and whenever it detects obstacle shud stop.
12 & 16 are IR sensors.

Code: Select all

try: 
 
  while True: 
 
    distance = measure() 
    print "Distance : %.1f" % distance 
    time.sleep(0.0) 
    if distance >= 130: 
            forward() 
    elif GPIO.input(12) and GPIO.input(16):
            stop()
    elif GPIO.input(12):
            right()
    elif GPIO.input(16):
            left()
    else: 
            stop() 

pcmanbob
Posts: 3878
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: line follow with obstacle

Mon Feb 19, 2018 5:03 pm

when trying to solve problems like this it a good idea to print the actual readings from inputs so you know what input vlaue is being fed in to your program.

example.

Code: Select all

try: 
 
  while True: 
 
    distance = measure() 
    print "Distance : %.1f" % distance 
    print "gpio12 ", GPIO.input(12)
    print "gpio16 ", GPIO.input(16)
    time.sleep(0.0) 
    if distance >= 130: 
            forward() 
    elif GPIO.input(12) and GPIO.input(16):
            stop()
    elif GPIO.input(12):
            right()
    elif GPIO.input(16):
            left()
    else: 
            stop() 
            
 

secondly if your first if statement is true all elif and else statement are ignored.

the same is true if the first if statement is false but the first elif is true then the 2nd & 3rd & else statements are ignored and so on.

https://www.tutorialspoint.com/python/p ... f_else.htm

So try it like this

Code: Select all

try: 
 
  while True: 
 
    distance = measure() 
    print "Distance : %.1f" % distance 
    print "gpio12 ", GPIO.input(12)
    print "gpio16 ", GPIO.input(16)
    time.sleep(0.0) 
    
    if GPIO.input(12) and GPIO.input(16):
            stop()    
    
    if GPIO.input(12):
            right()   
    
    if GPIO.input(16):
            left()    
    
    if distance >= 130: 
            forward() 
    else: 
            stop() 
            
you may have to change the order to get it moving smoothly.
Please only ask questions in the forum I will not answer questions sent to me directly via PM
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

chetuk
Posts: 41
Joined: Mon Feb 20, 2017 7:27 am

Re: line follow with obstacle

Tue Feb 20, 2018 6:51 am

Hi Pcmanbob,

I tried your suggestion but same the line follower works normal as well the obstacle when run separately.
But when merged together it doesn't no error robot goes any direction.

Below is a track and link where uploaded video .


https://youtu.be/FskRniUgYxs
Attachments
$7D0AFE04169F9E7F.jpg
$7D0AFE04169F9E7F.jpg (17.9 KiB) Viewed 1063 times

pcmanbob
Posts: 3878
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: line follow with obstacle

Tue Feb 20, 2018 1:18 pm

Then you have to watch what your inputs are doing which is why I said add lots of print statements so you can watch them.

then change the log in your program to make it do what you want.

first thing to try would be to stop if range is below 130 rather that go if range greater than 130

Code: Select all

try: 
 
  while True: 
 
    distance = measure() 
    print "Distance : %.1f" % distance 
    print "gpio12 ", GPIO.input(12)
    print "gpio16 ", GPIO.input(16)
    time.sleep(0.0) 
    
    if GPIO.input(12) and GPIO.input(16):
            stop()    
    
    if GPIO.input(12):
            right()   
    
    if GPIO.input(16):
            left()    
    
    if distance < 130: 
            stop() 
    else: 
            forward() 
            
or using you original code

Code: Select all

try: 
 
  while True: 
 
    distance = measure() 
    print "Distance : %.1f" % distance 
    print "gpio12 ", GPIO.input(12)
    print "gpio16 ", GPIO.input(16)    
    time.sleep(0.0) 
    if distance <130: 
            stop() 
    elif GPIO.input(12) and GPIO.input(16):
            stop()
    elif GPIO.input(12):
            right()
    elif GPIO.input(16):
            left()
    else: 
            forward() 
Please only ask questions in the forum I will not answer questions sent to me directly via PM
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

chetuk
Posts: 41
Joined: Mon Feb 20, 2017 7:27 am

Re: line follow with obstacle

Thu Feb 22, 2018 8:03 am

As suggested print was included but working of robot is abnormal not as expected.
may be i need to put some delay etc.

:cry:

/Chetan S.

pcmanbob
Posts: 3878
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: line follow with obstacle

Thu Feb 22, 2018 9:17 am

Cant really help with testing as I don't have a robot like yours, try taking out the line for the test of object in range and then see how things change , then add longer or shorted times for the motors to run, you may even need to check your line sensors while the motors are running.
You have to experiment to get your robot to work as you want.

Experimentation part of building a project like this they very rarely work correctly first time if ever.
Please only ask questions in the forum I will not answer questions sent to me directly via PM
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

PiGraham
Posts: 3219
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: line follow with obstacle

Thu Feb 22, 2018 9:25 am

chetuk wrote:
Tue Feb 20, 2018 6:51 am
Hi Pcmanbob,

I tried your suggestion but same the line follower works normal as well the obstacle when run separately.
But when merged together it doesn't no error robot goes any direction.

Below is a track and link where uploaded video .


https://youtu.be/FskRniUgYxs
What's wrong? It follows the line and there are no obstacles. what is the problem?

chetuk
Posts: 41
Joined: Mon Feb 20, 2017 7:27 am

Re: line follow with obstacle

Thu Feb 22, 2018 9:55 am

It is not following track and some time when obstacle detects it turns .

/Chetan S.

pcmanbob
Posts: 3878
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: line follow with obstacle

Thu Feb 22, 2018 12:25 pm

Did you try it using your original if elif code but adding the distance measurement as the first if.

Code: Select all

try: 
 
  while True: 
 
    distance = measure() 
    print "Distance : %.1f" % distance 
    print "gpio12 ", GPIO.input(12)
    print "gpio16 ", GPIO.input(16)    
    time.sleep(0.0) 
    if distance <130: 
            stop() 
    elif GPIO.input(12) and GPIO.input(16):
            stop()
    elif GPIO.input(12):
            right()
    elif GPIO.input(16):
            left()
    else: 
            forward() 
            
Please only ask questions in the forum I will not answer questions sent to me directly via PM
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 10583
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: line follow with obstacle

Sat Feb 24, 2018 1:27 pm

locked in favour of original posters duplicate thread.

need everyones permission to delete this thread, so cannot do so.
Be careful not to create duplicate threads, as this is a good example of the kind of mess you can get into.!

Return to “Automation, sensing and robotics”

Who is online

Users browsing this forum: No registered users and 10 guests