Archive for: April 2013

Cheapest Arduino based robot: parts, examples of use and documentation

This entry is part 3 of 5 in the series Cheapest robot (Arduino)
Ultrasonic Smart Car Kit

Ultrasonic Smart Car Kit

Arduino cheap robot car

Arduino cheap robot car

Funduino Arduino DuemilaNove clon

Funduino Arduino DuemilaNove clon

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)

// Blinking LED

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

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

void loop()
{
  printf ("2009");
  Serial.println("2009");
  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
}
Dual_H-Bridge_Motor_Driver

Dual_H-Bridge_Motor_Driver

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 and here you can find also more information, specially about electronics.

Here a file with a simple example, and here another example with speed control

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

int ledPin = 13;

void blink(int times)
{  
  while (times>0)
  {
    digitalWrite(ledPin, HIGH);   // sets the LED on
    delay(500);                  // waits for a second
    digitalWrite(ledPin, LOW);    // sets the LED off
    delay(500);
    times--;
  }
}

void initialize()
{
  Serial.println("Two motors");

  pinMode(ENA,OUTPUT);
  pinMode(ENB,OUTPUT);
  pinMode(IN1,OUTPUT);
  pinMode(IN2,OUTPUT);
  pinMode(IN3,OUTPUT);
  pinMode(IN4,OUTPUT);

  digitalWrite(ENA,LOW);
  digitalWrite(ENB,LOW);//stop motors

  digitalWrite(IN1,HIGH);
  digitalWrite(IN2,LOW);//setting motorA's forward directon
  digitalWrite(IN3,HIGH);
  digitalWrite(IN4,LOW);//setting motorB's forward directon
}

void setup()
{
  Serial.begin(57600);
  initialize();
}

void loop()
{
  int t=1;
  initialize();

  Serial.println("1 Full forward");
  analogWrite(ENA,255);//start driving motorA
  analogWrite(ENB,255);//start driving motorB

  delay(1000*t);

  Serial.println("2.1 Turning A");

  // Backwards motor A, turning robot
  digitalWrite(IN1,LOW);
  digitalWrite(IN2,HIGH);// setting motorA's backwards directon

  delay(500*t);

  // full forward again
  digitalWrite(IN1,HIGH);
  digitalWrite(IN2,LOW);//setting motorA's forward directon

  delay (1000*t);

  Serial.println("2.2 Turning B");

  // Backwards motor B, turning robot
  digitalWrite(IN3,LOW);
  digitalWrite(IN4,HIGH);//setting motorB's backward directon

  delay(500*t);

  // full forward again
  digitalWrite(IN3,HIGH);
  digitalWrite(IN4,LOW);//setting motorB's forward directon

  delay (1000*t);

  Serial.println("3 Full backward");
  // full backwards
  digitalWrite(IN1,LOW);
  digitalWrite(IN2,HIGH);// setting motorA's backwards directon

  digitalWrite(IN3,LOW);
  digitalWrite(IN4,HIGH);//setting motorB's backward directon

  delay (2000*t);
  // Slowing motors

  Serial.println("4 slowing motors");
  analogWrite (ENA,180);
  analogWrite (ENB,180);
  delay(1000*t);

  Serial.println("5 more slowing motors");
  analogWrite (ENA,110);//stop driving motorA
  analogWrite (ENB,110);//stop driving motorB
  delay(1000*t);

  Serial.println("6 stoping motors");
  analogWrite (ENA,0);//stop driving motorA
  analogWrite (ENB,0);//stop driving motorB

  blink (3);
Ultrasonic sensor hc-sr04

Ultrasonic sensor hc-sr04

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

/*
HC-SR04 Ping distance sensor]
VCC to arduino 5v GND to arduino GND
Echo to Arduino pin 13 Trig to Arduino pin 12
More info at: http://goo.gl/kJ8Gl
*/

#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 > 400 || distance < 0){
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }

  delay(500);
}

TowerPro micro servo SG90

TowerPro micro servo SG90

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

// Sweep
// by BARRAGAN <http://barraganstudio.com>
// This example code is in the public domain.

#include

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()
{
  // goes from 0 degrees to 180 degrees in steps of 1 degree 
  for(pos = 0; pos < 180; pos += 1) {
    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>=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
  }
}

Soon I will add an LCD display with 6 buttons

CM-900 Robotis IDE more programming examples

This entry is part 1 of 5 in the series Programming CM-900

I’ve started to program CM-900 “translating” the example from the C programming tutorial for CM-510

CM900_with_its parts_names

You can get more information at robotsource.org CM-900 community circle and the CM-900 e-manual. There you can find:

– Quick start guide

– Presentation Workshop

– Arduino based IDE to program it very easily (examples included)

– Source code and instructions

– Using other servos

– Interfacing sensors

I already have “translated” two examples: a simple “hello world” like where a servo is move to 2 different positions and another where the servo id and position is asked and after entered the data the servo is positionated in the typed position, validating that id and position are in range.

The “hello World ” example (here the zipped file):

#define P_GOAL_POSITION_L     30
#define P_MOVING              46

int counter;
int onlyOnceHappened;

void setup()
{
//USB Serial initialize

onlyOnceHappened=0;
counter=0;

SerialUSB.begin();
Dxl.begin(1);
delay (5000);
SerialUSB.println("Setup");
}

void loop()
{

delay (3000);
SerialUSB.print("loop");
SerialUSB.println(counter);
counter++;
delay(1000);

if (onlyOnceHappened==0)
{
onlyOnceHappened=1;
SerialUSB.println("Hello, World");
}

int id=8;

SerialUSB.println ("Simple example #0");

SerialUSB.print ("Perform movement 1 with the AX-12:");
SerialUSB.println (id);
byte bMoving = Dxl.readByte( id, P_MOVING);
byte CommStatus = Dxl.getResult();
if( CommStatus == COMM_RXSUCCESS )
Dxl.writeWord( id, P_GOAL_POSITION_L, 511 );
else
SerialUSB.println ("CommStatus IS NOT COMM_RXSUCCESS");

SerialUSB.println ("Pause half a second");
delay (500); // half-second pause

SerialUSB.println ("Beep!");
//buzzOn (100); // beep

SerialUSB.println ("Pause for a second");
delay (1000); // pause for 1 second

SerialUSB.print ("Perform movement 2 with the AX-12: ");
SerialUSB.println (id);

Dxl.writeWord( id, P_GOAL_POSITION_L, 611 );

SerialUSB.println ("End");

}

Asking Id and Position example, (here the zipped file)


int counter;
bool onlyOnceHappened;

int id;
int position;

bool debugOutputOn;
char charReaded;

void debugOutputUSB(char *buffer, bool newlineAfterMessage)
{
if (debugOutputOn)
{
if (newlineAfterMessage)
SerialUSB.println(buffer);
else
SerialUSB.print(buffer);
}
}

void debugOutputUSB(int value, bool newlineAfterMessage)
{
if (debugOutputOn)
{
if (newlineAfterMessage)
SerialUSB.println(value);
else
SerialUSB.print(value);
}
}

void debugOutputPairUSB(char *buffer, int value)
{
if (debugOutputOn)
{
SerialUSB.println ("");
SerialUSB.print(buffer);
SerialUSB.println(value);
}
}

void debugOutputPairUSB(char *buffer1, char *buffer2)
{
if (debugOutputOn)
{
SerialUSB.println ("");
SerialUSB.print(buffer1);
SerialUSB.println(buffer2);
}
}

void setup()
{
//USB Serial initialize

onlyOnceHappened=false;
counter=0;
debugOutputOn=false;

SerialUSB.begin();
Dxl.begin(1);
delay (3000);

debugOutputUSB("Setuped", true);
}

void checkIfDebugOn()
{
SerialUSB.print("Put debug ON?, y/n");
charReaded=SerialUSB.read();

if (charReaded=='y' || charReaded=='Y')
debugOutputOn=true;
else
debugOutputOn=false;

debugOutputPairUSB ("DebugPut is:", debugOutputOn);
}

void loop()
{
delay (1500);
SerialUSB.println ("Simple example #1");

if (onlyOnceHappened==false)
{
onlyOnceHappened=true;
checkIfDebugOn();
}

debugOutputPairUSB  ("Loop: ",counter);
counter++;
delay(500);

debugOutputUSB("Starting loop", true);

while(true) // we'll repeat this loop forever
{
id=getId(); // get the ID of the AX-12 that we want to move
position=getPosition(); // get the goal position

Dxl.writeWord( id, P_GOAL_POSITION_L, position); // sent the command to the Dynamixel
debugOutputUSB ("Dxl.writeWorded", true);
}
}
// I have created a simple ayoi because I have problem compiling atoi
int myatoi(char *buffer)
{
int len=strlen(buffer)-2;
int value=0;
int i=0;
int base=0;
int number=0;

for (i=len;i>=0;i--)
{
number=int(buffer[i])-int('0');

debugOutputPairUSB ("buffer[i]: ", int(buffer[i]));

if (i==len)
{
value=number;
}
else
{
value+=base * number;
}

debugOutputPairUSB ("i: ",i);
debugOutputPairUSB ("number: ", number);
debugOutputPairUSB("base: ", base);
debugOutputPairUSB("valor: ", value);

if (base==0)
{
base=10;
}
else
base=base*10;
}

return value;
}

bool isValid(int value, int MinValue, int MaxValue)
{
return (value>=MinValue && value<=MaxValue);
}

void readString(char *bufferParameter)
{
int i=0; // We'll use this variable as index of the buffer where we will store data

do
{
bufferParameter[i]=(char) SerialUSB.read(); // it store the read character in the i position of bufferParameter
SerialUSB.print(bufferParameter[i]); // showing it
if (bufferParameter[i]=='\b') // if Backspace was pressed
i--; // it goes to the previous position, rewritting the previoulsy typed character
else //
i++; // it will write in the next position
//    SerialUSB.println(int(bufferParameter[i-1]));
}while(bufferParameter[i-1]!=10); // while the last values is not INTRO. The symmbol ! represents the logical operator NOT

bufferParameter[i]=0; // A NULL (0, zero) is necessary in the last position of any string
}

/*
It read an string, it's converted to integer and returned
*/
int readInteger(char *bufferParameter)
{
//  return SerialUSB.parseInt();

readString(bufferParameter);

//  int valor=int(bufferParameter[0]-int('0'));
debugOutputPairUSB ("Leido: ", bufferParameter);
int valor=myatoi(bufferParameter);

debugOutputPairUSB ("Valor leido: ", valor);

return valor;
}

int getId()
{
/*
We define an array enough large, 256 bytes (characters). Probably it's enough with 4 bytes, but if we type more than the defined size we will get an error*/
char buffer[256];

/*
And we define another integer variable, it's very advisable to asign a value in the definition, in this case we assign the minimun value, 1*/
int ax12Id=1;

do
{ // starting the loop
SerialUSB.println ("Enter the ID of the AX-12 that you wwant to move, between 1 y 18: ");
SerialUSB.print("Id:");
ax12Id=readInteger(buffer); // this function will read from what we type in the keyboard
//// exclamation (!) is the NOT logical operator. It will repeat the loop while the value is not valid
}while(!isValid(ax12Id, 1, 18));

// Showing the typed value
SerialUSB.println(" ");
SerialUSB.print("AX12 ID:");
SerialUSB.println(ax12Id);

return ax12Id;
}

int getPosition()
{
char buffer[256];
int position=0;

do
{
SerialUSB.println ("Enter a value between 0 and 1023 as the goal position");
SerialUSB.print ("Position:");
position=readInteger(buffer);
}while(!isValid(position, 0, 1023));

// Showing the typed value
SerialUSB.println(" ");
SerialUSB.print("Position:");
SerialUSB.println(position);

return position;
}

%d bloggers like this: