Hey, need some help on where to start programming! (Balancing Bot)

Hey there arduino peoples.

So ive been here for a bit, i joined a while ago and had the plans of building a hexapod - i decided to do a crawler later, I'd rather do a moderately priced balancing bot first.

What im using:

Pololu 29:1 Gear Motors with Encoders Pololu VNH019 Motor Driver Arduino Uno (Havnt Chosen Yet): Gyroscope and Accelerometer

So im ordering these parts in a few days. I already have an arduino uno which ive been using for the past year. I finished the arduino cookbook early this year and so i have some knowledge of programming.

My question is, where do I start programming for this bot? My intention now is to get the motors running first, then to add in my gyro and accelerometer to get it balancing. And once it balances, to add more sensors and whatever.

So any help on helping me get my programming started would be appreciated! Thanks

Sounds like you have a plan. I'm not sure what you need advice on?

What I needed help on was on my code - i really dont know how to start off my code since ive never worked with dc motor control and motor drivers

edit:

to be honest i feel really lost on how I should start my code. Might sound dumb, but im not sure on what to do. If someone could help me out by maybe helping write the start of the code, or telling me how I should start, it would help. What im really not sure is how im supposed to change my code when using the Pololu motor driver, or would it be the same as just using the motors with the arduino itself? I checked the sample code for the driver, and i can understand part of it, but it isnt making much sense to me on where I should begin to get my motors moving forward and backwards. maybe im over thinking it.

jdmkid: to be honest i feel really lost on how I should start my code

Start with the simplest part of the problem you can find. Make your sketch run and print "hello world". Make it drive the motor forwards at varying speeds. Make it move the motor backwards at varying speeds.

Read up on the sensors you are going to need to use. Learn how to connect and power them. Learn how to read from them. Print the results you get and see how they vary as you wave the sensors around as if they were attached to a balance bot.

Think about what motor outputs your 'bot would need to provide in order to balance in those scenarios and see how those would be related to the corresponding sensor inputs. What you're designing here is a feedback control system. You will almost certainly want to use a PID control algorithm for this so read up on that and run the examples showing how it works.

You won't solve a problem like this in one go - you will solve it one small step at a time. Each time you succeed at one step, look at the next smallest step you could take towards the working solution. With persistence, you'll get there eventually.

Thank you very much for the reply, thats exactly what I needed!

Ill look into PID's and the other things you said and once im done and start my code ill post my progress and pictures of the bot.

Thanks again

This is what I came up with. Pretty much what I was hoping to achieve with this code was to make the motor go from stop to full speed, delay for a bit, go back to zero, and then do the same thing but in reverse. Let me know how it looks. I havnt been able to test it becuase my order hasnt come in yet.

#include <DualVNH5019MotorShield.h>

//Pin Map
int MINA1 = 2;         // Motor 1 direction input A
int MINB1 = 4;         // Motor 1 direction input B
int M1ENDIAG1 = 6;     // Motor 1 enable input/fault output
int M1CS = A0;         // Motor 1 current sense output
int MINA2 = 7;         // Motor 2 direction input A
int MINB2 = 8;         // Motor 2 direction input B
int M2ENDIAG2 = A2;
int M2CS = A1;

void setup()
{ 
  pinMode(MINA1, OUTPUT);    // Motor 1 forward 
  pinMode(MINB1, OUTPUT);    // Motor 1 reverse
  pinMode(MINA2, OUTPUT);    // Motor 2 forward
  pinMode(MINB2, OUTPUT);    // Motor 2 reverse 
}

void loop()
{
  delay(4000);
  for(int speedvalue = 0 ; speedvalue <= 255; speedvalue +10) {
    analogWrite(MINA1, speedvalue);
    analogWrite(MINA2, speedvalue);
    delay(6000);
  
  for(int speedvalue = 255 ; speedvalue >= 0; speedvalue -10) {
    analogWrite(MINA1, speedvalue);
    analogWrite(MINA2, speedvalue);
    delay(6000);
  }
}
  
  for(int speedvalue = 0 ; speedvalue <= 255; speedvalue +10) {
    analogWrite(MINB1, speedvalue);
    analogWrite(MINB2, speedvalue);
    delay(6000);
  
  for(int speedvalue = 255 ; speedvalue >= 0; speedvalue -10) 
    analogWrite(MINB1, speedvalue);
    analogWrite(MINB2, speedvalue);
    delay(6000);
  }
}

PeterH: Start with the simplest part of the problem you can find. Make your sketch run and print "hello world". Make it drive the motor forwards at varying speeds. Make it move the motor backwards at varying speeds.

...

You won't solve a problem like this in one go - you will solve it one small step at a time. Each time you succeed at one step, look at the next smallest step you could take towards the working solution. With persistence, you'll get there eventually.

This is really excellent advice. Breaking down a problem into its component parts like this is something that experienced programmers do without even thinking about it, and it's easy to forget that it's actually a skill of its own right.

jdmkid:

  for(int speedvalue = 0 ; speedvalue <= 255; speedvalue +10) {

I think you mean that to be “speedvalue += 10”.

Oh yeah thanks i didnt notice that! And yeah i agree, sorting the problem into smaller problems and solving them makes it much easier.

Other than that, is the code alright?

jdmkid: Other than that, is the code alright?

Sorry. I don't work with the servo library, so I can't say.

It looks correct to me, but you won't know for certain until you load it and see if it compiles. I've been thinking of building a balancing robot. I'd like to look over your shoulder as you progress. Keep us posted on your progress.

for(int speedvalue = 0 ; speedvalue <= 255; speedvalue +10) {
    analogWrite(MINA1, speedvalue);
    analogWrite(MINA2, speedvalue);
    delay(6000);
  
  for(int speedvalue = 255 ; speedvalue >= 0; speedvalue -10) {
    analogWrite(MINA1, speedvalue);
    analogWrite(MINA2, speedvalue);
    delay(6000);
  }
}

That loop nesting looks like it is going to take a while to execute. Is that what you intended, or did your indentation mislead you?

You cant use those pins (2,4,7,8) for your motors and analogWrite together. AnalogWrite uses the PWM pins (3,5,6,9,10,11), so what you have now will at best give you full forward and full reverse.

Just pointing that out.

HazardsMind: You cant use those pins (2,4,7,8) for your motors and analogWrite together. AnalogWrite uses the PWM pins (3,5,6,9,10,11), so what you have now will at best give you full forward and full reverse.

Just pointing that out.

Yeah i actually realized that just now so i re-wrote the code, this is what I have. And thank you to everyone who commented!

#include "DualVNH5019MotorShield.h"

DualVNH5019MotorShield md;

int M1EN = 6;
int M2EN = 12;

void setup() 
{
  md.init();
}

void loop()
{
  {  
  delay(2000);
  md.setM1Speed(400);
  pinMode(M1EN, OUTPUT);      // Sets the pin to an output.
  digitalWrite(M1EN, LOW);    // Makes pin low and disables motor.
  delay(1500);
  pinMode(M1EN, INPUT);       // Enables motor so it can run.
  }


  md.setM2Speed(-400); 
  pinMode(M2EN, OUTPUT);      // Sets the pin to an output.
  digitalWrite(M2EN, LOW);    // Makes pin low and disables motor.
  delay(1500);
  pinMode(M2EN, INPUT);       // Enables motor so it can run.
}

I think this will get the motors running in the same direction forward. Used another example code to get this and then modified it.

pmlapl: It looks correct to me, but you won't know for certain until you load it and see if it compiles. I've been thinking of building a balancing robot. I'd like to look over your shoulder as you progress. Keep us posted on your progress.

And cool! Ill keep posting here as a log. I know ill need more help soon.

I don't know anything about that library but, this does not look correct.

pinMode(M1EN, OUTPUT); // Sets the pin to an output. OK digitalWrite(M1EN, LOW); // Makes pin low and disables motor. OK delay(1500); ** OK I guess for a test, not good for full code** pinMode(M1EN, INPUT); // Enables motor so it can run. This does not look right, this sets the pin to be read, it does not set it HIGH to enable the motor.

Hmm okay, what if i added digitalWrite(M1EN, HIGH); after the last line? That should enable the motor right?

In replace of pinMode(M1EN, INPUT), it should.

Ok cool thanks Hazard!

Im ordering my stuff this week, and so once its here ill post up my progress. Should be about 1 working week for everything to get here, and ill probably get my bots body panels cut over the long weekend. Any recommendations on material? Theres not a lot of parts so cost isnt a problem. I was thinking maybe carbon fiber, maybe plexiglas-g, or even anodized aluminum. Ill decide this week!

Whatever materials you want to use it up to you, its your call. Personally I would start with wood, so if something breaks, you could easily replace it. But like I said its your call.

Don't forget to thank everyone else who helped too.

So just ordered my motors, driver, and my imu. I chose a sparkfun 3DOF acc_gyro imu. Ordered from robotshop though. No way im paying 30 bucks shipping and waiting 2-6 weeks for a package from the states. Robotshop has a distributor up in Montreal so 6 bucks shipping and itll be here in 2 business days. So by wednesday ill recieve it. Ill update then.

Shoot, i forgot to order batteries... :cold_sweat: