HOME ABOUT ME BLOGS CONTACT YOUTUBE Login Register

Reinforcement Learning for path learning in Mobile Robot

Dec. 31, 2018, 9 a.m.

Reinforcement learning is an important type of Machine Learning where an agent learn how to behave in a environment by performing actions and seeing the results.

                        #Implementation Code

import RPi.GPIO as GPIO
import numpy as np
import time
import random
import math


#Initializing pins for motors and ir sensor

m1p1 = 21
m1p2 = 20
m2p1 = 16
m2p2 = 12

elt = 26
lt = 19
ctr = 13
rt = 6
ert = 5

pwm1 = 7
pwm2 = 8

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

GPIO.setup(m1p1, GPIO.OUT)
GPIO.setup(m1p2, GPIO.OUT)
GPIO.setup(m2p1, GPIO.OUT)
GPIO.setup(m2p2, GPIO.OUT)
GPIO.setup(pwm1, GPIO.OUT)
GPIO.setup(pwm2, GPIO.OUT)
GPIO.setup(elt, GPIO.IN)
GPIO.setup(lt, GPIO.IN)
GPIO.setup(ctr, GPIO.IN)
GPIO.setup(rt, GPIO.IN)
GPIO.setup(ert, GPIO.IN)

p1 = GPIO.PWM(pwm1, 20)
p2 = GPIO.PWM(pwm2, 20)
p1.start(15)
p2.start(15)

area_map = np.array([[1,1,1],[1,1,1],[1,1,1]])
reward = np.array([[-3,-3,-3],[-3,-3,-3],[-3,-3,-3],[500,-3,-3]])
q = np.zeros((3,3,4))
action = {0:"up", 1:"down", 2:"left", 3:"right"}
policy = np.chararray((3,3), itemsize = 10)

def move_front():
	GPIO.output(m1p1, False)
        GPIO.output(m1p2, True)
        GPIO.output(m2p1, False)
        GPIO.output(m2p2, True)

def turn_right():
        GPIO.output(m1p1, False)
        GPIO.output(m1p2, True)
        GPIO.output(m2p1, True)
        GPIO.output(m2p2, False)

def turn_left():
        GPIO.output(m1p1, True)
        GPIO.output(m1p2, False)
        GPIO.output(m2p1, False)
        GPIO.output(m2p2, True)

def stop():
        GPIO.output(m1p1, False)
        GPIO.output(m1p2, False)
        GPIO.output(m2p1, False)
        GPIO.output(m2p2, False)

def left_turn_90():
	stop()
	turn_left()
	time.sleep(0.78)
	stop()
def left_turn_180():
        stop()
        turn_left()
        time.sleep(1.5)
        stop()

def right_turn_90():
        stop()
        turn_right()
        time.sleep(0.78)
        stop()

def up(current_heading):
	stop()
	move_front()
	time.sleep(0.25)
	stop()
	theta = current_heading
	if(theta == 0):
		stop()
		theta = 0
	if(theta == 90):
		right_turn_90()
		theta = 0
	if(theta == 180):
		left_turn_180()
		theta = 0
	if(theta == 270):
		left_turn_90()
		theta = 0
	return theta

def left(current_heading):
	stop()
	move_front()
	time.sleep(0.25)
	stop()
        theta = current_heading
        if(theta == 0):
                left_turn_90()
                theta = 90
        if(theta == 90):
                stop()
                theta = 90
        if(theta == 180):
                right_turn_90()
                theta = 90
        if(theta == 270):
                left_turn_180()
                theta = 90
        return theta

def right(current_heading):
	stop()
	move_front()
	time.sleep(0.25)
	stop()
        theta = current_heading
        if(theta == 0):
                right_turn_90()
                theta = 270
        if(theta == 90):
                left_turn_180()
                theta = 270
        if(theta == 180):
                left_turn_180()
                theta = 270
        if(theta == 270):
                stop()
                theta = 270
        return theta
def down(current_heading):
	stop()
	move_front()
	time.sleep(0.25)
	stop()
        theta = current_heading
        if(theta == 0):
                left_turn_180()
                theta = 180
        if(theta == 90):
                left_turn_90()
                theta = 180
        if(theta == 180):
                stop()
                theta = 180
        if(theta == 270):
                right_turn_90()
                theta = 180
        return theta


def next_index(c_r, c_c, theta, a, area_map):
      	if (a==0):
            	angle = up(theta)
		theta = angle
            	r = c_r + 1
	    	if(r>2):
		   r= c_r
            	c =c_c
		while((GPIO.input(elt)==1)and(GPIO.input(ert)==1)):
			move_front()
		stop()
		time.sleep(0.2)
                while((GPIO.input(elt)==1)and(GPIO.input(ert)==1)):
                        move_front()
                stop()

           	while(not((GPIO.input(elt)==1)and(GPIO.input(ert)==1))):
                  	if((GPIO.input(lt)==1) and (GPIO.input(ctr)==1) and (GPIO.input(rt)==1)):
                        	move_front()
                  	if(GPIO.input(lt) == 0):
                        	turn_right()
                  	if(GPIO.input(rt)==0):
                        	turn_left()
                  	if((GPIO.input(lt)==0) and (GPIO.input(ctr)==0)and (GPIO.input(rt)==0)):
                        	stop()
				print("Executed before.")
                        	angle = down(theta)
				print("Executed After. ")
                        	r = c_r - 1
                        	c=c_c
				if(r<0):
					r=c_r
				q[c_r][c_c][a] += -500
			while(not((GPIO.input(elt)==1)and (GPIO.input(ert)==1))):
				if((GPIO.input(lt)==1)and (GPIO.input(ctr)==1) and(GPIO.input(rt)==1)):
					move_front()
				if(GPIO.input(lt)==0):
					turn_right()
				if(GPIO.input(rt)==0):
					turn_left()
			stop()
		stop()
	if (a==1):
		angle = down(theta)
            	r = c_r - 1
	    	if(r<0):
		  	r=c_r
            	c =c_c
                while((GPIO.input(elt)==1)and(GPIO.input(ert)==1)):
                        move_front()
                stop()

            	while(not((GPIO.input(elt)==1)and(GPIO.input(ert)==1))):
                  	if((GPIO.input(lt)==1)and (GPIO.input(ctr)==1) and(GPIO.input(rt)==1)):
                        	move_front()
                  	if(GPIO.input(lt) == 0):
                        	turn_right()
                  	if(GPIO.input(rt)==0):
                        	turn_left()
                  	if((GPIO.input(lt)==0) and (GPIO.input(ctr)==0) and (GPIO.input(rt)==0)):
                        	stop()
                        	angle=up(angle)
                        	r = c_r + 1
				if(r>2):
					r=c_r
                        	c=c_c
                        	q[c_r][c_c][a] += -500

                        while(not((GPIO.input(elt)==1)and (GPIO.input(ert)==1))):
 				if((GPIO.input(lt)==1)and (GPIO.input(ctr)==1) and(GPIO.input(rt)==1)):
                                	move_front()
                                if(GPIO.input(lt)==0):
                                        turn_right()
                                if(GPIO.input(rt)==0):
                                        turn_left()
			stop()
		stop()


	if (a==2):
		angle = left(theta)
            	r = c_r 
            	c =c_c -1
	    	if(c<0):
		   	c=c_c
                while((GPIO.input(elt)==1)and(GPIO.input(ert)==1)):
                        move_front()
                stop()

            	while(not((GPIO.input(elt)==1)and(GPIO.input(ert)==1))):
			if((GPIO.input(lt)==1)and (GPIO.input(ctr)==1) and(GPIO.input(rt)==1)):
                        	move_front()
                  	if(GPIO.input(lt) == 0):
                        	turn_right()
                  	if(GPIO.input(rt)==0):
                        	turn_left()
                  	if(GPIO.input(ctr)==0):
                        	stop()
                        	angle=right(angle)
                        	r = c_r 
                        	c=c_c + 1
				if(c>2):
					c=c_c
                        	q[c_r][c_c][a] += -500

                        while(not((GPIO.input(elt)==1)and (GPIO.input(ert)==1))):
				if((GPIO.input(lt)==1)and (GPIO.input(ctr)==1) and(GPIO.input(rt)==1)):
                                	move_front()
                                if(GPIO.input(lt)==0):
                                        turn_right()
                                if(GPIO.input(rt)==0):
                                        turn_left()
			stop()
		stop()

	if (a==3):
            	angle = right(theta)
            	r = c_r 
            	c =c_c + 1
	    	if(c>2):
	    	  	c = c_c
                while((GPIO.input(elt)==1)and(GPIO.input(ert)==1)):
                        move_front()
                stop()

            	while(not((GPIO.input(elt)==1)and(GPIO.input(ert)==1))):
                  	if((GPIO.input(lt)==1)and (GPIO.input(ctr)==1) and(GPIO.input(rt)==1)):
                        	move_front()
                  	if(GPIO.input(lt) == 0):
                        	turn_right()
                  	if(GPIO.input(rt)==0):
                        	turn_left()
                  	if((GPIO.input(lt)==0) and (GPIO.input(ctr)==0) and (GPIO.input(rt)==0)):
                        	stop()
                        	angle=down(angle)
                        	r = c_r 
                        	c=c_c - 1
				if(c<0):
					c=c_c
                        	q[c_r][c_c][a] += -500

                        while(not((GPIO.input(elt)==1)and (GPIO.input(ert)==1))):
		 		if((GPIO.input(lt)==1)and (GPIO.input(ctr)==1) and(GPIO.input(rt)==1)):
                                	move_front()
                                if(GPIO.input(lt)==0):
                                        turn_right()
                                if(GPIO.input(rt)==0):
                                        turn_left()
			stop()

            	stop()
            
	return [r, c, angle]


if __name__ == "__main__":


	for i in range(0, 10, 1):
		start_at =[0,0,0]
		reach = [2,0,0]
      		while True:
            		a=np.argmax(q[start_at[0]][start_at[1]])
            		if(a==0):
                  		policy[start_at[0]][start_at[1]] = "up"
				print("UP")
            		if(a==1):
                  		policy[start_at[0]][start_at[1]] = "down"
				print("DOWN")
            		if(a==2):
                  		policy[start_at[0]][start_at[1]] = "left"
				print("LEFT")
            		if(a==3):
                  		policy[start_at[0]][start_at[1]] = "right"
				print("RIGHT")
            		nxt = next_index(start_at[0], start_at[1], start_at[2], a, area_map)
            		q[start_at[0]][start_at[1]][a] +=0.4*((reward[nxt[0]][nxt[1]]) + 0.9*(np.amax(q[nxt[0]][nxt[1]])) - q[start_at[0]][start_at[1]][a])
            		start_at = nxt
			print("I have reached: ", nxt)
            		if ((nxt[0] == reach[0])and(nxt[1]==reach[1])):
                  		break
      		print(q)
      		print(policy)
                    

TECHNOLOGIES THAT'S GOING TO CHANGE THE WORLD!

New York

Embedded Systems

Dec. 30, 2018, 11:18 a.m.

New York

Internet of Things

Dec. 30, 2018, 11:19 a.m.

New York

ML in Computer Vision

Dec. 30, 2018, 11:20 a.m.

×

Tickets

Need help?


CONTACT

Want to convey anything? Drop a Message.

Mumbai, India
Phone: +91 9555551613
Email: ayamamit@gmail.com

.iframeclass{ position: absolute; top: 0; width: 100%; } .iframecontainer{ position: relative; width: 100%; height: auto; padding-top: 61%; }