Do you want to build a robot

Post on 29-Jan-2018

169 views 0 download

transcript

Doyouwanttobuildarobot?

AnnaGerber

RobotDesignArobotisanautonomoussystemthatsensesandrespondsto,oractsuponthephysicalworld

Sensors(Inputse.g.ultrasonicsensor)

Control(Microcontrolleror

Single-Board-Computer)

Actuators(Outputse.g.motors)

PowerChassis

JSRobotics

• http://johnny-five.io/• https://www.espruino.com/• https://tessel.io/• https://docs.particle.io/reference/javascript/• https://cylonjs.com/• http://jerryscript.net/• http://mujs.com/• http://duktape.org/

Selectinghardware

• Johnny-FivesupportsArduino,RaspberryPi,Tessel,Particle,BeagleBone,andmore

Johnny-Five• OpenSourceJavaScriptRoboticsFrameworkforNode.jshttps://github.com/rwaldron/johnny-five• CommunicateswithmicrocontrollerslikeArduinousingtheFirmata protocol• Runson-boarddevicesthatcanrunNode.jse.g.RaspberryPi,BeagleBone Black,viaI/OPlugins

SetupRaspberryPiZeroW(headless)

• InstallRaspbian:• https://www.raspberrypi.org/documentation/installation/installing-images/README.md

• Createafilenamedwpa_supplicant.conf onthemicroSDcard:network={

ssid="campjs"psk="morecoffee"

}

• Addafilenamedssh (contentsdon’tmatter)ontherootofthemicroSDcardtoenableSSHonboot

ConnecttotheRaspberryPi

• Rpi usesmDNS soconnecttothePioverSSH:• ssh pi@raspberrypi.local• Defaultpasswordisraspberry• (Makesureyouchangethis)

• Raspbian comeswithnode.js installedbutifyouareusingsomethingelse,installnode• Thenusenpm toinstallthedependencies:

• npm installserialport• npm installjohnny-fiveraspi-io

RaspberryPiGPIO

• SoldersomeheadersontotheRaspberryPiZeroWfortheGPIOpins• Peripheralcomponents(e.g.sensors,actuators)attachviathesepins• 3.3Vlogic• InJohnny-Fivethepinsarenamed"P[header]-[pin]”e.g.P1-7

Breadboard• Usetoprototypecircuitswithoutsolderingbypluggingincomponentsandjumperwires• Numberedrowsareconnected• Somehavepowerrailsalongthesides

AttachanLEDusingaBreadboard

WritingJohnny-Fiveprograms

1. CreateaJavaScriptfile(e.g.blink.js)onthePi2. Edititusingatexteditor3. Requirethejohnny-fivelibraryandraspi-io librariesintoandsetupthe

boardobjectusingtheIO/Plugin

const raspi = require('raspi-io');const five = require('johnny-five');const board = new five.Board({io: new raspi()

});

Readyevent

• Whentheboardisreadyforourcodetostartinteractingwithitandtheattachedsensorsandactuators,itwilltriggerareadyevent

board.on('ready', () => {// code for sensors, actuators goes here

});

LED

• CreateanLedinstance

// attach LED on pin 7const led = new five.Led('P1-7');

// call strobe function to blink once per secondled.strobe(1000);

• Wecanchangetheparametertothestrobefunctiontochangethespeed:Thisinputvalueisprovidedinmilliseconds

LEDblinkprogram

const raspi = require('raspi-io');const five = require('johnny-five');const board = new five.Board({

io: new raspi()}); board.on('ready', () => {

// LED attached to RPi pin 7 (GPIO4)const led = new five.Led('P1-7');

led.strobe(500); });

Inputs- Sensors

• Environmentalconditions (e.g.temperature,humidity)•Magnetic(e.g.halleffectsensor)• Light(e.g.photoresistor)• Sound(e.g.microphone,piezo)•Movement/position(e.g.accelerometer,tiltswitch)• UserInput(e.g.button)

InputsPHOTORESISTORResistance changesdependingontheamountof ambient light

TILTSWITCHDetectorientation

PUSHBUTTONAlso knownasmomentaryswitch

PIEZOELEMENTDetectvibrations orknocks

TEMPERATURESENSORRead theambienttemperature

Outputs

• Light&Displays(e.g.LED,LCDscreen)• Sound(e.g.Piezo buzzer)•Movement(e.g.Servo,DCMotor,Solenoid)• Relays

OutputsPIEZOELEMENTApulseofcurrentwillcauseittoclick.Astreamofpulseswillcauseittoemitatone.

RGBLEDWeareusingCommonCathodeRGBLEDs.Thelongerleadisthecommonleadwhichconnectstoground.ThethreeotherleadsareforRed,GreenandBluesignal

9GHOBBYSERVOAbox containingamotorwithgearstomakeitpositionable from0to180degrees.

Digitalvs Analog

• Digital• discretevalues(0or1)• Examples:tiltsensor,pushbutton

• Analog• continuousvalues• typicallyvaluesforanalogsensorsareconstrainedwithinarangee.g.0– 255,0– 1023

• Example:photoresistor

• Somecomponentssupportbothdigitalandanalog

REPL

• Read,Eval,PrintLoop• Aconsoleforreal-timeinteractionwiththecode• ExposeourvariablestotheREPLtoenableinteractivecontrol:

board.on('ready', () => {// LED attached to RPi pin 7 (GPIO4)const myLed = new five.Led('P1-7');

myLed.strobe(500);board.repl.inject({

led: myLed});

});

ControllingtheLEDviatheREPL

• AttheREPLprompttypecommandsfollowedbyenter• Try:

• stop,• on,• off,• toggle,• strobee.g:>>led.stop()

Buttons

const button=newfive.Button("P1-11");const led=newfive.Led("P1-7");

button.on("down",(value)=>{led.on();});button.on(”up",(value)=>{led.off();});

http://johnny-five.io/api/button/

Servos

const myServo =newfive.Servo("P1-35");

board.repl.inject({servo:myServo

}); myServo.sweep();

board.wait(5000,()=>{myServo.stop();myServo.center();

});

PWM

• PulseWidthModulation• Produceanalogoutputviadigitalpins• Insteadofonoroff,asquarewaveissenttosimulatevoltagesbetween0V(off)and5V(on)• Usedtocontrolmotors,fadeLEDs etc

Piezo

const piezo=newfive.Piezo("P1-32");let val = 0; board.loop(200,function(){if (val ^= 1) { //Playnotea4for1/5secondpiezo.frequency(five.Piezo.Notes["a4"],200);

}});

Motors

const leftMotor = new five.Motor({

pins: {pwm: "P1-35", dir: "P1-13"},

invertPWM: true

});

const rightMotor = new five.Motor({

pins: {pwm: "P1-32", dir: "P1-15"},

invertPWM: true

});

leftMotor.forward(150);

rightMotor.forward(150);

SeeJohnny-FivemotorAPIhttp://johnny-five.io/api/motor/

Node-RED

• Anna’sblog:http://crufti.com• http://johnny-five.io/• Node-ARDX(examplesforArduino):http://node-ardx.org

Readmore