Example #1
0
int main()
{
	clear();	// clear the LCD
	print("Send serial");
	lcd_goto_xy(0, 1);	// go to start of second LCD row
	print("or press B");

	// Set the baud rate to 9600 bits per second.  Each byte takes ten bit
	// times, so you can get at most 960 bytes per second at this speed.
	serial_set_baud_rate(USB_COMM, 9600);

	// Start receiving bytes in the ring buffer.
	serial_receive_ring(USB_COMM, receive_buffer, sizeof(receive_buffer));

    while(1)
    {
		// USB_COMM is always in SERIAL_CHECK mode, so we need to call this
		// function often to make sure serial receptions and transmissions
		// occur.
		serial_check();

		// Deal with any new bytes received.
		check_for_new_bytes_received();

		// If the user presses the middle button, send "Hi there!"
		// and wait until the user releases the button.
		if (button_is_pressed(MIDDLE_BUTTON))
		{
			wait_for_sending_to_finish();
			memcpy_P(send_buffer, PSTR("Hi there!\r\n"), 11);
			serial_send(USB_COMM, send_buffer, 11);
			send_buffer[11] = 0;	// terminate the string
			clear();				// clear the LCD
			lcd_goto_xy(0, 1);		// go to start of second LCD row
			print("TX: ");
			print(send_buffer);

			// Wait for the user to release the button.  While the processor is
			// waiting, the OrangutanSerial library will not be able to receive
			// bytes from the USB_COMM port since this requires calls to the
			// serial_check() function, which could cause serial bytes to be
			// lost.  It will also not be able to send any bytes, so the bytes
			// bytes we just queued for transmission will not be sent until
			// after the following blocking function exits once the button is
			// released.  If any of this is a concern, you can replace the
			// following line with:
			// do
			// {
			//   while (button_is_pressed(MIDDLE_BUTTON))
			//     serial_check();	// receive and transmit as needed
			//   delay_ms(10);		// debounce the button press/release
			// }
			// while (button_is_pressed(MIDDLE_BUTTON));
			wait_for_button_release(MIDDLE_BUTTON);
		}
    }
}
Example #2
0
int main() {
	
	// capture any reset reason 
	G_reset_source = MCUSR;
	// clear the status register by writing ones 
	// (odd, I know, but that is how it is done)
	MCUSR = 0x1F;
	
	// Display on the LCD that serial connection is needed
	print("Waiting for");
	lcd_goto_xy(0, 1);
	print(" serial conn...");
	
	// This init will block if no serial connection present
	// so user sees message on LCD to make a connection
	init_interface();

	// Display the user interface over the serial usb
	// connection
	serial_check();
	print_reset_reason();
	
	print_usb("Welcome to lab 3!\r\n", 19);
	print_usage();
	print_prompt();
	
	// clear "Waiting for connection" message from the LCD
	clear();
	
	// turn on interrupts
	sei();
	
	init_motor();
	init_encoder();
	// set controller for 1000 Hz
	init_controller_w_rate(50);
	
	
	while (1) {
		serial_check();
		check_for_new_bytes_received();
	}
	
	return 0;
}
Example #3
0
int main(void) {

	init_menu();
	
	clear();
	init_motor();
	init_timers();
	encoders_init(IO_D2, IO_D3, IO_C4, IO_C5);

	sei();
	while (1) 
	{
		if(g_pd_release) {
			pd_task();
			g_pd_release = 0;
		}

		if(g_velocity_release) {
			velocity_task();
			g_velocity_release = 0;
		}

		if(g_encoder_release) {
			encoder_task();
			g_encoder_release = 0;
		}

		if(g_log_release) {
			log_task();
			g_log_release = 0;
		}

		if(g_interpolate_release) {
			interoplate_task();
			g_interpolate_release = 0;
		}

		serial_check();
		check_for_new_bytes_received();		
	}
}
int main(void) {
	
	delay_ms(100);
	
	init_leds();
	init_timers();
	init_motors();

	// Used to print to serial comm window
	char tempBuffer[80];
	
	// Initialization here.
	lcd_init_printf();	// required if we want to use printf() for LCD printing
	init_menu();	// this is initialization of serial comm through USB
	
	clear();	// clear the LCD

	//enable interrupts
	sei();
	
	while (1) {

		process_Trajectory();

		if (updateLCD)
		{
			//first line
			lcd_goto_xy(8,0);
			print("        ");
			
			lcd_goto_xy(8,0);
			//print_long(global_counts_m1);
			sprintf( tempBuffer, "V=%d\r", voltage);
			print(tempBuffer);

			//next line
			lcd_goto_xy(0,1);
			sprintf( tempBuffer, "R=%3.2f RPM=%3.2f\r", rotations, RPM);
			print(tempBuffer);
			
			updateLCD = false;
		}
		if (display_values_flag)
		{
			if (TARGET_ROT)
				length = sprintf( tempBuffer, "Kp:%f Ki:%f Kd:%f Pr:%f Pm:%f T:%d\r\n", Kp, Ki, Kd, goal, rotations, voltage);
			else if (TARGET_RPM)
				length = sprintf( tempBuffer, "Kp:%f Ki:%f Kd:%f Pr:%f Pm:%f T:%d\r\n", Kp, Ki, Kd, goal, RPM, voltage);
			else 
				length = sprintf( tempBuffer, "Kp:%f Ki:%f Kd:%f Pr:%f Pm:NA T:%d\r\n", Kp, Ki, Kd, goal, voltage);
			
			print_usb( tempBuffer, length );
			
			length = sprintf( tempBuffer, "C0:%d C1:%d C2:%d C3:%d C4:%d C5:%d Man:%d Eq:%d\r\n",	command_list[0], 
																						command_list[1],
																						command_list[2],
																						command_list[3],
																						command_list[4],
																						command_list[5],
																						maneuver_complete,
																						equilibrium);
			print_usb( tempBuffer, length );
							
			length = sprintf( tempBuffer, "%f, %f, %f\r\n",	error, integral, derivative);
			print_usb( tempBuffer, length );
			
			display_values_flag = false;
		}
		if (ready_to_dump_log)
		{
			ready_to_dump_log = false;
			
			length = sprintf( tempBuffer, "Goal, Kp, Ki, Kd, Rot, RPM, Volt\r\n");
			print_usb( tempBuffer, length );
			length = sprintf( tempBuffer, "%f, %f, %f, %f, %f, %f, %d\r\n", goal, Kp, Ki, Kd, rotations, RPM, voltage);
			print_usb( tempBuffer, length );
			
			int delta = (log_stop_timestep-log_start_timestep);
			
			length = sprintf( tempBuffer, "Time, Error, KpError, KiIntegral, KdDerivative, Rotations, RPM, Voltage\r\n");
			print_usb( tempBuffer, length );
			
			float temp_Kp = (Kp == 0.0) ? 1.0 : Kp;
			float temp_Ki = (Ki == 0.0) ? 1.0 : Ki;
			float temp_Kd = (Kd == 0.0) ? 1.0 : Kd;
			
			float time;
			
			for(int i=0; i<delta; i++)
			{
				time = (float)((i*dt)*60);
				length = sprintf( tempBuffer, "%f %f %f %f %f %f %f %d\r\n", time,	//time expressed in seconds
																			LOG[i].error,
																			(float)(temp_Kp * LOG[i].error),
																			(float)(temp_Ki * LOG[i].integral),
																			(float)(temp_Kd * LOG[i].derivative),
																			LOG[i].rotations,
																			LOG[i].RPM,
																			LOG[i].voltage);
				print_usb( tempBuffer, length );	
			}
		}

		//Process Control from the USB Port
		serial_check();
		check_for_new_bytes_received();
					
	} //end while loop
} //end main