This is an old revision of the document!


Lahendused harjutustele

Harjutus 2.1
#include <pololu/3pi.h>
 
int main()
{
  delay_ms(3000);
    while(1)
    {
      for(int i = 0; i < 4; i++){
        set_motors(50,50);
        delay_ms(500);
        set_motors(0,50);
        delay_ms(680);
      }
      set_motors(0,0);
      delay_ms(5000);
    }
}
Harjutus 2.2
#include <pololu/3pi.h>
 
int main()
{
	delay_ms(3000);
	while(1)
	{
 
		set_motors(50,50);
		delay_ms(500);
		set_motors(0,50);
		delay_ms(2200);
		set_motors(50,50);
		delay_ms(500);
		set_motors(50,0);
		delay_ms(2200);	
		set_motors(0,0);
		delay_ms(2000);
        }
}
Harjutus 3.1
#include <pololu/3pi.h>
 
int main()
{
	int count;
	while(1)
	{
 
		count = analog_read(TRIMPOT)/128; //ümardub täisarvuks
		clear();
		for(int i = 0; i < count; i++){
		print_character('|');
		}
		lcd_goto_xy(0,1);
		print("C: ");
		print_long(count);
		delay_ms(300);
	}
}
Harjutus 3.2
#include <pololu/3pi.h>
 
int main()
{
	char text[] ="Hello";
	while(1)
	{
 
		for(int i = 4; i >= 0; i--){
			clear();
			for(int j = i; j <= 4; j++){
				print_character(text[j]);
 
			}
			delay_ms(200);
		}
		lcd_scroll(LCD_RIGHT, 8, 250);
	}
}
Harjutus 4.1
#include <pololu/3pi.h>
#include <stdio.h>
 
int main()
{
	lcd_init_printf();
	int count[] = {0, 0, 0};
	unsigned long time;
 
		time = get_ms();
		while(1)
		{
			unsigned char button = get_single_debounced_button_press(ANY_BUTTON);
			if (button & TOP_BUTTON) count[2]++;
			if (button & MIDDLE_BUTTON) count[1]++;
			if (button & BOTTOM_BUTTON) count[0]++;
			if(get_ms() - time > 100){
				 clear();
				 printf("A%2d B%2d\nC%2d", count[0], count[1], count[2]);
				time = get_ms();
			}
		}
}
Harjutus 4.2
#include <pololu/3pi.h>
 
int main()
{
 
		while(1)
		{
			unsigned char button = get_single_debounced_button_press(ANY_BUTTON);
			if (button & TOP_BUTTON) stop_playing();
			if (button & BOTTOM_BUTTON){
				if(!is_playing()) play("!L16 V15 cdefgab>cbagfedcdefgab>cbagfedcdefgab>cbagfedc");
			}
		}
}
3Pi PID regulaatoriga joonejärgimine
// This is the main function, where the code starts.  All C programs
// must have a main() function defined somewhere.
int main()
{
    unsigned int sensors[5]; // an array to hold sensor values
    unsigned int last_proportional=0;
    long integral=0;
    currentIdx = 0;
 
    // set up the 3pi
    initialize();
    //int val =0;
    //char lisa=0;
    int x=18;       //prop
    int y=0;        //int
    int z1=4,z2=1;      //deriv
    int max = 150;
    int butp = 0;
    /*  pval = eeprom_read_byte((uint8_t*)10);
        ival = eeprom_read_byte((uint8_t*)11);
        dval1 =eeprom_read_byte((uint8_t*)12);
        dval2 =eeprom_read_byte((uint8_t*)13);*/
    while(1)
    {
 
        if(butp ==0)
        {
            if(button_is_pressed(BUTTON_C))
            {
                x++;
            }
            else if(button_is_pressed(BUTTON_A))
            {
                x--;
            }
            print("Proport: ");
            lcd_goto_xy(0,1);
            print_long(x);
            delay_ms(100);
        }
        else if(butp==1)
        {
            if(button_is_pressed(BUTTON_C))
            {
                y++;;
            }
            else if(button_is_pressed(BUTTON_A))
            {
                y--;
            }
            print("Integral: ");
            lcd_goto_xy(0,1);
            print_long(y);
            delay_ms(100);
        }
        else if(butp==2)
        {
            if(button_is_pressed(BUTTON_C))
            {
                z1++;
            }
            else if(button_is_pressed(BUTTON_A))
            {
                z1--;
            }
            print("D ?/Z2: ");
            lcd_goto_xy(0,1);
            print_long(z1);
            delay_ms(100);
        }
        else if(butp==3)
        {
            if(button_is_pressed(BUTTON_C))
            {
                z2++;
            }
            else if(button_is_pressed(BUTTON_A))
            {
                z2--;
            }
            print("D Z1/?: ");
            lcd_goto_xy(0,1);
            print_long(z2);
            delay_ms(100);
        }
        else if(butp==4)
        {
            if(button_is_pressed(BUTTON_C))
            {
                max++;
            }
            else if(button_is_pressed(BUTTON_A))
            {
                max--;
            }
            if(max >= 255)
            {
                max = 255;
            }
            if(max <= 20)
            {
                max = 20;
            }
            print("Speed: ");
            lcd_goto_xy(0,1);
            print_long(max);
            delay_ms(50);
        }
 
        else if(butp > 4) break;
        if(button_is_pressed(BUTTON_B))
        {
            butp++;
            delay_ms(200);
        }
        clear();
    }
 
    clear();
    print("  3Pi");
    lcd_goto_xy(0,1);
    print("  118");
 
    // This is the "main loop" - it will run forever.
    while(1)
    {
        // Get the position of the line.  Note that we *must* provide
        // the "sensors" argument to read_line() here, even though we
        // are not interested in the individual sensor readings.
        unsigned int position = read_line(sensors,IR_EMITTERS_ON);
 
        // The "proportional" term should be 0 when we are on the line.
        int proportional = ((int)position) - 2000;
 
        // Compute the derivative (change) and integral (sum) of the
        // position.
        int derivative = proportional - last_proportional;
        integral += proportional;
 
        // Remember the last position.
        last_proportional = proportional;
 
        // Compute the difference between the two motor power settings,
        // m1 - m2.  If this is a positive number the robot will turn
        // to the right.  If it is a negative number, the robot will
        // turn to the left, and the magnitude of the number determines
        // the sharpness of the turn.
        int power_difference = proportional/x + integral/y + derivative*(z1/z2);
 
        // Compute the actual motor settings.  We never set either motor
        // to a negative value.
 
        if(power_difference > max)
            power_difference = max;
        if(power_difference < -max)
            power_difference = -max;
 
        if(power_difference < 0)
            set_motors(max+power_difference, max);
        else
            set_motors(max, max-power_difference);
    }
 
    // This part of the code is never reached.  A robot should
    // never reach the end of its program, or unpredictable behavior
    // will result as random code starts getting executed.  If you
    // really want to stop all actions at some point, set your motors
    // to 0,0 and run the following command to loop forever:
    //
    // while(1);
}
et/projects/3pi/solutions.1447331282.txt.gz · Last modified: (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0