Jump to content

Photo

Outwitting Arduino's Control Loop


  • Please log in to reply
12 replies to this topic

#1 SushiKitten

SushiKitten

    Coffee Cat

  • Members
  • 1,916 posts
  • LocationCanada

Posted 31 January 2013 - 06:51 PM

So for my job, I have to work with some different robots and hardware to come up with a set of robotic/programming labs and/or improve the first year robot competition for the first year engineering students.

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_*

Guest_ElatedOwl_*
  • Guests

Posted 31 January 2013 - 08:20 PM

I've never touched an Arduino and it's very rare that I do any kind of low level programming, but don't you need it to sit inside a loop? That's (generally speaking) how most software works.

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 SushiKitten

SushiKitten

    Coffee Cat

  • Members
  • 1,916 posts
  • LocationCanada

Posted 02 February 2013 - 08:39 AM

I should never post help topics in coding, because after I post them, i obsess over it and get to an answer myself before anyone can help me anyway.

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_*

Guest_ElatedOwl_*
  • Guests

Posted 02 February 2013 - 10:28 AM

Neat. Is readRightSensor/readFrontSensor just an API call or do you actually talk to it?

#5 SushiKitten

SushiKitten

    Coffee Cat

  • Members
  • 1,916 posts
  • LocationCanada

Posted 02 February 2013 - 10:59 AM

The sensors work just like a potentiometer, it's giving off a certain voltage depending on how close the wall/obstacle is, so all readRightSensor/readFrontSensor is doing is looking at Arduino's analog to digital converter and returning the value depending on what voltage the sensor's giving off.

#6 Coconut Man

Coconut Man

    Gigabyte

  • Members
  • 798 posts
  • LocationThe latest Smash Major

Posted 02 February 2013 - 11:41 AM

This reminds me of the lego robots you could build and then program them to run various tasks.

fl9Uov4.gif


#7 SpleenBeGone

SpleenBeGone

    Deer Leader of the Goriest Revolution

  • Administrators
  • 14,951 posts
  • LocationHouston

Posted 08 February 2013 - 09:36 AM

So I was sweeping the floor at work yesterday, and I thought of this thread. How hard was it to make your little robot? I'm thinking something pretty much the exact same, just beefy enough to put an industrial sized dust mop on and let it loose in the building.
nmjUGDL.jpg

#8 Coconut Man

Coconut Man

    Gigabyte

  • Members
  • 798 posts
  • LocationThe latest Smash Major

Posted 08 February 2013 - 10:18 AM

The lego one was insanely easy to make.

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).

fl9Uov4.gif


#9 SushiKitten

SushiKitten

    Coffee Cat

  • Members
  • 1,916 posts
  • LocationCanada

Posted 08 February 2013 - 10:28 AM

That would be hilarious, like a DIY roomba but with a dust mop.

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_*

Guest_ElatedOwl_*
  • Guests

Posted 08 February 2013 - 10:30 AM

I'll post a photo of it later. Technically right now I'm posting at work, which is a bad thing I guess.

Pfffffffffffffffffffffffffffffffffffffffft. As long as the work gets done does it entirely matter what you do to fill your 8 hours?

#11 SIlhouette

SIlhouette

    Megabyte

  • Members
  • 383 posts

Posted 08 February 2013 - 10:40 AM

Lol, I did electionics all the way through high school, my solution to this problem is to create more hardware :D Simple circuit loop (stop loop > certain voltage > stop loop) so if it detects nothing it will stop and if it detects something big it will stop and at all other times it will follow. Pot / +1v mean -> transistor to + rail or - rail -> Cap as a timer maybe? else stop? might even be worth using a variable transistor for fine tuning...

I guess I am just talking to myself >.<

#12 SpleenBeGone

SpleenBeGone

    Deer Leader of the Goriest Revolution

  • Administrators
  • 14,951 posts
  • LocationHouston

Posted 08 February 2013 - 12:00 PM

The robot itself would be easy, as I'm pretty good at the physical building of things.
nmjUGDL.jpg

#13 SushiKitten

SushiKitten

    Coffee Cat

  • Members
  • 1,916 posts
  • LocationCanada

Posted 08 February 2013 - 02:41 PM

Then programming the Arduino or whatever board you choose shouldn't be too difficult either. It only took me about a week to go from getting an led to blink to having it follow the wall as long as certain conditions are true. Arduino's coding is basically just C++. The IDE for Arduino is very small and simple, but there are alternatives if you don't like it.

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.