# i think my robot died

hey guys

am having an issue with my grad project, am tasked with making a robot that moves in a square wave pattern like the first picture below in attachments ,while applying obstacle avoidance as well .
and in order to know how much distance did the little guy go i used IR encoders by counting the number of clicks the robot makes while moving and using that number in the following formula to get the distance

Distance traveled = (Encoder ticks / 360) * circumference

now the robot doesn’t move at all, here is the code am using

``````typedef struct
{
int X;
int Y;
bool scanned;
bool blocked;
}cell;
int Echo1 = A2;
int Trig1 = A3;
int in1 = 4;
int in2 = 5;
int in3 = 6;
int in4 = 7;
int ENA = 8;
int ENB = 9;
int encoder_left = 2;
int encoder_right = 3;
int leftclick,rightclick=0;
float leftdis,rightdis=0;
float lt,rt=0.16328;
int i,j,k,l,o,p,q,r,s,t=0;
int m =3/0.165;
int n =2/0.157;
int z = m*n;
int Distance = 0;
//Distance traveled = (Encoder ticks / 360) * circumference
void calculateright(void)
{
{
rightclick++;
}
rightdis=((float)rightclick/360)*(0.16328);
}
void calculateleft(void)
{
{
leftclick++;
}
leftdis=((float)leftclick/360)*(0.16328);
}
int Distance_test()
{
digitalWrite(Trig1, LOW);
delayMicroseconds(2);
digitalWrite(Trig1, HIGH);
delayMicroseconds(20);
digitalWrite(Trig1, LOW);
float Fdistance = pulseIn(Echo1, HIGH);
delay(10);
Fdistance = Fdistance / 29 / 2;
Serial.println(Fdistance);
return (int)Fdistance;
}
void Enable(void)
{
digitalWrite(ENA, HIGH);
digitalWrite(ENB, HIGH);
}
void Forward(void)
{
while(leftdis<lt&&rightdis<rt)
{
calculateleft();
calculateright();
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
Serial.println("ROBOT_MOVING_FORWARD");
}
leftclick=0;
rightclick=0;
leftdis=0;
rightdis=0;
}
void Backward(void)
{
while(leftdis<lt&&rightdis<rt)
{
calculateleft();
calculateright();
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
Serial.println("ROBOT_MOVING_BACKWARD");
}
leftclick=0;
rightclick=0;
leftdis=0;
rightdis=0;
}
void Left(void)
{
while(leftdis<lt/2&&rightdis<rt/2)
{
calculateleft();
calculateright();
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
Serial.println("ROBOT_MOVING_LEFT");
}
leftclick=0;
rightclick=0;
leftdis=0;
rightdis=0;
}
void Right(void)
{
while(leftdis<lt/2&&rightdis<rt/2)
{
calculateleft();
calculateright();
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
Serial.println("ROBOT_MOVING_RIGHT");
}
leftclick=0;
rightclick=0;
leftdis=0;
rightdis=0;
}
void Stop(void)
{
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
Serial.println("ROBOT_STOP");
}
void Disable(void)
{
digitalWrite(ENA, LOW);
digitalWrite(ENB, LOW);
}
void setup()
{
Enable();
Stop();
Serial.begin(9600);
pinMode(in1,OUTPUT);
pinMode(in2,OUTPUT);
pinMode(in3,OUTPUT);
pinMode(in4,OUTPUT);
pinMode(ENA,OUTPUT);
pinMode(ENB,OUTPUT);
pinMode(Echo1, INPUT);
pinMode(Trig1, OUTPUT);
pinMode(encoder_left, INPUT);
pinMode(encoder_right, INPUT);
}
void loop()
{
cell c[m][n];
for( k=0; k<m; k++)
{
for( i=0; i<n; i++)
{
c[k][i].X =i;
c[k][i].Y =k;
Distance =Distance_test();
if(i*k == z)
{
Stop ();
}
if(i== n-1)
{
if(Distance>=10)
{
if(t%2==0)
{
Right ();
Forward ();
Right ();
c[k][i]. scanned =true;
c[k][i].blocked=false;
}
else
{
Left ();
Forward ();
Left ();
c[k][i]. scanned =true;
c[k][i].blocked=false;
}
}
else if (Distance<10)
{
if(t%2==0)
{
Right ();
Forward ();
Left ();
Right ();
Right ();
c[k][i]. scanned =false;
c[k][i]. blocked=true;
}
else
{
Left ();
Forward ();
Right ();
Left ();
Left ();
c[k][i]. scanned =false;
c[k][i]. blocked=true;
}
}
t++;
}
else
{
if(Distance>=10)
{
Forward ();
c[k][i]. scanned =true;
c[k][i].blocked=false;
}
else if(Distance<10)
{
Left ();
Forward ();
Right ();
Forward ();
Right ();
Forward ();
Left ();
c[k][i]. scanned =false;
c[k][i]. blocked=true;
}
}
}
}
Disable();
}
``````

and for hardware am using the dagu DG012-SV tank frame which is equipped with two 5v dc motors
am using a single HC-SR04 for obstacle avoidance and 2 Encoder FC-03 one for each motor and a 11.1V-5.2A 3 cell Lipo battery with a dc-dc voltage converter that takes from 12-48v and produce 5vdc with 5A ,am using a L298 Dual Motor Driver Module 2A to drive the 2 motors

if anyone can point me in the right direction so i can fix the problem i’ll be very thankfull

DG02S.pdf (111 KB)

First is to test sub-systems. Make sure that the motors are working. Eg....manually wire the motors to a known working motor driver. Or.... if the motors handle 2.5 Volt DC, then use a bench supply (that is capable of supplying enough power) to supply 2.5 V DC to the motor. Make sure it works.

L298N can supply voltages much higher than 2.5 V DC to motors. So may need to see whether the L298N really did supply much more voltage (and/or power) than what the DC motors could handle.

When you think your robot has died, you need to describe the situation. Eg.... got lights on arduino? Got lights on L298N? No power or lights anywhere? etc.

Southpark:
First is to test sub-systems. Make sure that the motors are working. Eg....manually wire the motors to a known working motor driver. Or.... if the motors handle 2.5 Volt DC, then use a bench supply (that is capable of supplying enough power) to supply 2.5 V DC to the motor. Make sure it works.

L298N can supply voltages much higher than 2.5 V DC to motors. So may need to see whether the L298N really did supply much more voltage (and/or power) than what the DC motors could handle.

When you think your robot has died, you need to describe the situation. Eg.... got lights on arduino? Got lights on L298N? No power or lights anywhere? etc.

hey SP if i can call you that

i commented the encoder part of the code and the obstacle avoidance part too and powered the arduino
the power reached the driver and the motors with 2.8V~2.9V on each motor.
that was without the tracks and when i added the tracks and the voltage dropped to a range of 2.3V~2.5V.

but if i connect the ultrasonic sensor and uncomment it's part in the code and the encoders too it stops,the encoders are already connected from the beginning but the ultrasonic is connected later.

am considering getting a 6V /4Ah battery to replace the lipo and the converter maybe that would be better to remove the middle man "the converter in my case"

i had a typo in my initial problem post am using two 5dcv motors
sorry

mkhtab:
now the robot doesn't move at all

This makes it sound like you had a previous version of the code that did cause the robot to move then when you added the encoders it stopped moving. Is that correct? If so then go back to the previous working configuration. This will tell you whether the robot "died", which I would interpret as damaged hardware, or if something you did when you added the encoders caused the problem but didn't actually damage anything.

pert:
This makes it sound like you had a previous version of the code that did cause the robot to move then when you added the encoders it stopped moving. Is that correct? If so then go back to the previous working configuration. This will tell you whether the robot "died", which I would interpret as damaged hardware, or if something you did when you added the encoders caused the problem but didn't actually damage anything.

i checked each component separately with it's proper functions and they all worked,the encoders are the issue i guess but in the same time upon reconnecting the ultrasonic sensor it stops again like it wants to do either obstacle avoidance or moving it's pretty frustrating

``````float lt,rt=0.16328;
``````

What is the value in lt when this statement is done? It is NOT 0.16328.

``````   rightdis=((float)rightclick/360)*(0.16328);
``````

Magic numbers suck. I can't even guess what 0.16328 means.

``````  float Fdistance = pulseIn(Echo1, HIGH);
``````

The pulseIn() function does not return a float. The time that it returns isn't a distance, either.

Your robot forgets everything it learned on a given pass through loop. Why?

i fixed the ultrasonic part thanks for the help but the 0.16328 is the circumference of the wheel am using
and i fixed the lt part thanks for that point too.
now all i need is help in using the encoders to make the robot more accurate and effective
i connected the battery directly to the robot and used the usb cable to read the serial monitor that gave the robot enough power to move do i need a more powerful battery
thanks again

do i need a more powerful battery

Yes.

the problem was in a few parts of the model:
1- one of the two motors had a defective gearbox that prohibited proper movement.
2- the dc converter had some major losses in current"still working out why"
3- in the code the detection distance was to tight for the robot to take the proper action in time

fixes:
*replaced defective motor
*used a power converter circuit that damped the voltage from 11.1V to 7.1V which was the optimal operation voltage
*adjusted detection distance in code to 150 cm it seems to make the robot work just fine

bottomline:
i fixed all problems a day before the delivery date, got the robot to work fine enough to pass testing , got an

Very nicely done mkhtab. You were very thorough in your testing. Very good signs.

Excellent. And I can show off my vocabulary now, because that "square wave pattern like the first picture below"
has a name, boustrophedon, from the (ancient) Greek for 'like the ox turns' - as in ploughing a field.

MarkT:
Excellent. And I can show off my vocabulary now, because that "square wave pattern like the first picture below"
has a name, boustrophedon, from the (ancient) Greek for 'like the ox turns' - as in ploughing a field.

Hi Mark! What picture is that? !!! You better share the picture. I will be googling that word you mentioned. Got me interested already.

Also, did you post this message under the wrong thread?
(or case of a spambot/wormbot takeover of account? heheheheh)

I think he means this one.

Oh I see! Ok..... that's the modern form. In the old days, it used to be ox plough. But now it is tractor ploughing.

The only thing here is, assuming that a tractor knows how to write words in the ground ....... standard boustrophedon will have some kind of behaviour ---- such that when the tractor starts coming around to begin its right-to-left journey....... the tractor's writing needs to be flipped 180 degrees for the 'right-to-left' journey. This is assuming that the tractor was going left-to-right on the first line (leg) of the trip.

So it would sort of be similar to writing words in "cascade" format (ie. all joined along one line), except, when we curve the line around (to get to the next line), the line will need to be physically 'twisted' 180 degrees (ie. a 180 degree "axial twist" for each alternate line).