south jersey robotics club july 2014 meeting presentation topic –arduino (cont)

31
South Jersey Robotics Club July 2014 Meeting Presentation Topic – Arduino (cont)

Upload: augustine-patterson

Post on 18-Dec-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: South Jersey Robotics Club July 2014 Meeting Presentation Topic –Arduino (cont)

South Jersey Robotics Club

July 2014 Meeting

Presentation Topic –Arduino (cont)

Page 2: South Jersey Robotics Club July 2014 Meeting Presentation Topic –Arduino (cont)

Agenda

• New Member Introductions!• Show and Tell• Maker Fare trip planning• More Arduino (aka, small robot programming)• Next meeting planning…• Lunch!

Page 3: South Jersey Robotics Club July 2014 Meeting Presentation Topic –Arduino (cont)

Introductions

• Please state your:– Name– Interests in this area• Software? Electronics? Mechanical?• Which platforms (Arduino, Pic, NXT, Vex, RasPi, etc.)

– Level of experience– What you want to get out of the club– Quick description of any projects you’re currently

working on or want to start

Page 4: South Jersey Robotics Club July 2014 Meeting Presentation Topic –Arduino (cont)

Show and Tell

• New Raspberry Pi Model B+!

http://www.adafruit.com/products/1914

I’m getting to “play” (well, test) this now:

Page 5: South Jersey Robotics Club July 2014 Meeting Presentation Topic –Arduino (cont)

NYC World Maker Fare 2014

• September 20+21• Do we want to do a group ride?– Don’t have to decide this month, just keep it in

mind / pencil it into your calendar… • (Wait, people still have paper calendars?!?)

Page 6: South Jersey Robotics Club July 2014 Meeting Presentation Topic –Arduino (cont)

Arduino continued…

• First off – getting 1… – Microcenter has the Mega 2560 on sale for

$19.99! (I picked up 2 on Monday).• There seemed to be some confusion about

functions, and objects. So I figured we should work on that…

• Also going to take you guys through the evolution of a project, so you can see how I attack it…

Page 7: South Jersey Robotics Club July 2014 Meeting Presentation Topic –Arduino (cont)

A little about the project…

• So, I built the Makerscanner 3D laser scanner setup.

• Let’s see it in action….

• Problems:– Only does 1 face at a time– Need multiple scans to make a full object– Tedious to manually sweep the laser. Easy to bump the

unit…

Page 8: South Jersey Robotics Club July 2014 Meeting Presentation Topic –Arduino (cont)

A little about the project…

• Ok, so let’s automate this. Robotics to the rescue!

• To do this we need:– A servo to sweep the laser back and forth– A stepper motor to turn the turntable– A button to indicate when we’re ready to move to

the next stage of the scan process– And LCD to show the status to the user– A buzzer to indicate to get the user’s attention

Page 9: South Jersey Robotics Club July 2014 Meeting Presentation Topic –Arduino (cont)

What’s the difference in motor types?

• Servo– Good accuracy– Usually limited rotational range (although there are continuous rotation

servos). – You push a value and it goes to that rotational position. – Relatively low rotational speed.

• DC motor - You give it power, it spins, but no control over how much it spins (just how fast, depending on the PWM). – Need to use encoders to detect how far it’s turned... – Easy to exceed the desired turn amount.

• Stepper – sort of best of both. – Turns a specific amount on each “pulse” (usually in 1/nth of a turn). – Can go fast, but can also be pretty exacting.

Page 10: South Jersey Robotics Club July 2014 Meeting Presentation Topic –Arduino (cont)

First steps….

• Figure out your circuit…– I recommend laying out everything you know you’re

going to need• See if anything requires specific pins• See if there are any conflicts• If there are no conflicts, wire it up and keep track of the

function of each pin• Remember, all grounds MUST tie together somewhere!• Don’t mix motor power (Vmotor) (battery) with the circuitry

power (Vcc) (+5)

• I’ve already wired everything up on the board…

Page 11: South Jersey Robotics Club July 2014 Meeting Presentation Topic –Arduino (cont)

The Stepper Motor

• Gonna cheat with this one – using the Arduino motor shield!

• For the rest, don’t get hung up on the exact pins on the Arduino side – we’re using the Mega so we have TONS of I/O to spare. Just be concerned with not reusing pins, and making sure we use PWM-enabled pins where they’re called for.

Page 12: South Jersey Robotics Club July 2014 Meeting Presentation Topic –Arduino (cont)

Servo Wiring…

Page 13: South Jersey Robotics Club July 2014 Meeting Presentation Topic –Arduino (cont)

The Button

Page 14: South Jersey Robotics Club July 2014 Meeting Presentation Topic –Arduino (cont)

The Speaker

Page 15: South Jersey Robotics Club July 2014 Meeting Presentation Topic –Arduino (cont)

Here’s the LCD…

Page 16: South Jersey Robotics Club July 2014 Meeting Presentation Topic –Arduino (cont)

Building up the software…

• Every one of these has example code in the Arduino Examples folder. So we’ll use these as guidance on how to do each function.

• We’ll start from the Servo example and build up from there..

• Then we’ll build functionality, 1 step at a time.

Page 17: South Jersey Robotics Club July 2014 Meeting Presentation Topic –Arduino (cont)

#include <Servo.h> Servo myservo;

int pos = 0; // variable to store the servo position

void setup() { myservo.attach(10); // attaches the

servo on pin 10 to the servo object

}

void loop() { for(pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees { myservo.write(pos); delay(15) } for(pos = 180; pos>=0; pos-=1) { myservo.write(pos); delay(15); } }

Page 18: South Jersey Robotics Club July 2014 Meeting Presentation Topic –Arduino (cont)

#include <Servo.h> Servo myservo;

int pos = 0; // variable to store the servo position

void setup() { myservo.attach(10); // attaches the

servo on pin 10 to the servo object

}

void loop() { for(pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees { myservo.write(pos); delay(15) } for(pos = 180; pos>=0; pos-=1) { myservo.write(pos); delay(15); } }

Library

Servo Object

Method call on the object

Page 19: South Jersey Robotics Club July 2014 Meeting Presentation Topic –Arduino (cont)

Let’s try it!

• So this should basically take care of sweeping the laser back and forth… But how do we tell the code that we want to wait for a button press when we think the scan is complete?

• We need to add in some button code!• Let’s look at the Button example program…

Page 20: South Jersey Robotics Club July 2014 Meeting Presentation Topic –Arduino (cont)

const int buttonPin = 35; // the number of the pushbutton pin

const int ledPin = 13; // the number of the LED pin

// variables will change:int buttonState = 0; // variable

for reading the pushbutton status

void setup() { // initialize the LED pin as an

output: pinMode(ledPin, OUTPUT); // initialize the pushbutton pin as

an input: pinMode(buttonPin, INPUT);}

void loop() { // read the state of the pushbutton

value: buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed. // if it is, the buttonState is HIGH: if (buttonState == HIGH) { // turn LED on: digitalWrite(ledPin, HIGH); } else { // turn LED off: digitalWrite(ledPin, LOW); }}

Page 21: South Jersey Robotics Club July 2014 Meeting Presentation Topic –Arduino (cont)

#include <Servo.h> const int SERVOPIN = 10;const int BUTTONPIN = 35; // define our global variables.... Servo myservo; // create servo object to control a

servo int pos = 0; // variable to store the servo position boolean pressed = FALSE; // variable to store if the

button was pressed

void setup() { pinMode(BUTTONPIN, INPUT); myservo.attach(SERVOPIN); // attaches the servo on

pin 10 to the servo object Serial.begin(9600);} void checkButton() { if(!pressed && digitalRead(BUTTONPIN)) { pressed=TRUE; Serial.println("Button pressed!"); }}

void loop() { for(pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees { // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position checkButton(); } for(pos = 180; pos>=0; pos-=1) // goes from 180 degrees to 0 degrees { myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position checkButton(); } // check to see if the button was pressed... // if not, then sweep back and forth again if(pressed) { pressed = FALSE; // now do something, but what??? Serial.println("A button had been pressed!"); } }

Page 22: South Jersey Robotics Club July 2014 Meeting Presentation Topic –Arduino (cont)

Let’s try it!

• Ok, so you’re probably wondering why I checked the previous status of the pressed variable?– Well, what if I press the button and then release

it? The first check will see the press, but then the 2nd would say “It’s not pressed”, which would defeat the purpose. So instead, we’ll reset the variable once we get down to doing something with the button.

Page 23: South Jersey Robotics Club July 2014 Meeting Presentation Topic –Arduino (cont)

Let’s try it!

• Debouncing…– Yeah, I didn’t really do that here…

• What does Debouncing mean?• Checking the button pin twice quickly to make sure it really was

pressed (and not just registering pushed because of noise).

• Ok, so now we need to actually DO something because we pressed the button!– Let’s make the turntable rotate, and then wait until the user

presses the button again to indicate that they’re ready to capture again.

• First, let’s look at the Stepper example…

Page 24: South Jersey Robotics Club July 2014 Meeting Presentation Topic –Arduino (cont)

#include <Stepper.h>

// Map our pins to constants to make things easier to keep track of

const int pwmA = 3;const int pwmB = 11;const int brakeA = 9;const int brakeB = 8;const int dirA = 12;const int dirB = 13;

// The amount of steps for a full revolution of your motor.

// 360 / stepAngleconst int STEPS = 800;

// Initialize the Stepper classStepper myStepper(STEPS, dirA, dirB);

void setup() { // Set the RPM of the motor myStepper.setSpeed(30);

// Turn on pulse width modulation pinMode(pwmA, OUTPUT); digitalWrite(pwmA, HIGH); pinMode(pwmB, OUTPUT); digitalWrite(pwmB, HIGH);

// Turn off the brakes pinMode(brakeA, OUTPUT); digitalWrite(brakeA, LOW); pinMode(brakeB, OUTPUT); digitalWrite(brakeB, LOW);} // end setup()

void loop() { // Move the motor X amount of steps myStepper.step(STEPS); // Pause delay(2000);}

Page 25: South Jersey Robotics Club July 2014 Meeting Presentation Topic –Arduino (cont)

#include <Servo.h>#include <Stepper.h>

// Sets up all our pins// {Snipped to save space…. }// The amount of steps for a full revolution of your motor.// 360 / stepAngleconst int STEPS = 800;// define our global variables....Servo myservo; // create servo object to control a servoint pos = 0; // variable to store the servo positionboolean pressed = false; // variable to store if the button was pressed// Initialize the Stepper classStepper myStepper(STEPS, dirA, dirB);

void setup(){ pinMode(BUTTONPIN, INPUT); myservo.attach(SERVOPIN); // attaches the servo on pin 10 to the

servo object // Set the RPM of the motor myStepper.setSpeed(30); // Turn on pulse width modulation pinMode(pwmA, OUTPUT); digitalWrite(pwmA, HIGH); pinMode(pwmB, OUTPUT); digitalWrite(pwmB, HIGH); pinMode(brakeA, OUTPUT); digitalWrite(brakeA, LOW); pinMode(brakeB, OUTPUT); digitalWrite(brakeB, LOW); Serial.begin(9600);}

// Handle sweeping the servo back and forth.void sweep() { for (pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees { // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position checkButton(); } for (pos = 180; pos >= 0; pos -= 1) // goes from 180 degrees to 0 degrees { myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position checkButton(); }}

// Handle checking to see if the button was pressed.void checkButton() { if (!pressed && digitalRead(BUTTONPIN)) { pressed = true; Serial.println("Button pressed!"); }}

Page 26: South Jersey Robotics Club July 2014 Meeting Presentation Topic –Arduino (cont)

void loop(){ myservo.write(90); // set the laser to the calibrate

position Serial.println("Press button to start calibration"); while(!pressed) { checkButton(); } // if we get here the button was pressed. pressed = false; delay(2000); Serial.println("Press button when calibration done"); while(!pressed) { checkButton(); } pressed=false; delay(2000); Serial.println("Sweeping!"); // if we get here the button was pressed // start scanning! and do it until we detect a press while(!pressed) { sweep(); }

// ok the button was pressed, so stop sweeping

pressed=false; delay(2000); Serial.println("Save the file, then press

button to advance turntable"); while(!pressed) { checkButton(); } // ok pressed, advance the stepper moter pressed=false; Serial.println("Turning table..."); myStepper.step(STEPS); // 10 degrees on

the stepper, not nec the turntable. // Ok ,reset and do it all over again! Serial.println("Done!"); }

Page 27: South Jersey Robotics Club July 2014 Meeting Presentation Topic –Arduino (cont)

So what’s this doing?

• Prompt the user that we’re resetting for next scan.

• Setting laser to 90 degrees to calibrate, and waiting for button press to start scanning

• Performing the sweep back and forth repeatedly until the button is pressed again.

• Stop sweeping, rotate the turntable 10 degrees, and then start all over again!

Page 28: South Jersey Robotics Club July 2014 Meeting Presentation Topic –Arduino (cont)

Let’s add in the LCD and speaker…• So, what if we wanted to put the prompt onto a LCD instead of the

serial port?#include <LiquidCrystal.h>

LiquidCrystal lcd(43, 41, 51, 49, 47, 45); //set the proper pin #’s you used here…

void setup() { // set up the LCD's number of columns and rows: lcd.begin(16, 2); ….

• Then everywhere we had a Serial.println(), replace it with thislcd.print( text );

• For the Speaker, just add a line like this:tone(33, 200, 1000); // (pin#, frequency, duration)

Page 29: South Jersey Robotics Club July 2014 Meeting Presentation Topic –Arduino (cont)

So, let’s try the finished product!

Page 30: South Jersey Robotics Club July 2014 Meeting Presentation Topic –Arduino (cont)

Questions???• Build up your functionality a little at a time

– Get something working, then add in the next piece. – Save backup copies!

• Remember, loop() will get executed over and over until power is removed. Plan for this in your code!

• Writing out to Serial Console is a great debugging tool– Remember that it slows down your program, and timing can be important!

• Whenever you do something more than 1-2 times in your code, consider moving it into a function – Try to keep setup() and loop() short and simple to read!!!!– Makes the code smaller overall… Smaller=easier!

• LOTS of good libraries out there!!! – Use them instead of reinventing the wheel.

• The Examples are your friend! Make use of them!

Page 31: South Jersey Robotics Club July 2014 Meeting Presentation Topic –Arduino (cont)

Next meeting

• What’s a good date? August 9? 16?• Location?• Topic?– Some Ideas:

• Basic Electronics/ Circuits• Mini Sumo• IMUs + GPS• Filtering (Particle / Extended

Kahlman)• Encoders• Raspberry Pi primer• How to use an O-Scope / Logic

Analyzer• How to use EagleCAD• How to Solder

• OpenCV for beginners• Intro to ROS (Robot Operating

System)• Maze Solving• Obstacle detection and

avoidance/tracking• Web based robot control interface• Line following / line sensors / PID

control