NJ, USA
Offline
Newbie
Karma: 0
Posts: 23
|
 |
« Reply #30 on: February 01, 2013, 08:32:19 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Pittsburgh, PA, USA
Offline
Faraday Member
Karma: 31
Posts: 2947
I only know some basic electricity....
|
 |
« Reply #31 on: February 01, 2013, 09:27:29 pm » |
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.
|
|
|
|
|
Logged
|
Examples can be found at Learning in the Main Site and at the Playground
|
|
|
|
NJ, USA
Offline
Newbie
Karma: 0
Posts: 23
|
 |
« Reply #32 on: February 01, 2013, 09:31:29 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
|
|
East Anglia (UK)
Online
Edison Member
Karma: 49
Posts: 1435
May all of your blinks be without delay
|
 |
« Reply #34 on: February 02, 2013, 03:18:05 am » |
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 ?
|
|
|
|
|
Logged
|
|
|
|
|
NJ, USA
Offline
Newbie
Karma: 0
Posts: 23
|
 |
« Reply #35 on: February 02, 2013, 08:47:32 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Temple, Texas
Offline
Sr. Member
Karma: 14
Posts: 354
|
 |
« Reply #36 on: February 02, 2013, 10:30:47 am » |
Now that you know both motors are working you could do something like this to test the bumper switches: /* 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 // 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
|
|
|
|
|
Logged
|
|
|
|
|
NJ, USA
Offline
Newbie
Karma: 0
Posts: 23
|
 |
« Reply #37 on: February 12, 2013, 07:47:03 am » |
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/VQMzDsand on youtube /* 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
|
|
|
|
« Last Edit: February 12, 2013, 08:38:24 am by OmegaRa »
|
Logged
|
|
|
|
|
East Anglia (UK)
Online
Edison Member
Karma: 49
Posts: 1435
May all of your blinks be without delay
|
 |
« Reply #38 on: February 12, 2013, 11:18:37 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
NJ, USA
Offline
Newbie
Karma: 0
Posts: 23
|
 |
« Reply #39 on: February 14, 2013, 08:15:16 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
East Anglia (UK)
Online
Edison Member
Karma: 49
Posts: 1435
May all of your blinks be without delay
|
 |
« Reply #40 on: February 14, 2013, 01:49:17 pm » |
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 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 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 !
|
|
|
|
|
Logged
|
|
|
|
|
NJ, USA
Offline
Newbie
Karma: 0
Posts: 23
|
 |
« Reply #41 on: February 14, 2013, 06:40:51 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
|