Home Page
+ Reply to Thread
Results 1 to 3 of 3

Thread: Programming the iRobot Create

Hybrid View

  1. #1

    Contraption Programming the iRobot Create

    iRobot Create Programming

    I'm currently taking an Embedded Systems class at my university in which we get to program microcontrollers. In our lab we use the iRobot Create platform with the Cerebot II board, which looks a lot like one of those autonomous vacuum cleaners. We have 9 different structured labs in which we have to program the iRobot to do something. Then we have our final project, in which we have to program the robot to be FULLY autonomous and navigate through a course. It has to be able to get from the starting point to the finish, while traversing through many obstacles and staying within the borders of the course.



    This thread will be kind of like a blog in the sense that I'll be posting my various labs so you can check out the code if you're interested in getting into iRobots or microcontroller. They are a lot of fun. I'll try my best to get a video of the robot doing each project, but can't promise anything. You can download the libraries that are used in my code here: libraries.

    Here's a photo that I snapped of the iRobot Create that we use in lab. We've got an additional LCD and sonar/range finder attached to a servo.



    Collision Detection:
    Objective: The robot is to travel a total of 2 meters in the forward direction and react to collisions.
    1. Go forward. If the robot bumps into something it will backup 15 cm
    2. Next it will turn left is the right bumper is hit, or right if the left bumper is hit (if both bumpers are hit it will turn right)
    3. Then the robot will move 25cm forward and turn 90 degrees in the opposite direction relative to the first turn so that it's traveling in the same heading it was originally on
    4. Then it will resume traveling the original 2 meters and stop

    Code:
    Code:
    #include "open_interface.h"
    #include "util.h"
    #include <stdlib.h> /* required for randomize() and random() */
    #include "lcd.h"
    #include <stdio.h>
    
    int  goForward();
    void correctDirection(char previousTurn);
    void turn(char direction);
    void printCrap(int which);
    
    int forwardDistance = 0;
    oi_t *sensor_status;  //Declare a pointer to the data type oi_t defined in open_interface.h
    int 
    main (void)
    {
    	
    	sensor_status = oi_alloc();  //Allocate memory appropriate for a oi_t type data structure and assign the returned address of the structure to the variable sensor_status
    	oi_init(sensor_status);  // Initialize the Open Interface
    	oi_clear_distance(sensor_status);
    
    	while (forwardDistance <= (2 * 100) ) {
    		int bumperStatus = goForward();
    		switch (bumperStatus)
    		{	
    			case 9:
    				printCrap(2);
    				break;
    			case 1:
    				printCrap(2);
    				turn('L');
    				correctDirection('L');
    				break;
    			case 2:
    				printCrap(2);
    				turn('R');
    				correctDirection('R');
    				break;
    			case 3:
    				printCrap(2);
    				turn('R');
    				correctDirection('R');
    				break;
    		}
    	}
    	
    	return 0;
    }
    
    //Go straight until collision detected, then back up and return bumper states
    int  goForward() {
    	//keep going straight until conllision (or stop if the entire 2m is clear)
    	int currentBumperStatus = oi_bump_status(sensor_status);
    	while ((currentBumperStatus == 0) && ((oi_current_distance(sensor_status) + forwardDistance)  <= (2 * 100))) { 
    		oi_set_wheels(250,250);
    		currentBumperStatus = oi_bump_status(sensor_status);
    	}
    	oi_set_wheels(0,0); //STOP
    	forwardDistance += oi_current_distance(sensor_status); // copy over distance as it's about to be cleared
    	oi_clear_distance(sensor_status);
    	//Are 2m up? If so exit function & we're done!
    	if ((oi_current_distance(sensor_status) + forwardDistance) >= (2 * 100)) {
    		return 9;
    	}
    	//Now back that ass up for 15 cm
    	while (oi_current_distance(sensor_status) >= -15){
    		oi_set_wheels(-250,-250);
    	}
    	oi_set_wheels(0,0);
    	oi_clear_distance(sensor_status);
    	//Return which bumpber was hit to take appropriate action
    	return currentBumperStatus;
    
    }
    
    //Move 25cm forward & resume opposite direcion relative to original direction
    void correctDirection(char previousTurn) {
    	oi_clear_distance(sensor_status); 
    	while (oi_current_distance(sensor_status) <= 25) { 
    		oi_set_wheels(250,250);
    	}
    	oi_set_wheels(0,0);
    	oi_clear_distance(sensor_status);
    	if (previousTurn == 'L'){
    		turn('R');
    	}
    	if (previousTurn == 'R') {
    		turn('L');
    	}
    }
    
    void turn(char direction) {
    oi_clear_angle(sensor_status);
    int angle = oi_current_angle(sensor_status);	
    	//Turn left
    	if (direction == 'L') {
    		while ((angle >=0 && angle <= 90) || (angle >=270 && angle <= 360)) {
    			oi_set_wheels(250,0);
    			wait_ms(20);
    			angle = oi_current_angle(sensor_status);
    		}
    	}
    	//Turn right
    	if (direction == 'R') {
    		while ((angle >=0 && angle <= 90) || (angle >=270 && angle <= 360)) {
    			oi_set_wheels(0,250);
    			wait_ms(20);
    			angle = oi_current_angle(sensor_status);
    		}
    	}
    	oi_set_wheels(0,0);
    	oi_clear_distance(sensor_status);
    }
    //Print the distance, angle, or bumper status on the LCD for debugging purposes
    void printCrap(int which) { 
    lcd_init();
    	if (which == 0 ) { 
    
    		lcd_home_line1();
    		char str[10];
    		int distanceTraveled = oi_current_distance(sensor_status); //distance returned is in cm
    		sprintf(str, "Distance: %dcm", distanceTraveled);
    		lcd_puts(str);
    	}
    	if (which == 1 ) { 
    
    		lcd_home_line1();
    		char str[10];
    		int distanceTraveled = oi_current_angle(sensor_status); //distance returned is in cm
    		sprintf(str, "Angle: %d deg", distanceTraveled);
    		lcd_puts(str);
    	}
    	if (which == 2 ) {
    		lcd_home_line1();
    		int bumperStatus = oi_bump_status(sensor_status); //distance returned is in cm;
    		
    		char str[22];
    		sprintf(str,"%s %d", "Bumper status:", bumperStatus);
    		sprintf(str,"%s %s", str, ".");
    		lcd_puts(str);
    	}
    }

    1 x Gold Star!
    (list)
    Last edited by Mr. Hasselhoff; 01-28-2011 at 01:32 PM.


  2. #2
    ░▒▓█ ♫1337♫ █▓▒░ Djzzero is one step away from curing cancer. Djzzero is one step away from curing cancer. Djzzero is one step away from curing cancer. Djzzero is one step away from curing cancer. Djzzero is one step away from curing cancer. Djzzero is one step away from curing cancer. Djzzero is one step away from curing cancer. Djzzero is one step away from curing cancer. Djzzero is one step away from curing cancer. Djzzero is one step away from curing cancer. Djzzero is one step away from curing cancer. Djzzero's Avatar
    Join Date
    Jul 2007
    Posts
    3,911
    Youtube Steam Xfire Twitter XBL Home Page Send a message via AIM to Djzzero Send a message via MSN to Djzzero Send a message via Yahoo to Djzzero
    Im my engineering class I finally got a class that deals with Robotics, we are working on BoeBot, Lego, and some others haha. Finally getting into the whole robotics things. seems interesting.

    http://www.AnimeXZone.com - Your Anime Streaming Needs! Watch Anime here!


    30% DISCOUNT & FREE SHIPPING OFF KARMALOOP.COM IF YOU USE REP CODE '30SHIP'


  3. #3
    Well I realize that I haven't been updating this thread at all, but I'm almost done with my semester final project. We have to design a rover to traverse through an obstacle course that we cannot see via bluetooth connection and make it into the retrieval zone (denoted by a square with 4 pillars in each corner). There are holes in the course (craters), IR walls (to designate the course bounds), and various objects (rocks, etc).

    I've got telemetry implemented, which means that I'm constantly in communication with the bot and receive back sensor information and can send commands.

    I've got the following critical sensors which have priority 0, which means if they go off, the rover will automatically halt on it's own without any kind of interaction with the mission control.
    • Cliff sensors (on 3 sides)
    • Bump sensors (2 on the front)
    • IR wall
    • Wall proximity (on edges)

    Then I have these sensors that can be controlled from mission control
    • IR sweep (180 degree sweep)
    • Sonar sweep (180 degree sweep)

    And of course I have movement control as well to make the bot move and turn in any direction.


    Here's a preview of the GUI app that I wrote in c# that handles the communication, which is almost 100% done. It's got a 2D map that plots where the bot has been and marks the obstacles that it encounters :)


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)