23
Robotycs - YLab Robotics Group Toy Hacking Workshop Part 2– November 11, 2015

Robotycs -YLab Robotics Group Toy Hacking WorkshopPart 2 ...files.meetup.com/18566428/Toy-Hacking-RobotycsYLab... · power to directly drive a servo or motor •A custom “Library”

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Robotycs -YLab Robotics Group Toy Hacking WorkshopPart 2 ...files.meetup.com/18566428/Toy-Hacking-RobotycsYLab... · power to directly drive a servo or motor •A custom “Library”

Robotycs - YLab Robotics GroupToy Hacking Workshop Part 2– November 11, 2015

Page 2: Robotycs -YLab Robotics Group Toy Hacking WorkshopPart 2 ...files.meetup.com/18566428/Toy-Hacking-RobotycsYLab... · power to directly drive a servo or motor •A custom “Library”

Robot Talk – Ross LunanWhat do we build Robots with?

• With whatever you can find in your “junk-box”, get from lots of suppliers

• Typical Controllers/OS• Arduino (C++), Raspberry Pi (Linux/Python), Beaglebone (Linux),• Linux, Python, Scratch, Jscript, ROS, etc.

• Chassis, Sensors, Motors, LEDs, wireless devices, wires

• Patience and perseverance !

• While “Robotic” devices can take many forms, follows is an example of a basic vehicle hardware

2

Page 3: Robotycs -YLab Robotics Group Toy Hacking WorkshopPart 2 ...files.meetup.com/18566428/Toy-Hacking-RobotycsYLab... · power to directly drive a servo or motor •A custom “Library”

Sample Basic Robot Vehicle Hardware(Follows from J. Lau & P Tan “How to Build a robot in 10 Minutes)

Hardware:• Chassis (anything that will hold the robot)• Motor and Wheel• Microcontroller

• Arduino(Uno, Leonardo, Filo…), Raspberry Pi, …• Motor Driver (H-bridge)

• L298N Motor Shield• Battery holder and battery

$26 $16 $6/pair$6/pair

$13 (w/NiMh batteries

$26/kit$36/kit

Page 4: Robotycs -YLab Robotics Group Toy Hacking WorkshopPart 2 ...files.meetup.com/18566428/Toy-Hacking-RobotycsYLab... · power to directly drive a servo or motor •A custom “Library”

What’s an Arduino ? https://www.raspberrypi.org/

• Arduino is an open-source physical computing platform based on a simple i/o board and a development environment that implements the standard language called Processing/Wiring.

• Arduino can be used to develop stand-alone interactive objects (i.e. robot devices) or can be connected to software on your computer (e.g. Flash, Processing, MaxMSP). The open-source IDE can be downloaded for free (for Mac OS X, Windows, and Linux).

• It can be powered from the computer USB cable (usually during code development & testing) or battery pack (7-12 Vdc)

Page 5: Robotycs -YLab Robotics Group Toy Hacking WorkshopPart 2 ...files.meetup.com/18566428/Toy-Hacking-RobotycsYLab... · power to directly drive a servo or motor •A custom “Library”

What’s an Arduino ? … Cont’d

• Interfaces ( 3.3 or 5v) can include 20 Digital Input or Output Pins (I/O), 7 Pulse Width Modulation (PWM) Channels (to control LED brightness or Motor Speeds), 12 Analog-to-Digital Converters to measure analog voltages from Sensors.

• Interface pins on standard models are arranged to enable attaching additional boards on top. These are called “Shields” and there are 100’s available to many useful functions.

• Many physical variations, smaller and larger are available to customize the application

• Communication to other devices to computers can be done by attaching external Bluetooth or WiFi devices.

Page 6: Robotycs -YLab Robotics Group Toy Hacking WorkshopPart 2 ...files.meetup.com/18566428/Toy-Hacking-RobotycsYLab... · power to directly drive a servo or motor •A custom “Library”

What’s a Motor Shield ?

• A Motor and Servo Shield is an Arduino (R3) compatible shield (hardware circuit board and components) designed to make connecting motors, servos and sensors to your Arduino-based development board as fast and easy as possible. A separate power connection for the servo or motor allows customization to the optimum device specifications.

• The external shield is mandatory as the basic Arduino I/O does not provide sufficient power to directly drive a servo or motor

• A custom “Library” code file is available that increases the Arduino basic coding language specific to this hardware.

• There is a wide choice of Motor and Servo Shields depending on the end device requirements, available a completely built or kits requiring soldering.

Page 7: Robotycs -YLab Robotics Group Toy Hacking WorkshopPart 2 ...files.meetup.com/18566428/Toy-Hacking-RobotycsYLab... · power to directly drive a servo or motor •A custom “Library”

What is a Chassis Base?• As in any vehicle a frame is required to hold all the mechanical components, including

wheels, motors, control devices.• For a conventional flat surface vehicle, choices are usually 2, 4, 6 motor/wheel

assemblies• However there are 100’s of possible variations including flying, swimming, animal or

insectoid like devices.

Page 8: Robotycs -YLab Robotics Group Toy Hacking WorkshopPart 2 ...files.meetup.com/18566428/Toy-Hacking-RobotycsYLab... · power to directly drive a servo or motor •A custom “Library”

Arduino Controller Configuration

pwm_a = 3

pwm_b = 11

dir_b = 13 dir_a = 12

//Set control pins to be outputs

pinMode(pwm_a, OUTPUT); pinMode(pwm_b, OUTPUT);pinMode(dir_a, OUTPUT);pinMode(dir_b, OUTPUT);

DC Power (8-12 Vdc

Pin Numbers in the program

USB Serial Port for uploadingcode and Serial Monitor

BluetoothTx = 9 bluetoothRx = 10

Page 9: Robotycs -YLab Robotics Group Toy Hacking WorkshopPart 2 ...files.meetup.com/18566428/Toy-Hacking-RobotycsYLab... · power to directly drive a servo or motor •A custom “Library”

What is Digital I/O ? (https://www.arduino.cc/en/Tutorial/DigitalPins)

• The “D” or “A” pins on the Arduino can be configured as either inputs or outputs.

• Input: Arduino (Atmega) pins default to inputs, so they don't need to be explicitly declared as inputs with pinMode() when you're using them as inputs. Pins configured this way are said to be in a high-impedance state.

• Output: Pins configured as OUTPUT with pinMode() are said to be in a low-impedance state. This means that they can provide a substantial amount of current to other circuits. • Atmega pins can source (provide positive current)

or sink (provide negative current) up to 40 mA (milliamps) of current to other devices/circuits. This is enough current to brightly light up an LED (don't forget the series resistor), or run many sensors, for example, but not enough current to run most relays, solenoids, or motors.

*/ blink.ino /*

int ledPin = 13; // LED connected to digital pin 13

void setup(){

pinMode(ledPin, OUTPUT);// sets the digital pin as output

}

void loop(){

digitalWrite(ledPin, HIGH); // sets the LED ondelay(1000); // waits for a

seconddigitalWrite(ledPin, LOW); // sets the LED offdelay(1000); // waits for a

second}

Page 10: Robotycs -YLab Robotics Group Toy Hacking WorkshopPart 2 ...files.meetup.com/18566428/Toy-Hacking-RobotycsYLab... · power to directly drive a servo or motor •A custom “Library”

What is PWM I/O ?

• Pulse Width Modulation, or PWM, is a technique for getting analog results with digital means. Digital control is used to create a square wave, a signal switched between on and off. This

• on-off pattern can simulate voltages in between full on (5 Volts) and off (0 Volts) by changing the portion of the time the signal spends on versus the time that the signal spends off. The duration of "on time" is called the pulse width.

• To get varying analog values, you change, or modulate, that pulse width. If you repeat this on-off pattern fast enough with an LED for example, the result is as if the signal is a steady voltage between 0 and 5v controlling the brightness of the LED.

• In the graphic below, the green lines represent a regular time period. This duration or period is the inverse of the PWM frequency. In other words, with Arduino's PWM frequency at about 500Hz, the green lines would measure 2 milliseconds each. A call to analogWrite() is on a scale of 0 - 255, such that analogWrite(255) requests a 100% duty cycle (always on), and analogWrite(127) is a 50% duty cycle (on half the time) for example.

(https://www.arduino.cc/en/Tutorial/PWM)

Page 11: Robotycs -YLab Robotics Group Toy Hacking WorkshopPart 2 ...files.meetup.com/18566428/Toy-Hacking-RobotycsYLab... · power to directly drive a servo or motor •A custom “Library”

Motor Shield Configuration

pwm_a = 3

pwm_b = 11

dir_b = 13 dir_a = 12

//Set motor stop, 0% duty cycleanalogWrite(pwm_a, 0);//Set motor direction cw, 1a=2b=high, 1b=2a=low

digitalWrite(dir_a, HIGH); //Set motor direction ccw, 1b=2a=high, 1a=2b=low

digitalWrite(dir_b, LOW);

2a

1a

2b

1b

A

Pwm switch

Page 12: Robotycs -YLab Robotics Group Toy Hacking WorkshopPart 2 ...files.meetup.com/18566428/Toy-Hacking-RobotycsYLab... · power to directly drive a servo or motor •A custom “Library”

/* usb_tether_remote_test_motors.ino* For UNO, NOT DUE or Leonardo* Version of remote_test_motors w/USB tether connected to H/W Serial Rx-In D0/Tx-Out D1* Hardware Serial Monitor port (Serial command).* Added code to control direction from characters received from* Bluetooth radio connected to SoftwareSerial library serial Arduino Rx-In D10/Tx-Out D11* Android Client: hobbyprojects.com forward="A", backward="B", turn left=C,* turn right="D", stop="E", 360 deg CW="F", 360 deg CCW="G"* http://www.hobbyprojects.com/bluetooth-device-control/* Disregard the controller implementation* Anduino code from Instructables.com Android-bot-remote-control* http://www.instructables.com/id/Arduino-bot-Android-remote-control/* revised move functions to add "turn" CW and CCW, input speed for future* Based on test_motors.ino* Canada Robotics Motor Shield** *** Open Arduino IDE Serial monitor set to 9600 b/s ****/

#include <SoftwareSerial.h> // Library to enable serial tx/rx on any pinint bluetoothTx = 9; // Arduino Rx-In from Tx-O data Out pin of

Bluetooth moduleint bluetoothRx = 10; // Arduino Tx-Out to Rx-I data In pin of

Bluetooth module// Note the Arduino Rx/Tx Pins are crossed over with Bluetooth Tx/Rx

pins

SoftwareSerial bluetooth( bluetoothTx, bluetoothRx); // connected to Arduino Rx-In, Tx-Out D pin

// https://www.arduino.cc/en/Tutorial/SoftwareSerialExample// set pin #const int pwm_a = 3;const int pwm_b = 11;const int dir_a = 12;const int dir_b = 13;const int speedbot = 255;char incomingByte = 0; // incoming byte from Serial Terminal or

Bluetooth link// set pin modes

usb_tether_remote_test_motors.ino - Software

Page 13: Robotycs -YLab Robotics Group Toy Hacking WorkshopPart 2 ...files.meetup.com/18566428/Toy-Hacking-RobotycsYLab... · power to directly drive a servo or motor •A custom “Library”

void setup() {//Set control pins to be outputspinMode( pwm_a, OUTPUT); // Right Motor speedpinMode( pwm_b, OUTPUT); // Left Motor speedpinMode( dir_a, OUTPUT); // Right Motor direction Fwd or BackpinMode( dir_b, OUTPUT); // Left Motor direction Fwd or Back//initial set up straight forward, no speeddigitalWrite( dir_a, HIGH); //Reverse motor direction, 1 high, 2

lowdigitalWrite( dir_b, HIGH); //Reverse motor direction, 3 low, 4

highanalogWrite( pwm_a, 0); //set both motors to not moveanalogWrite( pwm_b, 0);Serial. begin(9600); // Begin serial monitor and keyboard at 9600

b/sbluetooth. begin(9600); // Begin Software Serial data interface at

9600 b/s} // setup()

# void loop() {/* Comment out test-motors commands ***********************

// see if there's incoming serial data on usb tether or bluetooth link:int serialByteReady = Serial. available() > 0;int bluetoothByteReady = bluetooth. available() > 0;

if ( serialByteReady || bluetoothByteReady) {// read the oldest byte in the serial buffer of either serial port

if ( serialByteReady) { incomingByte = Serial. read();}else if ( bluetoothByteReady) { incomingByte = bluetooth. read();}

// action depending on the instruction// as well as sending a confirmation back to the appi

usb_tether_remote_test_motors.ino – Software Page 2

Page 14: Robotycs -YLab Robotics Group Toy Hacking WorkshopPart 2 ...files.meetup.com/18566428/Toy-Hacking-RobotycsYLab... · power to directly drive a servo or motor •A custom “Library”

switch (incomingByte) {case 'A':

forward(speedbot);Serial.println("Going forward");break;

case 'C':turn(speedbot, true);Serial.println("Turning left");break;

Case 'D':turn(speedbot, false);Serial.println("Turning right");break;

case 'B':backward(speedbot);Serial.println("Going backwards");break;

case 'E':stopped();Serial.println("Stopping");

-

break;case 'F':

spin(speedbot, true); // CWSerial.println("Spinning CW");break;

case 'G':spin(speedbot, false); // CCWSerial.println("Spinning CCW");break;

default:// if nothing matches, do nothingbreak;} //case

} // if} //loop()

usb_tether_remote_test_motors.ino – Software Page 3

Page 15: Robotycs -YLab Robotics Group Toy Hacking WorkshopPart 2 ...files.meetup.com/18566428/Toy-Hacking-RobotycsYLab... · power to directly drive a servo or motor •A custom “Library”

void forw() { // no pwm defineddigitalWrite(dir_a, HIGH); //Reverse motor direction, 1 high, 2 lowdigitalWrite(dir_b, HIGH); //Reverse motor direction, 3 low, 4 high

}

void back() { // no pwm defineddigitalWrite(dir_a, LOW); //Set motor direction, 1 low, 2 highdigitalWrite(dir_b, LOW); //Set motor direction, 3 high, 4 low

}

void forward() { //full speed forwarddigitalWrite(dir_a, HIGH); //Reverse motor direction, 1 high, 2 lowdigitalWrite(dir_b, HIGH); //Reverse motor direction, 3 low, 4 high analogWrite(pwm_a, 255); //set both motors to run at (100/255 = 39)% duty cycleanalogWrite(pwm_b, 255);

}

void backward() { //full speed backwarddigitalWrite(dir_a, LOW); //Set motor direction, 1 low, 2 highdigitalWrite(dir_b, LOW); //Set motor direction, 3 high, 4 lowanalogWrite(pwm_a, 255); //set both motors to run at 100% duty cycle (fast)analogWrite(pwm_b, 255);

}

void stopped() { //stopdigitalWrite(dir_a, LOW); //Set motor direction, 1 low, 2 highdigitalWrite(dir_b, LOW); //Set motor direction, 3 high, 4 lowanalogWrite(pwm_a, 0); //set both motors to run at 100% duty cycle (fast)analogWrite(pwm_b, 0);

}

Build a robot in 10 minutes - Software

Page 16: Robotycs -YLab Robotics Group Toy Hacking WorkshopPart 2 ...files.meetup.com/18566428/Toy-Hacking-RobotycsYLab... · power to directly drive a servo or motor •A custom “Library”

void fadein() {// fade in from min to max in increments of 5 points:for(int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {

// sets the value (range from 0 to 255):analogWrite(pwm_a, fadeValue); analogWrite(pwm_b, fadeValue); // wait for 30 milliseconds to see the dimming effect delay(30);

} }

void fadeout() { // fade out from max to min in increments of 5 points:for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {

// sets the value (range from 0 to 255):analogWrite(pwm_a, fadeValue);analogWrite(pwm_b, fadeValue);// wait for 30 milliseconds to see the dimming effect delay(30);

}}

void astop() { //stop motor AanalogWrite(pwm_a, 0); //set both motors to run at 100% duty cycle (fast)

}

void bstop() { //stop motor BanalogWrite(pwm_b, 0); //set both motors to run at 100% duty cycle (fast)

}

Build a robot in 10 minutes - Software

Page 17: Robotycs -YLab Robotics Group Toy Hacking WorkshopPart 2 ...files.meetup.com/18566428/Toy-Hacking-RobotycsYLab... · power to directly drive a servo or motor •A custom “Library”

USB Tether – Serial Monitor - Control

• The Serial Monitor is a separate pop-up window that acts as a separate terminal that communicates by receiving and sending Serial Data. With USB cable connected beteenArduino and PC, open a separate window with Tools-Serial Monitor, set bit-rate.

• Serial Data is sent over a single wire (but usually travels over USB in our case) and consists of a series of 1's and 0's sent over the wire. Data can be sent in both directions (In our case on two wires).

void setup() /****** SETUP: RUNS ONCE ******/{Serial.begin(9600); Serial.println("--- Start Serial Monitor SEND_RCVE ---");

Serial.println(" Type in Box above, . ");Serial.println("(Decimal)(Hex)(Character)"); Serial.println();

}//--(end setup )---

Page 18: Robotycs -YLab Robotics Group Toy Hacking WorkshopPart 2 ...files.meetup.com/18566428/Toy-Hacking-RobotycsYLab... · power to directly drive a servo or motor •A custom “Library”

New Arduino Functions• Comments: */ This is a comment */ // So is this• #include SoftwareSerial.h, SoftwareSerial name(Rx-In pin, Tx-Out pin): Calls an

included Library to set up a two-way serial interface on selected digital pins.• Serial.begin(9600): Starts the Serial Terminal link at 9600 b/s• Name.begin(9600); Start the SoftwareSerial link at 9600 b/s• int serialByteReady=serial.available()>0; define a boolean variable and checks if a byte

is in the receiver buffer• if( ) {do this if true} else if () {do this if true} Check statements and direcvt action• Switch ( a byte ) {

case ‘A’;do this and this;break ; skip all other case statementsdefault: // if nothing matches, do nothingbreak;

Page 19: Robotycs -YLab Robotics Group Toy Hacking WorkshopPart 2 ...files.meetup.com/18566428/Toy-Hacking-RobotycsYLab... · power to directly drive a servo or motor •A custom “Library”

Next Step – Bluetooth Remote Control• Transmits characters from a Bluetooth device,

i.e.Andoid running an APP to the Arduino• Need to connect a Bluetooth module to your

Arduino – just 4 wires, +3.3 Vdc, Gnd, Tx-Out to Arduino Rx-In, Rx-In to Tx-Out. Suggested is Wireless Module HC-05

• It operates at 9600 bit/s identical to the USB tether.

• No change to the code is required

Download from Google Play:Bluetooth Robot Remote Control byHobbyproject.com

Page 20: Robotycs -YLab Robotics Group Toy Hacking WorkshopPart 2 ...files.meetup.com/18566428/Toy-Hacking-RobotycsYLab... · power to directly drive a servo or motor •A custom “Library”

Information – Googling and…..

YLab: www.ylab.ca , www.meetup.com/Markham-Makers-Group/

Supplers: www.arduino.cc, www.raspberrypi.org, www.beaglebone.org, www.picaxe.comwww.ros.org, www.letsmakerobots.com, www.adafruit.com, www.pololu.com , www.mimetics.com(Jade Robot) , www.trossenrobotics.com, www.sparkfun.com , www.xyrobotics.com , www.makershed.com , www.oreilly.com (Bookstore), lotsa vendors…..

Organizations: IEEE Robotics Society: http://sites.ieee.org/scv-ras/calendar/, www.logicsacademy.com , www.hbrobotics.org, www.seattlerobotics.org, www.hbrobotics.org, http://wiki.hbrobotics.org , www.clearpath.comMagazines: Servo www.servomagazine.com , Robot www.botmag.com, Nuts and Volts www.nutsvolts.com , www.makezine.com ,

Competitions: www.battlebots.com , First Robotics: www.firstroboticscanada.org/ , Underwater robotics: MATE: www.marinetech.org/international-competition/

20

Page 21: Robotycs -YLab Robotics Group Toy Hacking WorkshopPart 2 ...files.meetup.com/18566428/Toy-Hacking-RobotycsYLab... · power to directly drive a servo or motor •A custom “Library”

Upcoming YLab events

• Joint YLab/York Region Amateur Radio Club: Wed., Nov. 18, 2105 at David Dunlap Observatory (Richmond Hill)

• YLab Robotycs: Wed., Nov. 25, 2015, 7-9 pm Location TBD

Page 22: Robotycs -YLab Robotics Group Toy Hacking WorkshopPart 2 ...files.meetup.com/18566428/Toy-Hacking-RobotycsYLab... · power to directly drive a servo or motor •A custom “Library”

Other local upcoming events

• Markham Library: 3D design & Printed Workshops Nov 8, 19, 25, Dec 2, 13 2015. Register at https://www.eventbrite.ca/e/introduction-to-3d-printing-tickets-19020978258

• Markham Cave: MPL’s Media Makespace: thecavemarkham.blogspot.ca

Page 23: Robotycs -YLab Robotics Group Toy Hacking WorkshopPart 2 ...files.meetup.com/18566428/Toy-Hacking-RobotycsYLab... · power to directly drive a servo or motor •A custom “Library”

UNTIL NEXT MONTH – NOVEMBER 25, 2015Location : TBD, See www.ylab.ca for Robot Talk topic and Activities