Go Down

Topic: Help on improving my bot (Read 4179 times) previous topic - next topic

Pranav_Srivatsa

Oct 26, 2014, 04:01 pm Last Edit: Nov 20, 2014, 02:30 pm by Pranav_Srivatsa Reason: Code attached
Hello everyone Started with arduino ,and the forum has been helpful.
I read a few posts on Obstacle avoidance using HCSR04 and it's done :)  (image attached)!


Works fine with basic obstacle avoidance but i have a few doubts :-

1. Battery dies out fast (used two 9V batteries,also code attached). Is there any low power mode(like msp430) which i can utilise ?

2. Any algorithm which is a bit more efficient ? or any suggestions with improvements for the code is highly appreciated.

Thank you  :)
Code: [Select]
#include <NewPing.h>
#define TRIGGER_PIN  3  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     5  // 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.
const int Motor2Pin1 = 6;
const int Motor2Pin2 = 9;
const int Motor1Pin1 = 10;
const int Motor1Pin2 = 11;
int a;
int b;

void setup()
{

  pinMode(Motor1Pin1, OUTPUT);   
  pinMode(Motor1Pin2, OUTPUT);   
  pinMode(Motor2Pin1, OUTPUT);   
  pinMode(Motor2Pin2, OUTPUT); 
  Serial.begin(9600);

}



void loop()
{
  a=sensor(b);
  motor(a);
  delay(100);
}




int sensor(int b)
{                   
  unsigned int uS = sonar.ping();
  b = (uS / US_ROUNDTRIP_CM);
  return b;
}



int motor(int a)
{
  int val=a;
  if(val==0 || val>20)
    GoForward();

  else if(val<19&&val>14)
  {
    GoRight();

  }

  else if(val <14&&val>5)
  {
    GoLeft();

  }

  else if (val>0&&val<5)
  {
    GoBackward();

  }
}




void GoForward()
{
  digitalWrite(Motor1Pin2, HIGH);
  digitalWrite(Motor1Pin1, LOW);
  digitalWrite(Motor2Pin2, LOW);
  digitalWrite(Motor2Pin1, HIGH);
  delay(100);
}

void GoBackward(){

  digitalWrite(Motor1Pin2, LOW);
  digitalWrite(Motor1Pin1, HIGH);
  digitalWrite(Motor2Pin2, HIGH);
  digitalWrite(Motor2Pin1, LOW);
  delay(100);
}
void GoRight(){
  analogWrite(Motor1Pin2, 255);
  analogWrite(Motor1Pin1, 0);
  analogWrite(Motor2Pin2, 100);
  analogWrite(Motor2Pin1, 0);
  delay(100);
}
void GoLeft(){
  analogWrite(Motor1Pin2, 0);
  analogWrite(Motor1Pin1, 100);
  analogWrite(Motor2Pin2, 0);
  analogWrite(Motor2Pin1, 255);
  delay(100);
}

 

PaulS

Quote
1. Battery dies out fast (used two 9V batteries,also code attached). Is there any low power mode(like msp430) which i can utilise ?
No. You can use something other than pathetic little batteries, though.

Quote
2. Any algorithm which is a bit more efficient ?
More efficient than what? Pancake batter?
The art of getting good answers lies in asking good questions.

Pranav_Srivatsa

Thank you and i was referring to my gibberish code.

PaulS

Quote
Thank you and i was referring to my gibberish code.
Which you have not shared. So, more efficient than your code is an undefined unknown.
The art of getting good answers lies in asking good questions.

Arman5592

#4
Oct 30, 2014, 01:47 pm Last Edit: Oct 30, 2014, 01:50 pm by Arman5592
Use an AC/DC converter or re chargeable power sources , The weird chemistries Nicd Nimh Lipo Li ion  whatever .
Upgrade it with using little motors and better drivers .
no signature

Pranav_Srivatsa

Which you have not shared. So, more efficient than your code is an undefined unknown.
I'm sorry , it said code uploaded i didnt know it was a blank one. Here is the code. Please let me know if its uploaded now. Thank you

PaulS

#6
Oct 31, 2014, 01:03 pm Last Edit: Oct 31, 2014, 01:04 pm by PaulS
Code: [Select]
void loop()
{
sensor(b);
delay(100);
a=sensor(b);
delay(100);
motor(a);
delay(100);
}

Well, you were right. It is pathetic.

Start with some names that follow the Arduino (and every other intelligent programming paradigm) convention of function names that consist of a noun and a verb (like digitalRead(), analogWrite(), etc.).

One can only guess at what sensor() is supposed to do. Clearly, calling it with and without storing the return value is contradictory. If the function returns a value, discarding it is silly.

The robot() will do NOTHING during the stickYourThumbUpYourAssAndDoNothing() (aka delay()) call.

Code: [Select]
int sensor(int b)
{ delay(50);                    
  unsigned int uS = sonar.ping();
  b = (uS / US_ROUNDTRIP_CM);
  return b;
}

Assigning a value to the input argument does not make sense.

Code: [Select]
int motor(int a)
{
 int val=a;
  if(val==0 || val>20)
   GoForward();

Copying the input value, and then ignoring the input value, while never changing the copy, does not make sense.

Posting code that is so poorly indented does not make sense. Especially when Tools + Auto Format is so easy to use.

While I'm at it, zipping a single ino file, whose size is well under the cut-and-paste in a code box limit, and attaching it does not make sense.
The art of getting good answers lies in asking good questions.

Pranav_Srivatsa

Thanks for the extremely valuable suggestions (oh wait i have to search for it in that heap of criticism).
Good way to encourage a beginner, Says a lot about you sir. Thank you.

The bot works well (or maybe its Lokis illusion from asgard) and the  'stickYourThumbUpYourAssAndDoNothing() ' was intended to make the motor stay in that position for a while- was experimenting with delays and the additional two delay statements was to see if battery life improves (read it from some website).

nice way to flame up a thread, Good job.

Thank You  :)
   

AWOL

#8
Oct 31, 2014, 02:57 pm Last Edit: Oct 31, 2014, 03:03 pm by AWOL
Here's the code for those who can't be bothered to download and unzip it.
Code: [Select]
#include <NewPing.h>
#define TRIGGER_PIN  3  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     5  // 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.

const int Motor2Pin1 = 6;
const int Motor2Pin2 = 9;

const int Motor1Pin1 = 10;
const int Motor1Pin2 = 11;
int a;
int b;

void setup()
{
  pinMode(Motor1Pin1, OUTPUT);   
  pinMode(Motor1Pin2, OUTPUT);   
  pinMode(Motor2Pin1, OUTPUT);   
  pinMode(Motor2Pin2, OUTPUT); 
  Serial.begin(9600);

}



void loop()
{
  sensor(b);
  delay(100);
  a=sensor(b);
  delay(100);
  motor(a);
  delay(100);
}




int sensor(int b)
{
  delay(50);                   
  unsigned int uS = sonar.ping();
  b = (uS / US_ROUNDTRIP_CM);
  return b;
}



int motor(int a)
{
  int val=a;
  if(val==0 || val>20)
    GoForward();

  else if(val<19&&val>14)
  {
    GoRight();

  }

  else if(val <14&&val>5)
  {
    GoLeft();

  }

  else if (val>0&&val<5)
  {
    GoBackward();

  }
}




void GoForward()
{
  analogWrite(Motor1Pin2, 100);
  analogWrite(Motor1Pin1, 255);
  analogWrite(Motor2Pin2, 100);
  analogWrite(Motor2Pin1, 255);
  delay(100);
}

void GoBackward(){
  analogWrite(Motor1Pin1, 100);
  analogWrite(Motor1Pin2, 255);
  analogWrite(Motor2Pin1, 100);
  analogWrite(Motor2Pin2, 255);
  delay(100);
}

void GoLeft(){
  analogWrite(Motor1Pin1, 100);
  analogWrite(Motor1Pin2, 255);
  analogWrite(Motor2Pin2, 100);
  analogWrite(Motor2Pin1, 255);
  delay(100);
}

void GoRight(){
  analogWrite(Motor1Pin2, 100);
  analogWrite(Motor1Pin1, 255);
  analogWrite(Motor2Pin1, 100);
  analogWrite(Motor2Pin2, 255);
  delay(100);
}

void Stop(){
  digitalWrite(Motor1Pin2, LOW);
  digitalWrite(Motor1Pin1, LOW);
  digitalWrite(Motor2Pin1, LOW);
  digitalWrite(Motor2Pin2, LOW);
  delay(100);
}


Hint: A really good way of improving your code is by getting it to tell you what it is doing.

Pranav_Srivatsa


Arman5592

You can use non-gearboxed HIGH RPM (or geared for HIGHER RPM) motors , so the bot would move fast . Then make a fast-reaction code so you'd have something totally fast-reaction .
To make your arduino work faster , there are two things you can do :
1- Overclock
2- cool the system down
And as a blog post demonstrated , using liquid nitrogen can make the uno to go up to 64 Mhz which is 4 times faster .
You can safely (I AM NOT RESPONSIBLE FOR ANYTHING) overclock a uno to 20Mhz as the Atmega328 can go up to 20 Mhz .
no signature

Cactusface

Hi Pranav_Srivatsa,
                              We really need more info from you! Like what motors, batteries!! which could be your problem. If you can show us how you have everything wired up.

Regards

Mel.
Open your mind! But not too far, your brains might fall out.
Also like Photography, model building and my 300+ Cacti and Succs.

leolfs

Paul S does have a point though. (And it's a consistent one!) It can be hell to have to wade past a page of inane posts to find anything useful or consider offering any help with. The Forum really does need to be interesting technically not just amusing.(Though a little levity is a always a plus). The moderator should reject the following type of posts:
1 Help Me. With whatever I think I don't know but didn't think about.
2 Any mention of college assignments. That should go into a "I will pay handsomely for someone to cheat for me because my brain hurts too much" topic.
3 Any code with a delay()  to be banned from the Forum!

if asking for specific help then post code with first post. But before you hit that  post button do imagine you are someone else reading it and criticise severely. You will then definitely have a better experience on the forum. (Paul is sometimes quite forthcoming).
Anyway, getting shot down in flames is a great opportunity to get over your limitations. The worst you'll suffer is gravel rash. Learn from it.

Cactusface

Hi,
          Yes I agree to a point! Not sure about the delay() as it's easy for beginners to use.
Yes I have noticed, some that don't just want a guide or advice but want it all, we're not a code writting service, well I could be if the price was right (I'm not really that good).

What really gets me is when we all help offer our advice and sugesstions! Only to never get a reply from the poster who has now got the answer they wanted or has just gave up because their head is spinning..

And cross-posters what a waste!!  Or the one who follows his post 2 hours later with another, because he's had no reply yet.

Perhaps some forum rules need looking at!!

Regards

Mel.
Open your mind! But not too far, your brains might fall out.
Also like Photography, model building and my 300+ Cacti and Succs.

Pranav_Srivatsa

You can use non-gearboxed HIGH RPM (or geared for HIGHER RPM) motors , so the bot would move fast . Then make a fast-reaction code so you'd have something totally fast-reaction .
To make your arduino work faster , there are two things you can do :
1- Overclock
2- cool the system down
And as a blog post demonstrated , using liquid nitrogen can make the uno to go up to 64 Mhz which is 4 times faster .
You can safely (I AM NOT RESPONSIBLE FOR ANYTHING) overclock a uno to 20Mhz as the Atmega328 can go up to 20 Mhz .

Seems intresting, thanks will try it out :)

Go Up