Outwitting Arduino's Control Loop
#1
Posted 31 January 2013 - 06:51 PM
To start, I decided to replace the $50 circuit board that students have to buy themselves with an Arduino Uno. The software for the previous board was complicated to install (the lab instructors end up having to install it on everyone's laptop) and used C when students are taught C++ in first year. Arduino is a great improvement, it's far simpler to install and the programming has been easy thus far.
So far it's been great, but I am stuck on one part. I want to create a program where the robot follows a wall for a certain distance and then stops. I can make it follow a wall fine, but for some reason, the logic to make the thing stop just escapes me.
Arduino has a control loop, but I don't want my program to loop. I want it to run, and then just sit there and wait for me to unplug it, basically.
The only solution I've found is to create a stop function which sets all the outputs to low to stop the motor, and put my function call in it's own loop that takes a long time to get through. That doesn't last forever though. Is there a permanent solution I'm missing?
#2 Guest_ElatedOwl_*
Posted 31 January 2013 - 08:20 PM
something like
start loop detect wall (I have no idea what your hardware is) declare distance as 0 while distance < desired distance // track distance moved here, I'm assuming you have a way to see track rotations or whatever distance += whatever distanced traveled end end
But, again, I don't really ever get the chance to play with low level hardware interactions like this.
EDIT: Did a little bit of googling, do you write your programs in Java for this? o-o;
If you can post some of the actual code I can probably help you out
#3
Posted 02 February 2013 - 08:39 AM
I'll just post a bit of an explanation, Arduino programs in C++ and everything must be in a function called loop(). I realized I just could have used a while(1) look to make the thing stop forever after I was done, but I think what I did is a neater approach anyway. I have the robot check if there's anything in front, and then I have a counter defined outside the loop function. So it will follow the wall for 1m unless something is in front. Then it will wait for someone to remove the obstacle and continue. If I use condition checks before it starts instead of checks while it's already moving, I don't need a stop function.
But yeah, sorry for this topic.
void loop() { int frontSensorReads = readFrontSensor(); while (frontSensorReads < 500) { // While nothing is in front of the robot if (stepCounter < (100*(500/30)) ){ // Only run for a certain distance Roughly 30cm/500 steps int rightSensorReads = readRightSensor(); frontSensorReads = readFrontSensor(); if (rightSensorReads > 500){ // If too close to the wall, turn away turnLeft(15); stepCounter += 15; for (int i=0; i < 100; i++) { goForward(); stepCounter++; } } else if (rightSensorReads < 450){ // If too far away from the wall, turn closer turnRight(15); stepCounter += 15; for (int i=0; i < 100; i++){ goForward(); stepCounter++; } } else { goForward(); stepCounter++; } } } }
#4 Guest_ElatedOwl_*
Posted 02 February 2013 - 10:28 AM
#5
Posted 02 February 2013 - 10:59 AM
#6
Posted 02 February 2013 - 11:41 AM
#7
Posted 08 February 2013 - 09:36 AM
#8
Posted 08 February 2013 - 10:18 AM
The other one I'm making for fun is pretty hard to do, but it's a little model Frankenturret. When I finish this I may make a life-sized one, but the problem is it actually operates, and could cause mayhem if not programmed properly (the life-sized one).
#9
Posted 08 February 2013 - 10:28 AM
Simple robotics is actually really easy.
As for the assembly, I can't tell you how difficult that was. The robot I was working with was already put together, I just had to replace one board with another and learn how to work with the code. From what I remember when I took the course last year, putting the robot together was easy. It comes in a kit the faculty put together that the first year students have to assemble themselves. It'd be super easy to make your own if you had access to the right materials
I'll post a photo of it later. Technically right now I'm posting at work, which is a bad thing I guess.
#10 Guest_ElatedOwl_*
Posted 08 February 2013 - 10:30 AM
Pfffffffffffffffffffffffffffffffffffffffft. As long as the work gets done does it entirely matter what you do to fill your 8 hours?I'll post a photo of it later. Technically right now I'm posting at work, which is a bad thing I guess.
#11
Posted 08 February 2013 - 10:40 AM
I guess I am just talking to myself >.<
#12
Posted 08 February 2013 - 12:00 PM
#13
Posted 08 February 2013 - 02:41 PM
The hardest part for me was getting the motors to work smoothly. I had stepper motors so I needed the code to alternate between each motor after one step. You can't have one wheel make a full cycle and then look at the other. It took me a little bit to figure out how to go about that.
EDIT: https://dl.dropbox.c...12/IMAG0158.jpg Sorry for the large image, too lazy to resize.