So I had a great deal of problems with an USB wifi adapter for the Raspberry Pi that I bought on dealextreme.com. It just wasn’t stable.
Yesterday I got another USB wifi adapter, the Edimax EW-7811Un. It works great, I can recommend it. So I now had the hardware to try out prototype #2 for my R/C car.
Right now I have my Raspberry Pi powered by a USB battery pack. It runs alright but I’m not sure how much time I will get from a full charge. Then the Arduino is plugged into one USB port on the RPi, and the Edimax adapter in the other port. A 3-cell LiPo battery (mentioned before) gives power to the H-bridge and I ended up using the switch mode adapter to get 5V for the servo, as I had problems with it drawing too much current from the Arduino board and thus resetting it, sometimes just once, sometimes many times after each other.
On the RPi I have installed: nginx, uwsgi and Flask. These make my webserver. I followed one of many guides on the web to get those 3 to work together – it took hours to figure out because my linux skills are limited at best.
Here is my python code for Flask:
from flask import Flask, render_template, request, url_for app = Flask(__name__) import serial def do(action): port = serial.Serial("/dev/serial/by-id/usb-Arduino__www.arduino.cc__0043_5533033393035131C012-if00", baudrate=9600, timeout=3.0) port.open() port.write(action) port.close() @app.route("/", methods=['POST', 'GET']) def hello(): if request.method == 'POST': do(request.form['action']) print(request.form['action']) return "OK" else: return render_template('index.html') if __name__ == "__main__": app.run(host='0.0.0.0', port=8080)
I used this long path for serial control instead of simply “/dev/ttyACM0” because it would sometimes change to “/dev/ttyACM1” and then I had to change the code every time. I didn’t have that problem with this path, so I am guessing it won’t change.
This is index.html:
<title>Hello from Flask</title>
xmlhttp.send(“action=” + action);
<link rel=”stylesheet” type=”text/css” href=”static/style.css”/>
<input type=”image” onClick=”sendAction(‘M,50,S,-25’)” src=”static/upleft.gif” />
<input type=”image” onClick=”sendAction(‘M,100,S,0’)” src=”static/upup.gif” />
<input type=”image” onClick=”sendAction(‘M,50,S,25’)” src=”static/upright.gif” />
<input type=”image” onClick=”sendAction(‘M,50,S,0’)” src=”static/up.gif” />
<input type=”image” onClick=”sendAction(‘M,0,S,0’)” src=”static/stop.gif” />
<input type=”image” onClick=”sendAction(‘M,-50,S,0’)” src=”static/down.gif” />
<input type=”image” onClick=”sendAction(‘M,-50,S,-25’)” src=”static/downleft.gif” />
<input type=”image” onClick=”sendAction(‘M,-100,S,0’)” src=”static/downdown.gif” />
<input type=”image” onClick=”sendAction(‘M,-50,S,25’)” src=”static/downright.gif” />
So for each button I have a div so I can control its position with css. I am using AJAX to send the request to python without having to reload the page each time I push one of the buttons.
I remapped the serial commands, so instead of full speed being “M,255,S,0” it is now “M,100,S,0”. Seems more logical.
The css file:
As you can see, most of the buttons didn’t need to be positioned. The page now looked like this:
Pushing any of the buttons will send a POST request which will be handled by the python script and sent by serial connection to the Arduino. It worked alright until it suddenly wouldn’t run in reverse, so I sadly didn’t get to record any videos of it functioning.
It seems I broke my H-bridge. I don’t really know how, but I know that I won’t make another myself! I will keep it as an artifact (hmm maybe frame it? lol) and buy an IC for the project instead.
Some nice people on the subreddit /r/arduino recommended for me to add some diodes for the current to flow back from the motors to the battery, when the motors are turned off, to prevent the current from going through the transistors the wrong way. I did actually do that. So I am truly puzzled.
I guess I will also have to move away from the lunchbox as platform, and the straw and cardboard solution. Any ideas? Comments are more than welcome.