Results 1 to 3 of 3

Thread: [SOLVED] BrickOS Jitters

  1. #1
    Join Date
    Oct 2005
    Beans
    89

    [SOLVED] BrickOS Jitters

    Hey guys I was playing around with BrickOS and had a problem with this code:

    Code:
    #include <config.h>
    #include <dmotor.h>
    #include <dsensor.h>
    
    
    
    int main() 
    {
    
    while (1)
    	{
    
    		motor_a_dir(fwd);
    		motor_c_dir(fwd);
    
    		motor_a_speed(255);
    		motor_c_speed(255);
    
    		if (TOUCH_1 == 1)
    			{
    			 	motor_a_dir(rev);
    				motor_a_speed(0);	
    			}
    		else 
    			{
    				motor_a_dir(fwd);
    				motor_a_speed(255);
    			}
    	}
    
    return 0;
    }
    When I press the touch sensor the motor just jitters in the reverse direction rather than driving backwards at full speed. What am I doing wrong here?
    Last edited by HIGHLIFE; March 12th, 2008 at 04:43 AM.

  2. #2
    Join Date
    Oct 2007
    Beans
    1,914
    Distro
    Lubuntu 12.10 Quantal Quetzal

    Re: BrickOS Jitters

    Should be obvious. If you press the button then the code
    Code:
    {
       motor_a_dir(rev);
       motor_a_speed(0);	
    }
    is executed. Afterwards the end of the control loop is reached (the "while(1) {...}"), so the controller begins executing it from the beginning of the loop again, which starts with:
    Code:
    motor_a_dir(fwd);
    motor_c_dir(fwd);
    
    motor_a_speed(255);
    motor_c_speed(255);
    Afterwards the condition is checked again and
    Code:
    {
       motor_a_dir(rev);
       motor_a_speed(0);	
    }
    is again executed (and so on). This surely causes jitter since the direction of the motor is switched extremly often. You probably want:
    PHP Code:
    #include <config.h>
    #include <dmotor.h>
    #include <dsensor.h>

    int main() 
    {
       
    motor_a_dir(fwd);
       
    motor_c_dir(fwd);

       
    motor_a_speed(255);
       
    motor_c_speed(255);
       while (
    1)
        {

            if (
    TOUCH_1 == 1)
                {
                     
    motor_a_dir(rev);
                    
    motor_a_speed(0);    
                }
            else 
                {
                    
    motor_a_dir(fwd);
                    
    motor_a_speed(255);
                }
        }

    return 
    0;


  3. #3
    Join Date
    Oct 2005
    Beans
    89

    Re: BrickOS Jitters

    Haha thanks.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •