Baby O Motor Control Issue

Hi Guys,

I’m having an issue with controlling two motors using a Baby O, USB AVR programmer, Atmel Studio six and a benchtop power supply set at 6V. What I’m trying to accomplish is to control the direction of each motor separately by using external switches connected to the I/O lines of the controller. These switches are connected to 0V, so for example when I operate the switch connected to IO_C1, I want to rotate my crane clockwise. However when I operate the switches connected to IO_C1, C2 and C3 the motors operate at a very reduced speed and I’m only seeing 0.2-0.4V at the motor. When I switch on IO_C4 it operates fine. The baby O is currently sitting in a breadboard and I’ve checked the operation of the motors as well and they seem ok. This is my first ever robotics/programming venture so I’m pretty sure it’s to do with my code (which involved a lot of trial and error and cut and paste from examples). Any help would be gladly appreciated.

#include <pololu/orangutan.h>

void rotate_crane_clockwise()
{
	set_m1_speed(100);
	set_m2_speed(0);
}

void rotate_crane_anticlockwise()
{
	set_m1_speed(-100);
	set_m2_speed(0);
}

void extend_dolly()
{
	set_m1_speed(0);
	set_m2_speed(150);
}

void retract_dolly()
{
	set_m1_speed(0);
	set_m2_speed(-150);
}

void stationary()
{
	set_m1_speed(0);
	set_m2_speed(0);
}


int main()

{
	set_digital_input(IO_C1,PULL_UP_ENABLED);
	set_digital_input(IO_C2,PULL_UP_ENABLED);
	set_digital_input(IO_C3,PULL_UP_ENABLED);
	set_digital_input(IO_C4,PULL_UP_ENABLED);
	
	while(1)
{
	{if (is_digital_input_high(IO_C1))
	{stationary();
	} 
	else
	{rotate_crane_clockwise();
	}	
}
		
	{
		if (is_digital_input_high(IO_C2))
		{stationary();
		} 
		else
		{rotate_crane_anticlockwise();
		}
	}
	
		{
			if (is_digital_input_high(IO_C3))
			{stationary();
			} 
			else
			{extend_dolly();
			}
		}		
		
			{
				if (is_digital_input_high(IO_C4))
				{stationary();
				} 
				else
				{retract_dolly();
				}
			}
			
	}
}

Cheers

Justin

Hello, Justin. Your indentation is very confusing. I think Atmel Studio 6 probably has a feature to help you out with that. Here is how your main function would look with better indentation:

int main()
{
   set_digital_input(IO_C1,PULL_UP_ENABLED);
   set_digital_input(IO_C2,PULL_UP_ENABLED);
   set_digital_input(IO_C3,PULL_UP_ENABLED);
   set_digital_input(IO_C4,PULL_UP_ENABLED);
   
   while(1)
   {
      {
         if (is_digital_input_high(IO_C1))
         {
            stationary();
         } 
         else
         {
            rotate_crane_clockwise();
         }   
      }
      
      {
				if (is_digital_input_high(IO_C2))
				{
					stationary();
				} 
				else
				{
					rotate_crane_anticlockwise();
				}
      }
   
      {
         if (is_digital_input_high(IO_C3))
         {
            stationary();
         } 
         else
         {
            extend_dolly();
         }
      }      
      
      {
         if (is_digital_input_high(IO_C4))
         {
            stationary();
         } 
         else
         {
            retract_dolly();
         }
      }
   }
}

Now we can simplify it by removing the braces that weren’t doing anything:

int main()
{
	set_digital_input(IO_C1,PULL_UP_ENABLED);
	set_digital_input(IO_C2,PULL_UP_ENABLED);
	set_digital_input(IO_C3,PULL_UP_ENABLED);
	set_digital_input(IO_C4,PULL_UP_ENABLED);
   
	while(1)
	{
		if (is_digital_input_high(IO_C1))
		{
			stationary();
		} 
		else
		{
			rotate_crane_clockwise();
		}   
      
		if (is_digital_input_high(IO_C2))
		{
			stationary();
		} 
		else
		{
			rotate_crane_anticlockwise();
		}
   
		if (is_digital_input_high(IO_C3))
		{
			stationary();
		} 
		else
		{
			extend_dolly();
		}      
      
		if (is_digital_input_high(IO_C4))
		{
			stationary();
		} 
		else
		{
			retract_dolly();
		}
	}
}

Now suppose you press the switch connected to C2, but all other switches are not pressed. Your main loop will effectively be:

while(1)
{
  stationary();
  rotate_crane_anticlockwise();
  stationary();
  stationary();
}

Your main loop would be constantly setting M1 to different, conflicting values, so you are not going to get M1 to move at the speed you want it to. You should restructure your program so that you only call one of your motor control functions on each iteration of the loop. Does this make sense?

–David

Thanks for the quick reply David. That does make sense, I just need to figure out how to do that. I’ll go and play around with the coding some more.

Thanks again.

Justin