Intro to Robotics
Using the Raspberry Pi and Arduino for robotics applications.
Week One Overview • • •
• • • •
Review Robot Base platform kit Breakdown of parts and function Assembling the robot base and connecting Arduino. Making the robot move Connecting Ultrasonic Sensor Control speed using PWM Driving the robot using Skid Steering
SainSmart Mega 2560 R3 4WD Mobile Car Robot Kit For Arduino
Parts included in kit • • • •
•
SainSmart 4WD Drive Aluminum Mobile Car Robot Platform SainSmart Mega 2560 R3 SainSmart Sensor Shield Module V5 SainSmart L298N Dual H Bridge DC Motor Driver SainSmart Ultrasonic Module HC-SR04 Distance Sensor
SainSmart 4WD Drive Aluminum Mobile Car Robot Platform
• • •
• • •
Can hold many controllers, drivers, sensors and RF modules etc. Support plate with controller mounting holes, 2 dof pan. Tilt holds down groove, collision switch mounting hole, can be added with sensors. Elastic rubber tires. Damping, gripping, and wear-resisting. Light weight, high strength and no deformation characteristics. Unique design and color.
SainSmart Mega 2560 R3
SainSmart Mega 2560 R3 Microcontroller
ATmega2560
Operating Voltage
5v
Input Voltage (recommended)
7-12v
Input Voltage (limits)
6-20
Digital I/O Pins
54 (15 provide PWM output)
Analog Input Pins
16
DC Current per I/O Pin
40mA
DC Current for 3.3v Pin
50mA
Flash Memory
256 KB (8KB for bootloader)
SRAM
8KB
EEPROM
4KB
Clock Speed
16MHz
SainSmart Sensor Shield Version 5
• • • •
Features : It can connect to various modules like sensors, servos, relays, buttons, potentiometers Each functional module has buckled port with VCC, GND and Output, which has corresponding port on the Sensor Shield, connected with a plain 2.54mm dualfemale cable ready for connection Access to 14 Digital I/O and 6 Analog I/O and power pin provided for each one Connections for LCD Display, I2C and Serial Communications ports
SainSmart Sensor Shield Version 5 The sensor shield is used to make it easy to connect devices to the Arduino while also giving easy access to Ground, Voltage, and Signal pins (GVS) for each device
Special Notes • •
Uses separate power source for digital connection Uses Arduino 5v pin for analog connections to avoid unwanted noise caused by 2nd power supply
SainSmart L298N Dual H Bridge DC Motor Driver
• • • • •
Driver: L298N Dual H Bridge DC Motor Driver IC Drive Part of the Peak Current: 2A Logic Part of the Operating Current: 0 ~ 36mA Control Signal Input Voltage Range: Low:-0.3V ? Vin ? 1.5V; High: 2.3V ? Vin ? Vss Maximum Power Consumption: 20W ( at 75 deg C)
Sample Code For L298N Dual H Bridge (One channel) #define OUT1PIN1 2 #define OUT1PIN2 4 #define ENABLE1 3 #define OUT2PIN1 7 #define OUT2PIN2 8 #define ENABLE2 6
// H-bridge OUT1.1 // H-bridge OUT1.2 // H-bridge Ena 1 // H-bridge OUT2.1 // H-bridge OUT2.2 // H-bridge Ena 2
void setup() { // setup motor pins pinMode(OUT1PIN1, OUTPUT); pinMode(OUT1PIN2, OUTPUT); pinMode(ENABLE1, OUTPUT); digitalWrite(ENABLE1, HIGH); // set the enables to high
} void loop() { digitalWrite(OUT1PIN1, LOW); // set leg 1 of the H-bridge low digitalWrite(OUT1PIN2, HIGH); // set leg 2 of the H-bridge high delay(5000); digitalWrite(OUT1PIN1, HIGH); // set leg 1 of the H-bridge high digitalWrite(OUT1PIN2, LOW); // set leg 2 of the H-bridge low
delay(5000); }
SainSmart Ultrasonic Module HCSR04 Distance Sensor
• • • • •
power supply :5V DC quiescent current : <2mA effectual angle: <15° ranging distance : 2cm – 500 cm resolution : 0.3 cm
• • •
There are 4 pins on the module : VCC , Trig, Echo, GND . So it's a very easy interface for controller to use for ranging. The process is : pull the Trig pin to high level for more than 10us impulse , the module starts ranging. If it finds an object in front , Echo pin will be high level , and based on the distance it will set the duration of high level so we can calculate the distance easily :
Distance = ((Duration of high level)*(Sonic :340m/s))/2
HC-SR04 Sample Arduino Code
/ --------------------------------------------------------------------------// Example NewPing library sketch that does a ping about 20 times per second. // ---------------------------------------------------------------------------
#include #define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor. #define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor. #define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance. void setup() {
Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results. } void loop() { delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings. unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS). Serial.print("Ping: "); Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance and print result (0 = outside set distance range, no ping echo) Serial.println("cm"); }
Connecting HC-SR04 to Arduino (According to sample code above)
Pulse Width Modulation (PWM)
• • • •
•
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 a motor for example, the result is as if the signal is a steady voltage between 0 and 5v controlling the speed of the motor.
SKID Steering
• By using Skid steering we can make a robot with fixed wheels turn in different directions • This is very similar to how you would steer an army tank.
Next Week
• • •
Adding Pololu QTR Reflectance Sensors to the Robot Base Controlling the robotic arm with Arduino Connecting to Arduino with Raspberry Pi to control the robot