tags:

views:

224

answers:

4

So my partner and I need some help; we are working on a final project which is for an electrical engineering degree. Our problem, no one really knows much programming in the class. We need help or some general ideas on how to program this.

The project:

We have a monster truck with two IR (infra red) sensors detecting it's path via voltage. with this we are using a free scale circuit board as the "brains" along with this we have a nerf "missle turret" to shoot through open doors (we need to do programming to note a voltage drop from the IR sensors.

The programming:

We are trying and struggling very, very hard to create the code for this we

  1. need a delay function in the start of the program to set the monster truck on the ground, after we need it to follow a straight line from a set standard of voltage that the IR sensors are reading on the ground.
  2. while it is going it will have a large V drop reading while it passes an open door, we need it to stop shoot and then follow it's path.

I know it is a lot to ask but we are in a large need for help, our teacher while wise beyond his years in everything electrical (reminds me of doc from back to the future) the c programming is lacking and not too many here have the massive knowledge to use the C programming skills. finally, once we get this working (mid may) i will post video if able too and show you all. I appreciate any input and any ideas for this, thank you all for your time!!

A: 

Most universities have a programming club (i know mine did) - why not hang around there, see if anyone wants to help? Or even put something on the noticeboard around the computer rooms at uni, i'm sure most good programmers would find it a fun project to lend a bit of time to.

Being that it is a physical project you've got there, you really want someone there 'on the ground' with you, so to speak.

Chris
+5  A: 

Your code will probably look like:

  // Give yourself some time to set the robot down
  <sleep_for_some_interval>;

  // Keep reading the sensors and reacting until
  // some amount of time passes, or a button is pressed, etc.
  while(<keep_running_condition>)
  {
     // Update sensor readings
     int leftDist = <ConvertToDistance>(<read_left_voltage>);
     int rightDist = <ConvertToDistance(<read_right_voltage>);

     // React to sensor readings
     if(leftDist > <door_threshold> && 
        rightDist > <door_threshold>)
     {
         // A door has been detected.
         <stop>
         <shoot>
         <move_forward_fixed_amount>
     } 
     else if(leftDist > <turn_threshold>) 
     {
         // Left distance is beyond the threshold, 
         // need to turn left
         <turnLeft>;
     } 
     else if(rightDist > <turn_threshold>) 
     {
         // Right distance is beyond the threshold,
         // need to turn right
         <turnRight>
     } 
     else (<terminate_condition>) 
     {
         // Something happened (a sensor condition, etc)
         // that tells us that we need to end
         // the program
         break; // This will exit the while loop
     } 
     else (...) 
     {
         // Other conditions...
     } 
     else 
     {
         // Default reaction (happens if none of the previous 
         // conditions are met)
         <goForward>
     }
  }

  // Shutdown the robot
  <stop>
  // ...

Obviously the comparisons may need to be different, but the basic idea will be to continually read your sensors, and then have a list of conditions to check and actions to take when conditions are met.

Notes/Hints:

  • To "delay ... in the start of the program" you will call something like sleep/usleep
  • Make sure that you check your conditions in the right order (check the most restrictive conditions first) In the above example, if I moved the "turnLeft" check to the top, I might end up turning left instead of reacting to the door.
  • The way the above code is written, only one reaction will happen on each pass through the loop. (This will prevent you from accidentally trying to do two things when you should only be doing one, e.g. turning left and firing)
  • It may be helpful to write a function that can convert a voltage to a distance
  • Your code will be cleaner and easier to maintain if you take complicated actions (like the reaction after you detect a door) and move them into their own function.
  • Make sure to use some form of revision control, even if you do nothing more than periodically zip up your development directory. It will be very helpful when you find that you've introduced a bug into your code.
Daniel LeCheminant
+3  A: 

Out of curiosity, is there a reason you can't do all this in hardware? I'm assuming a lot about your project, but I'd think at least #1 might even be easier to do in hardware if that's your area of expertise. Unless, of course, you have instructions to do this in software that you must obey.

If the IR sensors are already outputting variable voltage, couldn't you just scale the voltage output from the sensors to drive the motors? I'm assuming the wheels are driven by motors and you're spinning one faster than the other to get it to turn. Then you might even get a smoother driving pattern (turning/adjusting as you go), as opposed to the standard software solution, which is go-or-turn.

For #2, you could implement a state machine. This could take care of the delay as well, just throw a timer between state 0 (waiting) and state 1 (normal). Then use the voltage output from the door sensor to trigger state 2 (firing). If you need, you can split it into two states, 2 (arming) and 3 (firing), with another timer between the two. The wheel motors are only active in state 1, and release the missile trigger when state 3 occurs.

lc
+1 for state machines
Daniel LeCheminant
A: 

To do the delay you can write a function like the following

 void spin_wait(U16 cycles)
 {
     while(cycles>16) // 16 cycles consumed by overhead           
     { 
        cycles -=  6;      // 6 cycles consumed each iteration
     }
 }

You will need to adjust the two parameters for your processor and clock rate. You will also get different time depending on what level of optimization you are using.

Since your processor cannot directly read a voltage it is doing one of two things either an ADC (analog to digital converter) will convert it to an integer level or the voltage is a logic level going directly to an input. In that case you would just read the port and mask off the bit and monitor for it to change state. For the ADC case you would poll the ADC register and then just do a comparison to a level or you can average the ADC and look for a deviation.

Your main function will look something like the following

#define THRESHOLD ( 127)   // set to proper level

void main( void )
{
   U16 i;
   U8 sen;
   sfrInit();  // function to init registers

   for ( i=0; i<1000;i++) // 10 second delay
   {
       spin_wait(0x2710);  // 10ms delay
   }

   // polling loop break all actions into small steps and 
   // then repeat a bunch of times
   for (;;)   
   {
       sen=ADC;
       if ( sen < THRESHOLD )
       {
            turn();
            shoot();
       }
       move_forward();  // causes you to go forward by a certain distance      
}
Rex Logan
i appricate all the help and input thank you very much, the clubs that i can go to are not around when i go (we both work days and goto school late at night) thank you all so much for the help i will let you k now what happens!