Mostrando entradas con la etiqueta Cheapest robot (Arduino). Mostrar todas las entradas
Mostrando entradas con la etiqueta Cheapest robot (Arduino). Mostrar todas las entradas

domingo, 23 de junio de 2013

Arduino C++ crash examples tutorial using Duemilanove (I)

This is the first example in a serie of cheap (absolutely free and cheap) articles to get some basic and practical C++ knowledge. It will references the great explanations provided at www.cplusplus.com C++ tutorial. Using Arduino Duemilanove (well, really it's a Funduino).

Here you can download The file with the code, and you can find links to a lot of free tutorials, courses and books to learn C++ here.


[sourcecode language="cpp"]
// C++ crash tutorial with Arduino Duemilanove.

// First example: http://softwaresouls.com/softwaresouls/2013/06/23/c-crash-tutorial-using-robotis-cm-900-board-and-ide-i/

/*
Hello World shows messages on construction and destruction
Also it lets you to salute.
Showing always its assigned identifiction number MyId
*/
class HelloWorld
{
private:
int myId; //Object identification num ber

public:

// class constructor http://www.cplusplus.com/doc/tutorial/classes/
HelloWorld(char *message, byte id)
{
myId=id;
Serial.print(message);
printId();
}

// class destructor http://www.cplusplus.com/doc/tutorial/classes/
~HelloWorld()
{
Serial.print ("Destructing object: ");
printId();
}

void printId()
{
Serial.print(" ID:");
Serial.println(myId);
Serial.println(" ");
}

void Salute(char *name)
{
Serial.print("Hi!, ");
Serial.println(name);
Serial.print("Regards from object: ");
printId();
}
};

/*
void setup() function it's only executed one time at the start of the execution.
It is called from a hidden main() function in the Ronbotis CM-900 IDE core.

\ROBOTIS-v0.9.9\hardware\robotis\cores\robotis\main.cpp
Note: "while (1)" is a forevr loop ( http://www.cplusplus.com/doc/tutorial/control ):

(Basic structure http://www.cplusplus.com/doc/tutorial/program_structure/)

int main(void) {
setup();

while (1) {
loop();
}
return 0;
}
*/
void setup()
{
Serial.begin(57600); //initialize serial USB connection
delay(3000); //We will wait 3 seconds, let the user open (Control+Shift+M) the Monitor serial console
}

//We will not see neither the construction nor the destruction of this global object because serial port it's not still initiated
HelloWorld hw0("construction object", 0); //Object construction http://www.cplusplus.com/doc/tutorial/classes/

// A counter to see progress and launch events
int iterationCounter=0; //An integer variable to count iterations http://www.cplusplus.com/doc/tutorial/variables

// void loop() will be executing the sentences it contains while CM-900 is on.
void loop()
{
// Lets's show only the first 5 iterations http://www.cplusplus.com/doc/tutorial/control/
if (iterationCounter<5)
{
Serial.print("starting iteration #");
Serial.println(++iterationCounter); // firts, iterationCounter is incremented and then printed

// We will see the consttructiona and destruction messages from this local (inside the loop function) object. Object construction http://www.cplusplus.com/doc/tutorial/classes/
HelloWorld hw1("constructing object", 1);
hw1.Salute("Joe");

if (iterationCounter==3)
{
// We will see the consttruction and destruction messages from this local (inside the "if" block inside the "loop" function) object. Objet construction http://www.cplusplus.com/doc/tutorial/classes/
HelloWorld hw2("constructing object", 2);
hw2.Salute("Jones");
} // Objet hw2 destruction http://www.cplusplus.com/doc/tutorial/classes/

//Let's show that object hw0 is alive
hw0.Salute("Pepe");

Serial.print("ending iteration #");
Serial.println(iterationCounter++); // first cpunter is printed, then incremented.
} // Objet hw1 destruction http://www.cplusplus.com/doc/tutorial/classes/
} // Program end. Objet hw0 destruction http://www.cplusplus.com/doc/tutorial/classes/
[/sourcecode]

domingo, 16 de junio de 2013

Arduino LCD 1602 (16x2) display

Another little pearl from www.dx.com is the Arduino compatible LCD 1602 (16 characters each of the 2 rows) display:

20130616_091530

It's really cheap, 6$/4.5€, works very fine and it's easy to use! I will use as a handy debug display and little dashboard (it has 6 buttons at the bottom) while on field robots debugging, but this wioll be a near post with the sourcecode (that also will be a order receiver to program a Rapsberry Pi to control a robot using and Arduino Funduino Duemilanove.)

Arduino LCD text

Two easy examples from the Arduino IDE wondeful examples, it's important that you notice that the initialization sentence should be:

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

The hello world
[sourcecode language="C"]
/*
LiquidCrystal Library - Hello World

Demonstrates the use a 16x2 LCD display. The LiquidCrystal
library works with all LCD displays that are compatible with the
Hitachi HD44780 driver. There are many of them out there, and you
can usually tell them by the 16-pin interface.

This sketch prints "Hello World!" to the LCD
and shows the time.

The circuit:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)

Library originally added 18 Apr 2008
by David A. Mellis
library modified 5 Jul 2009
by Limor Fried (http://www.ladyada.net)
example added 9 Jul 2009
by Tom Igoe
modified 22 Nov 2010
by Tom Igoe

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/LiquidCrystal
*/

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
//LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("Te, amo, Nuriitaa!");
}

void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(millis()/1000);
}
[/sourcecode]

Testing buttons
[sourcecode language="C"]
//Sample using LiquidCrystal library
#include <LiquidCrystal.h>

/*******************************************************

This program will test the LCD panel and the buttons
Mark Bramwell, July 2010

********************************************************/

// select the pins used on the LCD panel
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

// define some values used by the panel and buttons
int lcd_key = 0;
int adc_key_in = 0;
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5

// read the buttons
int read_LCD_buttons()
{
adc_key_in = analogRead(0); // read the value from the sensor
// my buttons when read are centered at these valies: 0, 144, 329, 504, 741
// we add approx 50 to those values and check to see if we are close
if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 195) return btnUP;
if (adc_key_in < 380) return btnDOWN;
if (adc_key_in < 555) return btnLEFT;
if (adc_key_in < 790) return btnSELECT;
return btnNONE; // when all others fail, return this...
}

void setup()
{
lcd.begin(16, 2); // start the library
lcd.setCursor(0,0);
lcd.print("Push the buttons"); // print a simple message
}

void loop()
{
lcd.setCursor(9,1); // move cursor to second line "1" and 9 spaces over
lcd.print(millis()/1000); // display seconds elapsed since power-up


lcd.setCursor(0,1); // move to the begining of the second line
lcd_key = read_LCD_buttons(); // read the buttons

switch (lcd_key) // depending on which button was pushed, we perform an action
{
case btnRIGHT:
{
lcd.print("RIGHT ");
break;
}
case btnLEFT:
{
lcd.print("LEFT ");
break;
}
case btnUP:
{
lcd.print("UP ");
break;
}
case btnDOWN:
{
lcd.print("DOWN ");
break;
}
case btnSELECT:
{
lcd.print("SELECT");
break;
}
case btnNONE:
{
lcd.print("NONE ");
break;
}
}
}
[/sourcecode]

lunes, 15 de abril de 2013

Cheapest Smart Car Robot: parts, examples of use and documentation

[caption id="attachment_1552" align="aligncenter" width="600"]Ultrasonic Smart Car Kit Ultrasonic Smart Car Kit[/caption]

[caption id="attachment_1746" align="alignleft" width="150"]Funduino Arduino DuemilaNove clon Funduino Arduino DuemilaNove clon[/caption]

It's really cheap but it does not include any documentation; but don't panic, all you will need can be found on Internet. And now, the main electronic components: the microcontroller, the dual motor driver, the ultrasonic sensor and the servo:

The microcontroller brain is a Funduino (Arduino Duemilanove clone), and as far I've used it (and as other buyer said is fully, at least for software, compatible). Here we will not have any problem because there are a lot of documentation about Arduino and Duemilanove.

A simple hello world example (here the file)

[sourcecode language="c"]

// Blinking LED

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

void setup()
{
Serial.begin(57600);
printf (&amp;amp;quot;Setup/n/l&amp;amp;quot;);
Serial.println(&amp;amp;quot;Hello world!&amp;amp;quot;);
pinMode(ledPin, OUTPUT);      // sets the digital pin as output
}

void loop()
{
printf (&amp;amp;quot;2009&amp;amp;quot;);
Serial.println(&amp;amp;quot;2009&amp;amp;quot;);
digitalWrite(ledPin, HIGH);   // sets the LED on
delay(1000);                  // waits for a second
digitalWrite(ledPin, LOW);    // sets the LED off
delay(1000);                  // waits for a second
}

[/sourcecode]

[caption id="attachment_1748" align="alignleft" width="150"]Dual_H-Bridge_Motor_Driver Dual_H-Bridge_Motor_Driver[/caption]

But to control the two motors this kit use an Dual H-Bridge motor driver (L298). Here (read the Drive Two DC Motors section) you can find a lot of useful documentation and examples,  and  here you can find also more information, specially about electronics.

Here the file with the example

[sourcecode language="c"]
int ENA=5;//connected to Arduino's port 5(output pwm)
int IN1=2;//connected to Arduino's port 2
int IN2=3;//connected to Arduino's port 3
int ENB=6;//connected to Arduino's port 6(output pwm)
int IN3=4;//connected to Arduino's port 4
int IN4=7;//connected to Arduino's port 7
void setup()
{
Serial.begin(57600);
Serial.println(&amp;amp;quot;Two motors&amp;amp;quot;);
pinMode(ENA,OUTPUT);//output
pinMode(ENB,OUTPUT);
pinMode(IN1,OUTPUT);
pinMode(IN2,OUTPUT);
pinMode(IN3,OUTPUT);
pinMode(IN4,OUTPUT);
digitalWrite(ENA,LOW);
digitalWrite(ENB,LOW);//stop driving
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);//setting motorA's directon
digitalWrite(IN3,HIGH);
digitalWrite(IN4,LOW);//setting motorB's directon
}
void loop()
{
analogWrite(ENA,255);//start driving motorA
analogWrite(ENB,255);//start driving motorB
delay(3000);
analogWrite (IN1,0);//stop driving motorA
analogWrite (IN2,0);//stop driving motorB

analogWrite (IN3,0);//stop driving motorA
analogWrite (IN4,0);//stop driving motorB

delay (3000);

int ledPin = 13;
while (1)
{
digitalWrite(ledPin, HIGH);   // sets the LED on
delay(1000);                  // waits for a second
digitalWrite(ledPin, LOW);    // sets the LED off
delay(000);
}
&amp;amp;lt;pre&amp;amp;gt;[/sourcecode]

[caption id="attachment_1753" align="alignleft" width="150"]Ultrasonic sensor hc-sr04 Ultrasonic sensor hc-sr04[/caption]

The HC-SR04 ultrasonic distance sensor, example of use (here the file):

[sourcecode language="c"]

#define trigPin 9
#define echoPin 11

void setup() {
Serial.begin (115200);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}

void loop() {
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance &amp;amp;gt; 400 || distance &amp;amp;lt; 0){
Serial.println(&amp;amp;quot;Out of range&amp;amp;quot;);
}
else {
Serial.print(distance);
Serial.println(&amp;amp;quot; cm&amp;amp;quot;);
}
delay(500);
}

[/sourcecode]


[caption id="attachment_1755" align="alignleft" width="150"]TowerPro micro servo SG90 TowerPro micro servo SG90[/caption]

And the Tower PRO SG-90 micro servo (and here the file with the example):

[sourcecode language="c"]
// Sweep
// by BARRAGAN &amp;amp;lt;http://barraganstudio.com&amp;amp;gt;
// This example code is in the public domain.

#include &amp;amp;lt;Servo.h&amp;amp;gt;

Servo myservo;  // create servo object to control a servo
// a maximum of eight servo objects can be created

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

void setup()
{
myservo.attach(3);  // attaches the servo on pin 3 to the servo object
}

void loop()
{
for(pos = 0; pos &amp;amp;lt; 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
}
for(pos = 180; pos&amp;amp;gt;=1; 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
}
}
[/sourcecode]

Soon I will add an LCD display with 6 buttons

sábado, 30 de marzo de 2013

Cheapest robotic platform: Smart car

[Updated 12/12/2013:
The commented kit is not being sold currently at dx, but it's sold at Hobby King ]

There are also others kits like:

Funduino Tracking Maze Car for Arduino

Smart Car Chassis Kit for Arduino
]

I have bought the most affordable robotics platform (some photos here) I have found around Internet (at DX): 38€/49$ for all this components:

Ultrasonic Smart Car Kit

My construction:

[caption id="" align="alignnone" width="445"]Cheapest robotic platform Cheapest robotic platform[/caption]

It's really cheap, all the components work flawlessly and very well but IT DOES NOT INCLUDE ANY DOCUMENTATION and some parts (pan tilt base) are impossible to ensemble (it needs another micro servo, 3€/4$) and there is no way to mount on the robot base. Even that, I Think is a good deal, but you should work to get all the needed information (yes, all needed information is over Internet) and put your imagination to build it. I have used Lego Technics parts to mount the ultrasonic sensor.

Here is all the info I have found for every electronic part, code examples and, of course, (soon) a video.

domingo, 17 de marzo de 2013

C. C++, C# robotics programming Workshop/tutorial with the cheapest robotic platform

[caption id="" align="alignleft" width="108"]My Smart Car construction My Smart Car construction[/caption]

[Update March 30, 2013: Added photo gallery]

I will start a C. C++, C# robotics programming Workshop/tutorial with the cheapest robotic platform I have found at dealextreme.com: (Arduino based) plus a Raspberry Pi (like this that use Bioloid as the hardware platform):

Ultrasonic Smart Car Kit

I have not still received the kit, but I will review the kit and start the workshop as soon as I receive it.