Go Down

Topic: Question about my Sketch. (Read 2189 times) previous topic - next topic



I would recommend removing the indicated *** lines in setup,  Compile and run, the motor should go on 4 seconds and then off 4 seconds, on 4, off 4.  Then in loop() just change the leftMotors()s to rightMotor().  You're just making sure the motor works here.
Also see if you can add the lines in the loop, to test the reverse:
  right fwd full
  delay
  right fwd 0    (stop)
  delay
  right reverse full
  delay

Modify for both motors fwd, 1 fwd 1 reverse, half speed (128), etc.

Make sure you understand the concept that setup runs once, first-- and loop then runs forever, over and over.

Cheers,
John


Okay, I did all that and got the motors working, not both at the same time, but I got each motor to go forward and backward, and switching back and forth.

GoForSmoke

You could bury a powered cable under the lawn and use a magnetic field sensor or two on the mower to follow that like a toy line-follower robot. You'd get a perfect path every time regardless of wheel slip, etc. No map or mapping required, just one long piece of single conductor wire and some time making and filling very thin, shallow trenches.

Could probably run something like that with a Tiny.

Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts


You could bury a powered cable under the lawn and use a magnetic field sensor or two on the mower to follow that like a toy line-follower robot. You'd get a perfect path every time regardless of wheel slip, etc. No map or mapping required, just one long piece of single conductor wire and some time making and filling very thin, shallow trenches.

Could probably run something like that with a Tiny.




This is more or less the plan eventually. Right now I am just going to use it in my backyard, which is fully fenced in, so I don't need sensors yet, just bumpers to make it turn around should it run into anything.

here are links to a couple pictures of what it looks like so far

http://sdrv.ms/14EWuxa

http://sdrv.ms/14EWqxI

http://sdrv.ms/14EWyNz

I have to properly mount the Board and give it some paint...but overall that is it, minus it's covers

UKHeliBob


Okay, I did all that and got the motors working, not both at the same time, but I got each motor to go forward and backward, and switching back and forth.

Did they fail to run at the same time or was that how you programmed it ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.



Okay, I did all that and got the motors working, not both at the same time, but I got each motor to go forward and backward, and switching back and forth.

Did they fail to run at the same time or was that how you programmed it ?


That was just how I programmed it to test. I tried adding the other motor in after and got both working.

johncc

Now that you know both motors are working you could do something like this to test the bumper switches:

Code: [Select]

/* Bumpers */
const int LEFT_BUMP = A2, RIGHT_BUMP = A3;
boolean bump(int pin) {
// there's fancier ways but for now you will understand this ...
if (digitalRead(pin) == LOW)    // switches are wired to ground
return true;
else
return false;
}


and in loop do something like
Code: [Select]

// test the bumper switches
void loop() {
if (bump(LEFT_BUMP)){
leftMotor(FORWARD, FULLSPEED);  // left motor on
rightMotor(FORWARD, 0);         // right off
}
else if (bump( RIGHT_BUMP)){
// right motor on, left off
...
}
}


Remember to set INPUT_PULLUP in setup()...

Cheers,
John

#37
Feb 12, 2013, 01:47 pm Last Edit: Feb 12, 2013, 02:38 pm by OmegaRa Reason: 1
Okay guys here is my final sketch :) I have a video of it working too, but it is still uploading. I will link to it later when it is done. I need to head to work soon.

EDIT: here is a link to the video on my Skydrive. http://sdrv.ms/VQMzDs
and on youtube http://www.youtube.com/watch?v=KYJl1B1y6bo

Code: [Select]
/*
  Mowbot Sketch
*/
const int RIGHT_MOTOR=13, RIGHT_BRAKE=8, RIGHT_SPINNER=11;
const int LEFT_MOTOR=12, LEFT_BRAKE=9, LEFT_SPINNER=3;
const int FORWARD=HIGH, REVERSE=LOW;
const int FULLSPEED=255;

void leftMotor( int direction, int speed)
{
 digitalWrite( LEFT_MOTOR, direction); //Establishes direction of Channel A
 digitalWrite( LEFT_BRAKE , LOW);      //Disengage the Brake
 digitalWrite( LEFT_SPINNER, speed);   //Spins the motor
}
void rightMotor( int direction, int speed)
{
 digitalWrite( RIGHT_MOTOR, direction); //Establishes direction of Channel B
 digitalWrite( RIGHT_BRAKE , LOW);      //Disengage the Brake
 digitalWrite( RIGHT_SPINNER, speed);   //Spins the motor
}
/* Bumpers */
const int LEFT_BUMP = A2;
const int RIGHT_BUMP = A3;

boolean bump(int pin) {
 // there's fancier ways but for now ...
 if (digitalRead(pin) == LOW)
   return true;
 else
   return false;
}

void setup()
{
 pinMode(LEFT_MOTOR, OUTPUT);  
 pinMode(LEFT_BRAKE, OUTPUT);
 pinMode(RIGHT_MOTOR, OUTPUT);
 pinMode(RIGHT_BRAKE, OUTPUT);
 pinMode(LEFT_BUMP, INPUT_PULLUP);
 pinMode(RIGHT_BUMP, INPUT_PULLUP);

}
void loop()
{
 int randNumber = random(900, 2500);

 if (bump(LEFT_BUMP))            // left bumper hit
 {
   leftMotor(REVERSE, FULLSPEED);  // backup
   rightMotor(REVERSE, FULLSPEED);
   delay(1500);
   leftMotor(REVERSE, FULLSPEED);  // turn
   rightMotor(FORWARD, FULLSPEED);
   delay(randNumber);       // a random amount
 }
 else if (bump(RIGHT_BUMP))
 {
   leftMotor(REVERSE, FULLSPEED);
   rightMotor(REVERSE, FULLSPEED);
   delay(1500);
   rightMotor(REVERSE, FULLSPEED);
   leftMotor(FORWARD, FULLSPEED);
   delay(randNumber);
 }
 else
 {
   leftMotor(FORWARD, FULLSPEED);
   rightMotor(FORWARD, FULLSPEED);
 }
}  // then back to loop(), over and over

UKHeliBob

Congratulations on getting it working.  Your code is fairly easy to read but I would suggest making each of the sets of motor controls into a function, each with a meaningful name.  Some of the functions, such as  the ones for forward and reverse would be called more than once and using functions would make it easier to understand the flow of the code.  A function to make the mower spin in a random direction could also help to mix up the movement a little too.

Another point, it is not very kind to the motors to go from full speed forward to full speed backwards without a period of time when they are stopped.  It would be even kinder to the motors to bring them up to full speed over a period of time rather than instantaneously, but that can be added to your present code.  You could do this inside the functions that I have suggested using as there would then only be one place to implement the changes.

Have you tried the mower on grass yet ?  The motor driving the blade looks too small to deliver much power, but maybe not.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

as it is winter, there is no grass to cut sadly. But once the snow melts and it warms up a bit I will try it out on the grass. The blade I made is pretty lite and it spins very quickly on the motor, we'll see what happens when there is a little resistance with grass. As for your other points, they make sense, but I don't really know anything about coding to be able to properly implement them. It is something I can work on in the future, but for now I Am just glad I got it working.

UKHeliBob

You are right to be pleased that it works but I am fairly sure that it won't cut the grass, but we shall see.

As to my suggestion to use functions, here is a modified portion of your code that illustrates the idea
Code: [Select]
  else if (bump(RIGHT_BUMP))
  {
    backup(FULLSPEED);
    delay(1500);
    spinRight(FULLSPEED);
    delay(randNumber);
  }
  else
  {
    forward(FULLSPEED);
  }
}  // then back to loop(), over and over

void forward(int speed)
{
  leftMotor(FORWARD, speed);
  rightMotor(FORWARD, speed);
}

void backup(int speed)
{
  leftMotor(REVERSE, speed);
  rightMotor(REVERSE, speed);
}

void spinRight(int speed)
{
  rightMotor(REVERSE, speed);
  leftMotor(FORWARD, speed);
}

To my mind it is easier to understand what is going and make the loop() function less cluttered.  You could take the idea even further and combine these lines
Code: [Select]
    backup(FULLSPEED);
    delay(1500);
    spinRight(FULLSPEED);
    delay(randNumber);

into a function called, perhaps avoidRight(), and call it when you want to back up and spin right.  How far you take it is up to you. 
Creating functions like these also allows you to copy them to other programs without the need to write the code again.

What next ?
A robot vacuum cleaner perhaps.  Yes, I do know that they are already available !
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

I Can give that a try at some point, as for the robot vacuum, I already own a roomba 550 and an older 400 series. The 400 series has non-functional brush motor, but other than that it works, was thinking of converting it into something.

Go Up