python101
Posts: 50
Joined: Sun Jun 22, 2014 11:20 am

Robot

Fri Jul 03, 2015 10:52 am

I have created a robot and wanted to control it to a website. I have got the website to work fine but the python script isn't working. The robot responds to just one command and after one command it stops (Say if i typed in forward then backward, it would go forward then stop). Here is the python script:

Code: Select all

import time
import RPi.GPIO as io
import pyttsx
import sys

engine = pyttsx.init()

io.setwarnings(False)

io.setmode(io.BCM)
#Motor 1 is designed to be the motors on the left, Motor 2 is designed to be on$
#If one motor is in the wrong direction you can swap the pins around to save yo$
m1a = 17 #Motor 1 Forwards
m1b = 18 #Motor 1 Backwards
m2a = 22 #Motor 2 Forwards
m2b = 23 #Motor 2 Backwards
pins = (m1a,m1b,m2a,m2b)
for i in pins:
  io.setup(i,io.OUT)

for i in pins:
  io.output(i,False)

while True:
  file=open("/var/www/test.txt", "r")
  command=file.read()
  if(command=="Forward"):
    io.output(m1a, True)
    io.output(m2b, True)
  elif(command=="Backward"):
    io.output(m1b, True)
    io.output(m2a, True)
All Help would be appreciated

Code: Select all

if human contains caffeine:
work.hard()
else:
state.sleep()

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Robot

Fri Jul 03, 2015 10:57 am

The file is being continually opened and re-read from the start in the while True loop.

Open the file before the while loop.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Robot

Fri Jul 03, 2015 11:23 am

Also, if that file has multiple lines then you'll need to loop over each line and process each command. "read()" loads the whole file into a string, "readlines()" will give you a list that you can loop over.

I suspect this is intentional and that your website just writes a single command to your file. It would help if you could give an example of the contents of the file and also explain how often it's updated.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

python101
Posts: 50
Joined: Sun Jun 22, 2014 11:20 am

Re: Robot

Fri Jul 03, 2015 2:10 pm

When I put the open file command outside the while loop the robot didn't move at all but when I looked at the text file it had the word "Forward" in it so that worked. Also it is just one line in the text file.
Python Script:

Code: Select all

import time
import RPi.GPIO as io
import pyttsx
import sys

engine = pyttsx.init()

io.setwarnings(False)

io.setmode(io.BCM)
#Motor 1 is designed to be the motors on the left, Motor 2 is designed to be on$
#If one motor is in the wrong direction you can swap the pins around to save yo$
m1a = 17 #Motor 1 Forwards
m1b = 18 #Motor 1 Backwards
m2a = 22 #Motor 2 Forwards
m2b = 23 #Motor 2 Backwards
pins = (m1a,m1b,m2a,m2b)
for i in pins:
  io.setup(i,io.OUT)

for i in pins:
  io.output(i,False)

file=open("/var/www/test.txt", "r")
command=file.read()

while True:
  if(command=="Forward"):
    io.output(m1a, True)
    io.output(m2b, True)
  elif(command=="Backward"):
    io.output(m1b, True)
    io.output(m2a, True)

The text file:

Code: Select all

Forward

Code: Select all

if human contains caffeine:
work.hard()
else:
state.sleep()

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Robot

Fri Jul 03, 2015 3:03 pm

How and when is you txt file being written? If it's written after your code enters that while loop then this isn't going to work.

I also can't help but thing that this can't be the best way to control your robot from the website. It seems quite inefficient to have the web page write to a text file and then have your script read the file. Would something like WebIOPi (which lets you control the GPIO pins from a webpage) be an option for you?
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

python101
Posts: 50
Joined: Sun Jun 22, 2014 11:20 am

Re: Robot

Fri Jul 03, 2015 3:19 pm

I'm gonna use WebIOPi thanks for all the help

Code: Select all

if human contains caffeine:
work.hard()
else:
state.sleep()

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Robot

Fri Jul 03, 2015 3:29 pm

python101 wrote:I'm gonna use WebIOPi thanks for all the help
OK. Don't get me wrong, if you want to get your script to work as it is then I'll happily help if I can.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

danjperron
Posts: 3502
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Robot

Fri Jul 03, 2015 4:07 pm

I use webiopi.

Check this post. I did post some info about my robot.

viewtopic.php?p=783281#p783281

python101
Posts: 50
Joined: Sun Jun 22, 2014 11:20 am

Re: Robot

Fri Jul 03, 2015 4:22 pm

Is it possible to customize the look of it because this robots for my family and they won't understand how it looks

Code: Select all

if human contains caffeine:
work.hard()
else:
state.sleep()

danjperron
Posts: 3502
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Robot

Fri Jul 03, 2015 4:42 pm

Webpage look or robot physical look.

The webpage look could be very simple or very complicated. This is all depends how much time you want to put into your design.

But the core is the same it is just another layer with drawing and images.

B.T.W. this code

Code: Select all

if human contains caffeine:
work.hard()
else:
state.sleep()
Will give error since you are missing the indentation!

Also you need to include more conditions like the lunch time, the break, the rest room interrupt, people interactions, phone sms , the dog, the cat, watering the plant, burglar alarm .... so many variables....
Last edited by danjperron on Fri Jul 03, 2015 7:39 pm, edited 1 time in total.

python101
Posts: 50
Joined: Sun Jun 22, 2014 11:20 am

Re: Robot

Fri Jul 03, 2015 4:52 pm

I'm just looking to have simple commands like forward (pin 18 and 23) backward (pin 19 and 22) left and right. Also I would need to put in a custom text box for something else

Code: Select all

if human contains caffeine:
work.hard()
else:
state.sleep()

python101
Posts: 50
Joined: Sun Jun 22, 2014 11:20 am

Re: Robot

Fri Jul 03, 2015 5:08 pm

When I tried WebIOPi it just came up with a bunch of python errors

Code: Select all

Exception happened during processing of request from ('192.168.1.83', 61479)
Traceback (most recent call last):
  File "/usr/lib/python2.7/SocketServer.py", line 295, in _handle_request_noblock
    self.process_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 321, in process_request
    self.finish_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/lib/python2.7/SocketServer.py", line 649, in __init__
    self.handle()
  File "/usr/lib/python2.7/BaseHTTPServer.py", line 340, in handle
    self.handle_one_request()
  File "/usr/lib/python2.7/BaseHTTPServer.py", line 328, in handle_one_request
    method()
  File "build/bdist.linux-armv6l/egg/webiopi/protocols/http.py", line 267, in do_GET
    self.processRequest()
  File "build/bdist.linux-armv6l/egg/webiopi/protocols/http.py", line 258, in processRequest
    except (GPIO.InvalidDirectionException, GPIO.InvalidChannelException, GPIO.SetupException) as e:
NameError: global name 'GPIO' is not defined

Code: Select all

if human contains caffeine:
work.hard()
else:
state.sleep()

danjperron
Posts: 3502
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Robot

Fri Jul 03, 2015 8:10 pm

When I tried WebIOPi it just came up with a bunch of python errors
where is the meat?

How did you installed webiopi.


Step 1 - Get most recent webiopi and decompress it.

Code: Select all

cd ~
wget  http://downloads.sourceforge.net/project/webiopi/WebIOPi-0.7.1.tar.gz
tar -xzf WebIOPi-0.7.1.tar.gz
Step 2- install webiopi

Code: Select all

cd WebIOPi-0.7.1
sudo ./setup.sh
Answer 'no' to 'Do you want to access WebIOPi over Internet ? [y/n]'

et voila!

Reboot or start webiopi manually.

Code: Select all

sudo service webiopi start
from a browser input the IP of your raspberry Pi with the port 8000.
It should respond with the main webpage of webiopi.

The user is 'webiopi' and the password is 'raspberry'.

If your Raspberry Pi IP is 192.168.0.100 use this url : http://192.168.0.100:8000

Next will be to implement your own functions into webiopi. Tell me when you have the basic webiopi running and I will explain to you how to create you own function into a python file if you like.

danjperron
Posts: 3502
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Robot

Sat Jul 04, 2015 2:27 am

On the first post the command to go forward and backward doesn't toggle off the GPIO. I'm confuse the way they work!
I thing you forget to turn them OFF.
I assume that your are using H type driver and each GPIO switch power or ground to the motor.
I did make a small python script which uses your corresponding GPIO and assumes a H type driver.

1 - First create your python script

Code: Select all

cd
nano  WebRobot.py

Code: Select all

#!/usr/bin/env python
import webiopi
import RPi.GPIO as io


io.setwarnings(False)
io.setmode(io.BCM)


m1a = 17 
m1b = 18 
m2a = 22 
m2b = 23 

#assume motor1 forward  is  m1a=True  m1b=False
#assume motor1 backward is  m1a=False m1b=True
#assume motor2 forward  is  m2a=True  m2b=False
#assume motor2 backward is  m2a=False m2b=True


pins = (m1a,m1b,m2a,m2b)
for i in pins:
  io.setup(i,io.OUT)


@webiopi.macro
def Forward():
   io.output(m1b,False)
   io.output(m2b,False)
   io.output(m1a,True)
   io.output(m2a,True)

@webiopi.macro
def Stop():
   io.output(m1b,False)
   io.output(m2b,False)
   io.output(m1a,False)
   io.output(m2a,False)

@webiopi.macro
def Backward():
   io.output(m1a,False)
   io.output(m2a,False)
   io.output(m1b,True)
   io.output(m2b,True)

@webiopi.macro
def TurnRight():
   io.output(m1a,False)
   io.output(m2b,False)
   io.output(m1b,True)
   io.output(m2a,True)

@webiopi.macro
def TurnLeft():
   io.output(m1b,False)
   io.output(m2a,False)
   io.output(m1a,True)
   io.output(m2b,True)


Stop()
2 - add the script into the webiopi config after the SCRIPTS label

Code: Select all

sudo nano /etc/webiopi/config

Code: Select all

[SCRIPTS]
# Load custom scripts syntax :
# name = sourcefile
#   each sourcefile may have setup, loop and destroy functions and macros
#myscript = /home/pi/webiopi/examples/scripts/macros/script.py
myscript = /home/pi/WebRobot.py
3 - Restart webiopi

Code: Select all

sudo service webiopi restart
if any problem check the log in /var/log/webiopi

I also create a simple html page to control the motors.

4 - create the webpage

Code: Select all

sudo nano /usr/share/webiopi/htdocs/WebRobot.html

Code: Select all

<html>
<head>
<title>WebRobot</title>

<script type="text/javascript" src="/webiopi.js"></script>
<script type="text/javascript">
   function Forward() {
       webiopi().callMacro("Forward");
    }

   function Stop() {
       webiopi().callMacro("Stop");
    }

   function TurnLeft() {
       webiopi().callMacro("TurnLeft");
    }

   function TurnRight() {
       webiopi().callMacro("TurnRight");
    }

   function Backward() {
       webiopi().callMacro("Backward");
    }

</script>


<style type="text/css">

.myButton {
	-moz-box-shadow:inset 0px 1px 0px 0px #ffffff;
	-webkit-box-shadow:inset 0px 1px 0px 0px #ffffff;
	box-shadow:inset 0px 1px 0px 0px #ffffff;
	background:-webkit-gradient(linear, left top, left bottom, color-stop(0.05, #ffffff), color-stop(1, #f6f6f6));
	background:-moz-linear-gradient(top, #ffffff 5%, #f6f6f6 100%);
	background:-webkit-linear-gradient(top, #ffffff 5%, #f6f6f6 100%);
	background:-o-linear-gradient(top, #ffffff 5%, #f6f6f6 100%);
	background:-ms-linear-gradient(top, #ffffff 5%, #f6f6f6 100%);
	background:linear-gradient(to bottom, #ffffff 5%, #f6f6f6 100%);
	background-color:#ffffff;
	-moz-border-radius:6px;
	-webkit-border-radius:6px;
	border-radius:6px;
	border:1px solid #dcdcdc;
	display:inline-block;
	cursor:pointer;
	color:#666666;
	font-family:Arial;
	font-size:15px;
	font-weight:bold;
	padding:6px 24px;
	text-decoration:none;
	text-shadow:0px 1px 0px #ffffff;
        width: 130px;
        height:30px;
        margin:1px;
}
.myButton:hover {
	background:-webkit-gradient(linear, left top, left bottom, color-stop(0.05, #f6f6f6), color-stop(1, #ffffff));
	background:-moz-linear-gradient(top, #f6f6f6 5%, #ffffff 100%);
	background:-webkit-linear-gradient(top, #f6f6f6 5%, #ffffff 100%);
	background:-o-linear-gradient(top, #f6f6f6 5%, #ffffff 100%);
	background:-ms-linear-gradient(top, #f6f6f6 5%, #ffffff 100%);
	background:linear-gradient(to bottom, #f6f6f6 5%, #ffffff 100%);
	background-color:#f6f6f6;
        padding:6px 24px;
}
.myButton:active {
	color:#ff0000;
        position:relative;
	top:1px;
        padding:6px 24px;
}

</style>

</head>
<body>
<center><h3> My robot</h3></center><br>


<center>
<table>
<tr><td colspan=3 align="center"><button class="myButton" onmousedown="Forward()" onmouseup="Stop()">Forward</button></td></tr>
<tr><td><button class="myButton" type="button" onmousedown="TurnLeft()" onmouseup="Stop()">Left</button></td>
<td><button class="myButton" type="button" onclick="Stop()">Stop</button></td>
<td><button class="myButton" type="button" onmousedown="TurnRight()" onmouseup="Stop()">Right</button></td></tr>
<tr><td colspan=3 align="center"><button class="myButton" type="button" onmousedown="Backward()" onmouseup="Stop()">Backward</button></td></tr>
</table>
</center>


</body>
</html>
Since the default button layout on webiopi are change with webiopi.css, I include a css class to give the button a normal style.
You could change the look of the button by changing the style of it. I use this webpage to create myButton style. http://www.bestcssbuttongenerator.com/#/23

Now if you open the url with your RaspberryPi IP on port 8000 something like this

http://192.168.0.100:8000/WebRobot.html it should work.

If it doesn't work, open a second browser's window with the webiopi GPIO status and check if they toggle when you press the button.


like this https://dl.dropboxusercontent.com/s/ivu ... bRobot.mp4
B.T.W. For tablet onmouse won't work you need ontouch !

Also the stop button do nothing since no button press = Stop
Last edited by danjperron on Thu Aug 01, 2019 5:48 pm, edited 1 time in total.

cristobal.aguila
Posts: 2
Joined: Wed Jul 08, 2015 2:09 am

Re: Robot

Wed Jul 08, 2015 1:02 pm

Hi,
In your first code I think the problem was that you didn't close the text file. So you was reading the file once and then your webpage would not be able to write to the file again. Try writing at the end of your while file.close()

Code: Select all

import time
import RPi.GPIO as io
import pyttsx
import sys

engine = pyttsx.init()

io.setwarnings(False)

io.setmode(io.BCM)
#Motor 1 is designed to be the motors on the left, Motor 2 is designed to be on$
#If one motor is in the wrong direction you can swap the pins around to save yo$
m1a = 17 #Motor 1 Forwards
m1b = 18 #Motor 1 Backwards
m2a = 22 #Motor 2 Forwards
m2b = 23 #Motor 2 Backwards
pins = (m1a,m1b,m2a,m2b)
for i in pins:
  io.setup(i,io.OUT)

for i in pins:
  io.output(i,False)

while True:
  file=open("/var/www/test.txt", "r")
  command=file.read()
  if(command=="Forward"):
    io.output(m1a, True)
    io.output(m1b, False)
    io.output(m2a, False)
    io.output(m2b, True)
  elif(command=="Backward"):
    io.output(m1a, False)
    io.output(m1b, True)
    io.output(m2a, True)
    io.output(m2b, False)
  file.close()
Also I recommend you using bottle as your web server. It's very simple and easy to use.

Return to “Python”