Exemplo n.º 1
0
int main(void)
{

    usart0_init(25, 8, 1, USART_PARITY_EVEN,1); //38400
    usart1_init(25, 8, 1, USART_PARITY_DISABLED,0); //TODO check oi_alternative OI_ALTERNATE_BAUD_RATE

    _delay_ms(333);
    oi_switch_baud_rate();
    _delay_ms(333);
    oi_init();
    oi_full_mode();
    int i;
    int val;
    for (i = 0; i < 10; i++)
    {
        val = i % 2;
        oi_set_leds(val, val, val, val, 0xFF * val, 0xFF);
        _delay_ms(50);
    }
    oi_init();

    //input_capture_test();
    printf0("Hello world!\r\n");
    printf0(" 0x%02X 0x%02X 0x%02X", UCSR0A, UCSR0B, UCSR0C);
    _delay_ms(3000);
    doSweepLoop();
    doPingLoop();
    doIrLoop();
    servo_test();
}
Exemplo n.º 2
0
int main() {
	
	//initialize all necessary sensors and utilities
	lcd_init();
	timer1_init();
	timer3_init();
	move_servo(90);
 	ADC_init();
	USART_Init(MYUBRR);
	init_push_buttons();
	
	oi_t *sensor_data = oi_alloc();
	oi_init(sensor_data);
	
	audioInit(sensor_data);
	//oi_play_song(1);
	
	while(1) {
		//empty currentObjects before proceeding by setting all stored objects to "invalid" - ignored by later checks
		for (int i = 0; i < 20; i++) {
			currentObjects[i].isValid = 0;
		}
		char received = serial_getc(); //take keyboard input from putty
		takeDirectionInput(received, currentObjects); //translate keyboard input into functionality
	}
	
	return 0;
}
Exemplo n.º 3
0
/**
* Initializes everything on the robot
* @author Group B1
* @param oi The open interface of the robot
* @date 12/4/2012
*/
void init_all(oi_t *oi)
{
	oi = oi_alloc();
	oi_init(oi);
	init_buttons();
	init_usart();
	lcd_init();
	timer3_init();
	ADC_init();
	init_printf(0,write_one_char);
	move_servo(0);
	wait_ms(1000);
	printf("\n");
	printf("\n");
}
/**
 *
 * Function to initialize the timers, registers, etc. for the ir, ping, servo, lcd
 */
void initialize()
{
	// Initialize Serial Communication
	serial_init(57600);
	// initialize timers for use by ping sensor and servo
	timer3_init(); 
	timer_init();
	// initialize ADC for use by IR sensor
	ADC_init(); 
	// Initialize Open Interface and sensor data
	sensor_data = oi_alloc();
	oi_init(sensor_data);
	// initialize LCD
	lcd_init();
	load_songs();
	
}
Exemplo n.º 5
0
//Handler for OI, moved from control.
void oi_system()
{
	movement_data_t movement_data;
	rotation_data_t rotation_data;
	
	enum {
		oi_command_init = 0,
		oi_command_move = 1,
		oi_command_rotate = 2,
		oi_command_play_song = 3,
		oi_command_dump = 4,
		oi_command_end_sequence = 5
	} oi_command = usart_rx();

	txq_enqueue(oi_command);

	switch (oi_command) {

		case oi_command_init:
			oi_init(&(control.oi_state));
			break;

		case oi_command_move:
	
			if(rx_frame()) {
				r_error(error_frame,"Move should not have multiple frames.");
			}

			struct {
				uint16_t speed;
				uint16_t dist;
				bool stream;
			} *move_data = (void *) &control.data;
			
			struct {
				uint16_t dist;
				uint8_t flag;
			} *response_move = (void *) &control.data;

			#warning "Stream functionality to be implemented later."
			//Stream returns the distance traveled
			//lcd_puts  ("In OI subsystem"); //debug
			movement_data = move_dist(&(control.oi_state), move_data->dist, move_data->speed);
			
			response_move->dist = movement_data.travelled;
			response_move->flag = movement_data.flag;
			
			if (movement_data.flag != 0) {
				movement_data = move_dist(&(control.oi_state), -20, move_data->speed);
				response_move->dist += movement_data.travelled;
			}

			control.data_len = 3;
			tx_frame(false);
			break;

		case oi_command_rotate:

			if (rx_frame()) {
				r_error(error_bad_message, "Rotate should only have one data frame.");
			}

			int16_t *angle = &(control.data[0]); // TODO: test if this is the right number of bytes

			if (control.data_len != 2/*sizeof(*angle)*/) { // TODO: hardcoding to debug
				r_error(error_bad_message, "Received too much data with rotate "
																	   "message.");
			}

			struct {
				uint16_t rotation;
			} *response_rotation = (void *) &control.data;

			rotation_data = turn(&(control.oi_state), *angle);
			response_rotation->rotation = rotation_data.rotated;
			control.data_len = 2;
			tx_frame(false);
			break;
			
			//Sing me a song.
		case oi_command_play_song:
			
			#warning "oi_command_play_song is deprecated"
			
			;
			//assuming that we get two data frames, the first containing the notes and the second containing the durations.
			int j;
				struct {
					uint8_t n; //The number of notes present in the song
					//char data[n];
					uint8_t index;
				} *song_data = (void *) &control.data;
		
			//	int j;
		 
			while(rx_frame()) { //this should happen twice please
				char tmp_notes[song_data->n];
				char tmp_durs[song_data->n];
			
				for(j =0; j<song_data->n; j++) {
					//tmp_notes[n]; TODO: broken
					#warning "oi_command_play_song not implemented"
				}

			}
		
			break;

		case oi_command_dump:
			//copies all of the data from OI_UPDATE and transmits to Control.
			oi_update(&(control.oi_state));
			memcpy(control.data, &control.oi_state, sizeof(control.oi_state));
			control.data_len = sizeof(control.oi_state);
			tx_frame(false);
			break;
		
		case oi_command_end_sequence:
			#warning "oi_command_end_sequence not implemented"
			
			// Switch power LED off
			oi_set_leds(1, 1, 0, 0);
			
			wait_ms(50);
			
			// Switch power LED to orange
			oi_set_leds(1, 1, 170, 255);
			
			wait_ms(50);
			
			// Switch power LED off
			oi_set_leds(1, 1, 0, 0);
			
			wait_ms(50);
			
			// Switch power LED to yellow
			oi_set_leds(1, 1, 70, 255);
			
			wait_ms(50);
			
			// Switch power LED off
			oi_set_leds(1, 1, 0, 0);
			
			wait_ms(50);
			
			// Switch power LED back to default state (from oi_init())
			oi_set_leds(1, 1, 7, 255);
			
			// Play song
			songs_load(RICK_ROLL);
			
			break;
		
		default:
			r_error(error_bad_message, "Bad OI Command");
			break;
	}
}
Exemplo n.º 6
0
/**
* The main loop which waits for commands via blue tooth and
* then acts based on commands received.
*
* @author Jacob Moyer,
*         Ed Droesch,
*         Aaron Pederson, &
*         Matthew Backes
*
* @date 12/13/2013
*/
void main()
{
    ///Initializes the open_interface struct to be used throughout the program.
    oi_t *sensor_data = oi_alloc();
    oi_init(sensor_data);
    initializer();
    char key =0;

    ///Waits for commands from the USART connection,
    ///Then performs the command and returns that the command was performed.
    while(1)
    {
        char message[32] = "";
        key = USART_Receive();

        ///Returns what the current tape sensor is reading.
        if (key == 'p')
        {
            oi_update(sensor_data);
            sprintf(message,"sensor->signal =%d\r\n",(int)sensor_data->cliff_frontleft_signal);
            string_tran(message);
        }

        ///Moves the servo motor to the 0 degree position.
        if(key == 'c')
        {
            calibrate_servo(0);
        }

        ///Scans 0 - 180 degrees in front of the robot and returns the readings,
        ///it also guesses where objects are and returns their angle and width.
        if(key == 'j')
        {
            fullScan();
            string_tran("Scan Complete.\r\n");
        }

        ///Moves FORWARD_DISTANCE centimeters forward.
        if(key =='w')
        {
            sprintf(message,"Moved %d\r\n",move(sensor_data, FORWARD_DISTANCE,&sensors));
            string_tran(message);
        }

        ///Moves BACKWARD_DISTANCE centimeters backwards.
        if(key=='s')
        {
            sprintf(message,"Moved %d\r\n",move(sensor_data, BACKWARD_DISTANCE,&sensors));
            string_tran(message);
        }

        ///Rotates ROTATE_DEGREES counterclockwise.
        if(key=='a')
        {
            turn_CCW(sensor_data,ROTATE_DEGREES);
            sprintf(message,"Rotated %d\r\n",ROTATE_DEGREES);
            string_tran(message);
        }

        ///Rotates ROTATE_DEGREES clockwise.
        if(key=='d')
        {
            turn_CCW(sensor_data,-ROTATE_DEGREES);
            sprintf(message,"Rotated %d\r\n",-ROTATE_DEGREES);
            string_tran(message);
        }

        ///Plays the Morrowind theme song.
        if(key=='r')
        {
            play_morrowind();
            string_tran("Playing song.");
        }
    }
}
Exemplo n.º 7
0
  int main(int argc, char *argv[])
#endif
{
  /* C90 requires all vars to be declared at top of function */

  CoiHandle entity;
  CoiHandle node;
  CoiHandle light;
  CoiHandle rendersystem;
  CoiHandle renderwindow;
  CoiHandle viewport;

  CoiHandle plane;
  CoiHandle plane_entity;
  CoiHandle plane_node;

  CoiHandle terrain_group;
  CoiHandle terrain_iterator;
  CoiHandle import_data;
  CoiHandle image;

  float direction[3];
  float colour[4];

#if defined(LLCOI_TEST_USE_OPENINPUT)
  // Openinput
  oi_event evt;
  char openinput_window_params[100];
  unsigned int windowHnd = 0;

#if defined(PLATFORM_LINUX)
  Display *disp;
  Window win;
  unsigned int scrn;
#endif
#endif //LLCOI_TEST_USE_OPENINPUT

  keep_going = 1;
  long loop_x = 0;
  long loop_y = 0;

  // setup
  create_root("plugins.cfg", "ogre.cfg", "ogre.log");

  if (!(restore_config() || show_config_dialog()))
    {
      return 1;
    }

	setup_resources("resources.cfg");

  renderwindow = root_initialise(1, "Ogre Renderwindow");

  set_default_num_mipmaps(5);

  initialise_all_resource_groups();

  create_scene_manager("OctreeSceneManager", "The SceneManager");

  myCamera = create_camera(get_scene_manager(), "mycam");

  camera_set_position(myCamera, 1683, 50, 2116);

  camera_lookat(myCamera, 1963, -50, 1660);

  camera_set_near_clip_distance(myCamera, 1);
  camera_set_far_clip_distance(myCamera, 50000);

  viewport = add_viewport(myCamera);

  viewport_set_background_colour(viewport, 0, 0, 0);

  camera_set_aspect_ratio(myCamera, 800, 600);


  // entities

  plane = create_plane_from_normal(0, 1, 0, 0);
  mesh_manager_create_plane("ground", "General", plane, 1500, 1500, 20, 20, 1, 1, 5, 5, 0, 0, 1);
  plane_entity = create_entity(get_scene_manager(), "plane", "ground");
  entity_set_material_name(plane_entity, "Dev/Red");
  plane_node = create_child_scene_node(get_scene_manager(), "planenode");
  scene_node_attach_entity(plane_node, plane_entity);

  entity = create_entity(get_scene_manager(), "OgreHead", "ogrehead.mesh");

  node = create_child_scene_node(get_scene_manager(), "headNode");

  scene_node_attach_entity(node, entity);

  scene_manager_set_ambient_light_rgb(get_scene_manager(), 0.5f, 0.5f, 0.5f);

  light = create_light(get_scene_manager(), "mainLight");

  light_set_position(light, 20, 80, 50);


  // terrain
  create_terrain_global_options();
  terrain_group = create_terrain_group(get_scene_manager(), ALIGN_X_Z, 513, 12000.0f);
  terrain_group_set_filename_convention(terrain_group, "BasicTutorial3Terrain", "dat");
  terrain_group_set_origin(terrain_group, 0, 0, 0);

  // terrain defaults
  terrain_global_options_set_max_pixel_error(8);
  terrain_global_options_set_composite_map_distance(3000);

  terrain_global_options_set_light_map_direction_vector3(light_get_derived_direction(light));

  terrain_global_options_set_composite_map_ambient_colour(scene_manager_get_ambient_light(get_scene_manager()));// sm

  terrain_global_options_set_composite_map_diffuse_colour(light_get_diffuse_colour(light));// light

  import_data = terrain_group_get_default_import_settings(terrain_group);
  terrain_group_import_data_set_terrain_size(import_data, 513);
  terrain_group_import_data_set_world_size(import_data, 12000.0f);
  terrain_group_import_data_set_input_scale(import_data, 600);
  terrain_group_import_data_set_min_batch_size(import_data, 33);
  terrain_group_import_data_set_max_batch_size(import_data, 65);

  terrain_group_import_data_resize_layers(import_data, 3);
  terrain_group_import_data_set_layer(import_data, 0, 100, "nvidia/dirt_grayrocky_diffusespecular.dds", "nvidia/dirt_grayrocky_normalheight.dds");
  terrain_group_import_data_set_layer(import_data, 1, 30, "nvidia/grass_green-01_diffusespecular.dds", "nvidia/grass_green-01_normalheight.dds");
  terrain_group_import_data_set_layer(import_data, 2, 200, "nvidia/growth_weirdfungus-03_diffusespecular.dds", "nvidia/growth_weirdfungus-03_normalheight.dds");


  // define terrains
  for (loop_x; loop_x <= 0; ++loop_x)
    {
      for (loop_y; loop_y <= 0; ++loop_y)
        {
          if (resource_exists(terrain_group_get_resource_group(terrain_group), terrain_group_generate_filename(terrain_group, loop_x, loop_y)))
            {
              terrain_group_define_terrain(terrain_group, loop_x, loop_y);
            }
          else
            {
              image = create_image();
              image_load(image, "terrain.png");
              if (loop_x % 2 != 0)
                {
                  image_flip_around_y(image);
                }
              if (loop_y % 2 != 0)
                {
                  image_flip_around_x(image);
                }

              terrain_group_define_terrain_image(terrain_group, loop_x, loop_y, image);
              image_delete(image);
              image = NULL;
            }

        }
    }


  terrain_group_load_all_terrains(terrain_group, 1);

  // blend maps
  /*
  terrain_iterator = terrain_group_get_terrain_iterator(terrain_group);
  while(terrain_iterator_has_more_elements(terrain_iterator))
    {
      CoiHandle terrain = terrain_iterator_get_next(terrain_iterator);

      int blend_map_size = terrain_get_layer_blend_map_size(terrain);
      CoiHandle blend_map_0 = terrain_get_layer_blend_map(terrain, 1);
      CoiHandle blend_map_1 = terrain_get_layer_blend_map(terrain, 2);

      float min_height_0 = 70;
      float fade_dist_0 = 40;
      float min_height_1 = 70;
      float fade_dist_1 = 15;

      float* blend_1 = terrain_layer_blend_map_get_blend_pointer(blend_map_1);

      for(unsigned int y = 0; y < blend_map_size; ++y)
        {
          for(unsigned int x = 0; x < blend_map_size; ++x)
            {
              float tx, ty;
              terrain_layer_blend_map_convert_image_to_terrain_space(x, y, &tx, &ty);
              float height = terrain_get_height_at_terrain_position(tx, ty);
              float val = (height - min_height_0) / fade_dist_0;
              val = math_clamp_f(val, 0.0f, 1.0f);
              val = (height - min_height_1) / fade_dist_1;
              val = math_clamp_f(val, 0.0f, 1.0f);

              *blend_1++ = val;
            }
        }

      terrain_layer_blend_map_dirty(blend_map_0);
      terrain_layer_blend_map_dirty(blend_map_1);
      terrain_layer_blend_map_update(blend_map_0);
      terrain_layer_blend_map_update(blend_map_1);
    }
  */

  terrain_group_free_temporary_resources(terrain_group);
  // listeners

  add_frame_listener(frame_listener_test,EVENT_FRAME_RENDERING_QUEUED|EVENT_FRAME_STARTED);

	add_window_listener(renderwindow, window_event_listener_test);

  input_manager = create_input_system(render_window_get_hwnd(renderwindow));
  input_listener = create_input_listener();

  keyboard = create_keyboard_object(input_manager, 1);
  mouse = create_mouse_object(input_manager, 1);

  attach_keyboard_listener(keyboard, input_listener);
  attach_mouse_listener(mouse, input_listener);
  add_key_pressed_listener(input_listener, key_pressed_test);

  while(keep_going)
    {
      keyboard_capture(keyboard);
      mouse_capture(mouse);

      // Pump window messages for nice behaviour
      pump_messages();
      // Render a frame
      render_one_frame();

      if (render_window_closed(renderwindow))
        {
          keep_going = 0;
        }

    }

#if defined(LLCOI_TEST_USE_OPENINPUT)
  windowHnd = render_window_get_hwnd(renderwindow);

#if defined(PLATFORM_LINUX)
  disp = XOpenDisplay( NULL );
  scrn = DefaultScreen(disp);
  sprintf(openinput_window_params, "c:%u s:%u w:%u", (unsigned int)disp, (unsigned int)scrn, windowHnd);
#else
  sprintf(openinput_window_params, "c:%u s:%u w:%u", 0, 0, windowHnd);
#endif

  oi_init(openinput_window_params, 0);

  log_message("***************************");
  log_message("*** All Systems online! ***");
  log_message("***************************");

  //render_loop();
  while (keep_going)
    {
      // ask oi to wait for events
      oi_events_poll(&evt);
      switch(evt.type)
        {
        case OI_QUIT:
          // Quit
          keep_going = 0;
          break;

        case OI_KEYDOWN:
          // Keyboard button down
          //WTF?? Better way to check this, please..
          if(evt.key.keysym.sym == OIK_ESC)
            keep_going = 0;
          break;

        default:
          break;
        }
      // Pump window messages for nice behaviour
      pump_messages();
      // Render a frame
      render_one_frame();

      if (render_window_closed())
        {
          keep_going = 0;
        }
    }

  oi_close();
#endif //LLCOI_TEST_USE_OPENINPUT

	remove_window_listener(renderwindow);

  destroy_keyboard_object(input_manager, keyboard);
  destroy_mouse_object(input_manager, mouse);
  destroy_input_system(input_manager);
  release_engine();

  return 0;
}
/**
*	Used to control the robot.
*	Receive and transmit data, measure the distance from object and navigate to the retrieval zone.
**/
int main(void)
{
	lcd_init();
	timer3_init();
	timer_init();
	ADC_init();
	USART_Init();
	oi_t *sensor_data = oi_alloc();
	oi_init(sensor_data);//should turn the iRobot Create's power LED yellow
  
	lcd_init();
	serial_puts("Start");
	//USART_Transmit(13);
	//USART_Transmit(10);
	
	
    int TempAngle[4] = {0,0,0,0};
	int TempIR[4] = {0,0,0,0};
	int pos[4] = {0,0,0,0};
	int AddIR[4] = {0,0,0,0};
	int count[4] = {0,0,0,0};
	int found = 0;
	int x1 = 0;
	int x2 = 0;
	int x3 = 0;
	int x4 = 0;
	unsigned angle = 0;
	unsigned char IR = 0;
	volatile int i=0;
	volatile int x = 0;
	char command;
    char display[100];
	char display1[20];
	char display2[20];
	char display3[20];
	char display4[20];
	char display5[100];
	char display6[100];	
			
	while (1)
	{
		command = USART_Recieve();
		USART_Transmit(command);
		//USART_Transmit(13);
		//USART_Transmit(10);
		
		if (command == '1')
		{
			found = 0;
			angle = 0;
			int t;
			int TempAngle[6] = {0, 0,0,0,0,0};
			int TempIR[6] = {0, 0,0,0,0,0};
			int pos[6] = {0, 0,0,0,0,0};
			int AddIR[6] = {0, 0, 0,0,0,0};
			int count[6] = {0, 0, 0,0,0,0};
			for (angle = 0;angle < 181;angle++)
			{	
				move_servo(angle);
				wait_ms(20);				
				
				IR = 0;
				IR = 42800*pow(ADC_read(2),-1.23);
				
				sprintf(display6, "Angle: %5d		IR: %5d",angle,IR);
				serial_puts(display6);

				if (IR < 80)
				{
				   TempAngle[found]++;
				   count[found]++;
				   AddIR[found]+=IR;
				   TempIR[found]=AddIR[found]/count[found];
				}
				
				else
				{
					if(TempAngle[found] < 5)
					{
						TempAngle[found] = 0;
					}
					
					else
					{
						pos[found] = angle- TempAngle[found]/2;
						if (TempIR[found]*TempAngle[found]< 460)
						{
							USART_Transmit(13);
							USART_Transmit(10);
							for (int i = 0;i<strlen(s8);i++)
							{
								USART_Transmit(s8[i]);
							}
							
							sprintf(display5, "object position: %5d",pos[found]);
							serial_puts(display5);
						}
						sprintf(display, "object position: %5d		IR: %5d		object size: %5d",pos[found],TempIR[found],TempAngle[found]);
						serial_puts(display);
						USART_Transmit(13);
						USART_Transmit(10);
						found++;
					}	
			     }
			}
			OCR3B = 1000-1;				//return to 0 degree	
		}				

		 if (command == 'w')
		 {
			 move_forward(sensor_data,20);
		 }
		 if (command == 's')
		 {
			 move_backforward(sensor_data,20);
		 }
		 if (command == 'a')
		 {
			turn_clockwise(sensor_data,82);
		 }
		 if (command == 'd')
		 {
			turn_counterclockwise(sensor_data,82);
		 }
		 if (command == 'q')
		 {
			 turn_clockwise(sensor_data,38);
		 }
		 if (command == 'e')
		 {
			 turn_counterclockwise(sensor_data, 38);
		 }
		 if (command == '8')
		 {
			 move_forward(sensor_data,5);
		 }
		 if (command == '5')
		 {
			 move_backforward(sensor_data,5);
		 }
		 if (command == 'p')
		 {
			 oi_t* sensor = oi_alloc();
			 oi_init(sensor);
			 
			 load_songs();
			 oi_play_song(songings);
		 }
		 if(command == 'k')
		 {
			oi_update(sensor_data);
			x1 = sensor_data->cliff_left_signal;
			x2 = sensor_data->cliff_right_signal;
			x3 = sensor_data->cliff_frontleft_signal;
			x4 = sensor_data->cliff_frontright_signal; 
			sprintf (display1, "left        = %d",x1);
			sprintf (display2, "right       = %d",x2);
			sprintf (display3, "front left  = %d",x3);
			sprintf (display4, "front right = %d",x4);
			USART_Transmit(13);
			USART_Transmit(10);
	        serial_puts(display1);
	        serial_puts(display3);
	        serial_puts(display4);			
			serial_puts(display2);
			if (x1>500||x2>500||x3>500||x4>500)
			{
				USART_Transmit(13);
				USART_Transmit(10);
				for (int i=0;i<strlen(s6);i++)
				{
					USART_Transmit(s6[i]);
				}
			}						
		}
	}	
}