Build Project a Raspberry How to make Pi-controlled Car

Build Project  a Raspberry How to make  Pi-controlled Car 





Make use of cutting-edge web technologies to take control of a remote controlled car with a smartphone or tablet…


Web technologies are moving forward at an enormous pace, cloud Technologies bring collective computing to individuals, And devices have reached a perfect moment in time All sensors, displays and wireless technology have evolved Powerful and affordable devices. We are really at a point Almost everyone can take an idea from nothing to work Product within a week, at very little cost. Just like this project, It's fun, fast, easy to build, and a great way Learn. We'll take an old car with a remote control, tear it apart Radio receiver and replace it with Raspberry Pi, connect it Network, turn on the bleeding web server and get it Your smartphone or tablet to control by tilting the device. by At the end of this, you will not only have a fun game - you will have I learned about the basic techniques that started in power The latest and largest economy in the world for the foreseeable future future. Welcome to tomorrow!

Raspberry Pi-controlled car build process


Before you can take control of your car with a smartphone, you’ll need to make some signifi cant changes to the chassis



To help our toy car come to life using the latest web technologies and our credit card-sized computer, we’re going to need to make some pretty signifi cant changes to its workings. Fortunately, the most complex aspects of the build can be accomplished with a couple of affordable purchases, namely a servo controller board to take care of the steering and throttle, and a 5V battery pack to keep the Raspberry Pi running smoothly.



01 Identify and remove old radio


This project is effectively replacing the car’s normal transmitter and receiver. Notice the three

Sockets on the original receiver:
 one goes to the motor controller and one to the steering servo. Some remote-control cars also have separate battery for the electronics, but those (especially with an electronic speed
controller with BEC) get their 5V power supply directly from the speed controller, saving on components. If you don’t have a speed controller with 5V BEC, you’ll need to get a 5V supply elsewhere. Many shops sell 5V battery power supplies – often as mobile phone emergency top-ups. www.modmypi.com sells a suitable 5V battery power bank for under £20.

We’re using the Raspberry Pi’s I2C bus to control the servo interface board

02 Attach the servo cables to the new controller 

02 Attach the servo cables to the new controller



 We soldered our 16-channel I2C servo controller board from www.modmypi.com as per its instructions and simply plugged channel 0 (steering) and channel 1 (motor) headers onto it. There are six cables in total: the bottom two are ground, the middle two are the power and the top two are the PWM (pulse-width modulation) signals. This is a good time to think of places to mount the extra components and the best fixing method seems to be sticky-back Velcro.

03 Connect the I2C bus to the Raspberry Pi




03 Connect the I2C bus to the Raspberry Pi



 We’re using the Raspberry Pi’s I2C bus to control the servo interface board, which only needs four cables – they all go between the Raspberry Pi and the servo controller board as pictured.
This month’s accelerometer tutorial explains how to set up I2C on the Raspberry Pi. From top to bottom we need to use the 1. GND, 2. SCL, 3. SDA and 4. VCC, which map directly to the same ports on the Raspberry Pi. Essentially this is power, ground and two communication channels – it’s all pretty straightforward so far…

04 Hooking it up to theRaspberry Pi


 On a Rev 1 Raspberry Pi, the cables look the same. Though the Rev boards have different labelling, the physical pins are in the same place. Bottom left (closest to the RasPi power connector) is the 3.3V power; next to that is the SDA header which is the data channel. Next to that in the bottom right is the SCL channel, which controls the clock of the I2C devices. And finally – on the top-right port – is the Ground.


05 Overview of the main components 

05 Overview of the main components



You should now have the servo board in the middle with the steering servo and speed controller on one side and the Raspberry Pi on the other. The motor is connected to the other end of the speedcontroller (that end should have muchthicker wires); the  speed controlleralso has two thick wires going to the main car’s battery – in this case a 7.2VNiCad. We now have two very separate power systems with the high current
motors on one side and the low current electronics on the other. Let’s make sure it stays that way!


06 Find everything a home


06 Find everything a home


 Now it’s time to find a home for the new components. Use plenty of sticky-back Velcro, tie wraps or elastic bands to keep everything secure and find spaces in the car’s body to hide the wires where possible. While it is possible to stick or screw the Raspberry Pi directly to the car, we recommend to use at least the bottom half of a case for added protection and ease of access. Insert your SD card, network cable or Wi-Fi dongle (if programming from another machine) and power supply. Sit back and admire your hacking. Next we’ll tackle the software side of the project…



Controlling your Raspberry Pi-powered car

Control a toy car with a smartphone and the latest web technologies


Now we have our fantastic Raspberry Pi-powered car all wired and charged, it’s time to make it come alive. We’re using the best web technologies that the JavaScript programming language offers, to harness the natural movement of your hand and wirelessly drive the vehicle. Each little movement will trigger an event that calculates
what the car should do and then sends it over a socket connection up to 20 times a second.

01 Download and install


the software To get the I2C connectivity working, you can follow the steps from pages
64-65. 
Next we’ll need to fi nd a home for our new project code – how about / var/www/picar? Type sudo mkdir / var/www/picar in the terminal to make the directory and then change into that directory: cd /var/www/picar Now, to download the project using Git, type sudo git clone http:// github.com/shaunuk/picar. If you
haven’t got Git, install it with sudo aptget install git.

This will download the custom software for driving the car, but we still
need the web server and some other bits before we can start burning rubber…

02 Download and install


Node.js We now need to get the awesome Node. js and its package tool, the Node package manager (npm). Type sudo wget http:// nodejs.org/dist/v0.10.21/nodev0.10.21-
linux-arm-pi.tar.gz. This will download a fairly recent version of Node.js – the version Raspbian has in its repositories is way too old and just doesn’t work with the new technologies

we’re about to use. Extract the node package by typing sudo tar -xvzf node-v0.10.21-linux-arm-pi.tar.gz.

03 Confi gure Node.js


To make it easy to run from everywhere, we will create symbolic links for Node and npm binaries. In the terminal, type sudo ln -s /var/www/ node-v0.10.21-linux-arm-pi/bin/ node /bin/node and then sudo ln -s /var/www/node-v0.10.21-linuxarm pi/bin/npm /bin/npm. Then, to get the extra modules, type npm install socket.io node-static socket.io adafruit-i2c-pwm-driver sleep optimist 

04 Get to know the project


 Now we have everything, you should see three fi les: the server (app.js), the client (socket.html) and the jQuery JavaScript library for the client. The server not only drives the servos, but it is a web server and sends the socket. html fi le and jQuery to the browser when requested – it’s a really neat and simple setup and just right for what we’re trying

05 Test the servos


05 Test the servos

Our handy little program (app.js) has a special mode just for testing. We use two keywords here: beta for servo 0 (steering) and gamma for servo 1 (motor control). Type node app.js beta=300. You should see the front wheels turn. Now the numbers need experimenting with. On our example, 340 was left, 400 was centre and 470 was right. Do the same for the motor by typing node app.js gamma=400 and take note of the various limits of your car.

06 Confi gure sensible defaults


Now you know what your car is capable of, we can set the defaults in app.js and socket.html. Edit app.js and fi nd the section that says ‘function emergencyStop’. Adjust the two numbers to your car’s rest values. Then open socket.html and adjust the predefi ned values under ‘Defi ne your variables here’.

07 Going for a spin

07 Going for a spin

We’re almost ready to try it out, but you need to know the IP address of your Pi car, so type ifconfig at the terminal. Then fi re up the app by typing node app.js. Now grab the nearest smartphone or tablet, making sure it’s on the same network as your Pi. Open the web browser and go to http://[your IP address]:8080/socket.html. You should get an alert message saying ‘ready’ and as soon as you hit OK, the gyro data from your
phone will be sent to the car and you’re off!

We’ll harness the natural movement of your hand and wirelessly drive the vehicle

                                                     
 Full code listing






socket.html

<html>
<head>
<script src=”jquery-2.0.3.min.js” language=”javascript”></
script>
<script src=”/socket.io/socket.io.js”></script>
<meta name=”viewport” content=”user-scalable=no, initialscale=
1.0, maximum-scale=1.0;” />
<script>
//------ Define your variables here
var socket = io.connect(window.location.hostname+’:8080’);
var centerbeta = 400; //where is the middle?
var minbeta = ‘340’; //right limit
var maxbeta = ‘470’; //left limit
var multbeta = 3; //factor to multiply the raw gyro figure 􀀛􀀃
by to get the desired range of steering
var centergamma = 330;
var ajustmentgamma = 70; //what do we do to the angle to get
to 0?
var mingamma = 250; //backwards limit
var maxgamma = 400; //forward limit
var multgamma = 1; //factor to multiply the raw gyro figure 􀀛􀀃
by to get the desired rate of acceleration
window.lastbeta=’0’;
window.lastgamma=’0’;
$(function(){
window.gyro = ‘ready’;
alert(‘Ready -- Lets race !’);
});
window.ondeviceorientation = function(event) {
beta = centerbeta+(Math.round(event.beta*-1)*multbeta);
if (beta >= maxbeta) {
beta=maxbeta;
}
if (beta <= minbeta) {
beta=minbeta;
}
gamma = event.gamma;
gamma = ((Math.round(event.gamma)+ajustmentgamma)* 􀀛􀀃
multgamma)+ centergamma;
//stop sending the same command more than once
send = ‘N’;
if (window.lastbeta != beta) { send = ‘Y’ }
if (window.lastgamma != gamma) { send = ‘Y’ }
window.lastbeta=beta;
window.lastgamma=gamma;
if (window.gyro == ‘ready’ && send==’Y’) { //don’t send 􀀛
another command until ready...
window.gyro = ‘notready’;
socket.emit(‘fromclient’, { beta: beta, gamma: gamma } );
window.gyro = ‘ready’; }}
app.js
//declare required modules
var app = require(‘http’).createServer(handler)
, io = require(‘socket.io’).listen(app)
, fs = require(‘fs’)
, static = require(‘node-static’)
, sys = require(‘sys’)
, PwmDriver = require(‘adafruit-i2c-pwm-driver’)
, sleep = require(‘sleep’)
, argv = require(‘optimist’).argv;
app.listen(8080);



//set the address and device name of the 􀀛􀀃
breakout board
pwm = new PwmDriver(0x40,’/dev/i2c-0’);
//set pulse widths
setServoPulse = function(channel, pulse) {
var pulseLength;
pulseLength = 1000000;
pulseLength /= 60;
print(“%d us per period” % pulseLength);
pulseLength /= 4096;
print(“%d us per bit” % pulseLength);
pulse *= 1000;
pulse /= pulseLength;
return pwm.setPWM(channel, 0, pulse);
};
//set pulse frequency
pwm.setPWMFreq(60);
//Make a web server on port 8080
var file = new(static.Server)();
function handler(request, response) {
console.log(‘serving file’,request.url)
file.serve(request, response);
};
console.log(‘Pi Car we server listening on port 8080 visit 􀀛
http://ipaddress:8080/socket.html’);
lastAction = “”;
function emergencyStop(){
pwm.setPWM(0, 0, 400); //center front wheels
pwm.setPWM(1, 0, 330); //stop motor
console.log(‘###EMERGENCY STOP - signal lost or shutting 􀀛
down’);
} if (argv.beta) {
console.log(“\nPerforming one off servo position move 􀀛􀀃􀀃
to: “+argv.beta);
pwm.setPWM(0, 0, argv.beta); //using direct i2c pwm module
pwm.stop();
return process.exit();
} if (
argv.gamma) {
console.log(“\nPerforming one off servo position move 􀀛􀀃􀀃
to: “+argv.gamma);
pwm.setPWM(1, 0, argv.gamma); //using direct i2c pwm module
pwm.stop();
return process.exit();
}
//fire up a web socket server
io.sockets.on(‘connection’, function (socket) {
socket.on(‘fromclient’, function (data) {
console.log(“Beta: “+data.beta+” Gamma: “+data.gamma);
//exec(“echo ‘sa “+data+”’ > /dev/ttyAMA0”, puts); //using 􀀛
http://electronics.chroma.se/rpisb.php
//exec(“picar.py 0 “+data.beta, puts); //using python 􀀛􀀃
adafruit module
pwm.setPWM(0, 0, data.beta); //using direct i2c pwm module
pwm.setPWM(1, 0, data.gamma); //using direct i2c pwm module
clearInterval(lastAction); //stop emergency stop timer
lastAction = setInterval(emergencyStop,1000); //set 􀀛􀀃
emergency stop timer for 1 second
});
});
process.on(‘SIGINT’, function() {
emergencyStop();
console.log(“\nGracefully shutting down from SIGINT 􀀛
(Ctrl-C)”);
pwm.stop();
return process.exit();
});

Publier un commentaire

0 Commentaires