duminică, 15 aprilie 2012

Almost ready...





Built with PIC 18F2550 from Microchip.

Future improvement: implementation of a steering control so you just turn to the desired angle.
For now this is my mobile robot:



This is the Pinguino with 18F2550 from Microchip:


This is DC motor controller which I have used.

..and the unipolar stepper controller:


And the code that I use it now:

#include
#include

const int pingPin = 7;
const int SafeDist = 40;
unsigned long duration;
long int cm,st,dr;
int motorPin1=14;
int motorPin2=16;
int motorPin3=13;
int motorPin4=15;
int motorDelay=3;/// viteza stepper
int i;

void setup()
{
///pini motoare dc
pinMode (0,OUTPUT);
pinMode (1,OUTPUT);
pinMode (2,OUTPUT);
pinMode (3,OUTPUT);
///pini stepper bipolar
pinMode(13, OUTPUT);
pinMode(14, OUTPUT);
pinMode(15, OUTPUT);
pinMode(16, OUTPUT);
}

void SonarScan()
{
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH,10000);
}

void inainte()
{
digitalWrite(0, HIGH);
digitalWrite(1, LOW);
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
delay(200);
digitalWrite (0,LOW);
digitalWrite (1,LOW);
digitalWrite (2,LOW);
digitalWrite (3,LOW);
}
void ho()
{
digitalWrite(0, LOW);
digitalWrite(1, LOW);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
}
void inapoi()
{
digitalWrite (0,LOW);
digitalWrite (1,HIGH);
digitalWrite (2,LOW);
digitalWrite (3,HIGH);
delay(1000);
digitalWrite (0,LOW);
digitalWrite (1,LOW);
digitalWrite (2,LOW);
digitalWrite (3,LOW);
}
void dreapta()
{
digitalWrite (0,HIGH);
digitalWrite (1,LOW);
digitalWrite (2,LOW);
digitalWrite (3,HIGH);
delay(666);
digitalWrite (0,LOW);
digitalWrite (1,LOW);
digitalWrite (2,LOW);
digitalWrite (3,LOW);
}
void stanga()
{
digitalWrite (0,LOW);
digitalWrite (1,HIGH);
digitalWrite (2,HIGH);
digitalWrite (3,LOW);
delay(666);
digitalWrite (0,LOW);
digitalWrite (1,LOW);
digitalWrite (2,LOW);
digitalWrite (3,LOW);
}
///baleere stanga
void bast()
{
for(i=1;i<130;i++)
{
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
delay(motorDelay);

digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
delay(motorDelay);

digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
delay(motorDelay);

digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
delay(motorDelay);
}
}
///baleere dreapta
void badr()
{
for(i=1;i<130;i++)
{
digitalWrite(motorPin4, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin1, LOW);
delay(motorDelay);

digitalWrite(motorPin4, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin1, HIGH);
delay(motorDelay);

digitalWrite(motorPin4, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin1, HIGH);
delay(motorDelay);

digitalWrite(motorPin4, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin1, LOW);
delay(motorDelay);
}
}
void loop()
{
SonarScan();
if (duration < 2000)
{
ho();
duration=0;
delay(300);
badr();
SonarScan();
dr=duration;
bast();
bast();
SonarScan();
badr();
st= duration;
if (dr< 2000 && st <2000)
{
inapoi();
stanga();
delay(55);
stanga();
}
else if (dr > st)
{
dreapta();
}
else stanga();
}
else inainte();
}