|
THS 2011-2012 |
Main /
LibbysCodePageCode that I have written for the create resides on this page, so far there are a few working (more or less) pieces of code here Wall Following Code
// Include the Command Module API
#include "create.h"
// Pointer to the cr8_t struct
cr8_t *cr8;
// Main function
int main(void)
{
cr8 = cr8_alloc();
// Initialize the Create and Command Module
cr8_init(cr8);
//Update cr8
cr8_update(cr8);
while(1)
{
cr8_set_leds(0,1,0,0);
// Drive forward without hitting wall
while (cr8->wall_range < 120 && cr8->wall_range > 4)
{
cr8_set_leds(1,1,0,0);
// Set the linear and angular speed of the Create to 200mm/s linear and 0 mr/s angular.
cr8_set_speed(200, 0);
//Update again
cr8_update(cr8);
cr8_delay(15);
}
if (cr8->wall_range < 4)
{
cr8_set_speed(0,0); //stop
cr8_set_leds(0,0,0,0);
// cr8_beep_short(); //beep and
cr8_set_speed(50, -70); //turn right if wall seems too far away
}
else if (cr8->wall_range > 120)
{
cr8_set_leds(1,0,0,0);
//cr8_beep_long(); //beep and
cr8_set_speed(60, 70); //turn left if wall seems too close
}
cr8_update(cr8);
cr8_delay(50);
cr8_set_speed(0,0);
//cr8_beep_short();
}
cr8_free(cr8);
return 0;
}
Line Following Code
// Include the Command Module API
#include "create.h"
// Pointer to the cr8_t struct
cr8_t *cr8;
/* Exercise function for initialization
int exerciseInit(void);
//Exercise function for updating
int exerciseUpdate(void); */
// Initialize function called after the create has been initialized and
// before the update cycle begins.
// Return a value of zero to continue, and any other value to stop.
int Init(void)
{
return 0; // exerciseInit();
}
// Update function called once every iteration.
// Return a value of zero to continue, and any other value to stop.
/* int Update(void)
{
cr8_update(cr8);
return exerciseUpdate();
} */
// Main function
int main(void)
{
int stop = 0;
// Allocate the cr8_t structure
cr8 = cr8_alloc();
// Initialize the Create and Command Module
cr8_init(cr8);
// User initialization
if (Init())
{
return -1;
}
//cr8_set_speed(0,10);
// Update loop
while (stop == 0)
{
// Update call
// stop = Update();
cr8_update(cr8);
cr8_delay(15);
// BEGIN NEW CODE
// Power LED is green
cr8_set_leds(0,0,0,255);
// turns Power LED orange and drives at 200 as long as all of the cliff sensors measure greater than 600.
if ((cr8->cliff_frontleft_range>600) && (cr8->cliff_frontright_range>600) &&
(cr8->cliff_left_range>600) && (cr8->cliff_right_range>600))
{
cr8_set_leds(0,0,127,255);
cr8_set_wheels(200,200);
} else if ((cr8->cliff_frontright_range<600) || (cr8->cliff_right_range<600)){
//stop = 1;
cr8_set_speed(0,0);
cr8_set_speed(60, 200);
}
else {
cr8_set_speed(0,0);
cr8_set_speed(60, -200);
}
// END NEW CODE
}
// NEW CODE - beeps and turns Power LED red once it detects sensor
cr8_beep_long();
cr8_set_leds(0,0,255,255);
// END NEW CODE
// Stop the create from moving
cr8_set_speed(0,0);
cr8_free(cr8);
return 0;
}
|