/* * Main Loop */ int main() { LOG("--------------------------------\r\n"); /* Unmask interrupt for output compare match A on TC0 */ timers_setup_timer(TIMER_COUNTER0, TIMER_MODE_CTC, 1000UL); TIMSK0 |= (1 << OCIE0A); lcd_load_custom_character(degree_symbol, CUSTOM_SYMBOL_DEGREE); cli_init(); motor_init(&g_timers_state); log_init(); scheduler_init(&g_timers_state, g_tasks, COUNT_OF(g_tasks)); interpolator_init(&g_timers_state); sei(); log_start(); /* Main Loop: Run Tasks scheduled by scheduler */ while (1) { int i; for (i = 0; i < 50; i++) { serial_check(); /* needs to be called frequently */ } scheduler_service(); } }
void empty_sendbuf() { do { serial_check(); } while (!serial_send_buffer_empty(USB_COMM)); }
void rob_serial_check (void) { vTaskSuspendAll(); { serial_check(); } xTaskResumeAll(); }
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; }
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); } } }
void wait_for_sending_to_finish() { int32_t timeout = SERIAL_SEND_TIMEOUT; // need to have a timer here that can timeout // this helps in the case the serial port is not connected and // we could hang here until reset. while(!serial_send_buffer_empty(USB_COMM) && ( 0 < timeout-- ) ) { serial_check(); // USB_COMM port is always in SERIAL_CHECK mode } }
int s_read(char* buf, int want, int msecTimeout) { static int ringNext = -1; // -1 means uninitialized static char ringBuffer[kBufSize]; // size must fit in unsigned char if (want <= 0 || buf == 0) return 0; int maxRead = kBufSize - 1; if (want > maxRead) want = maxRead; if (ringNext < 0) { serial_receive_ring(USB_COMM, ringBuffer, kBufSize); ringNext = 0; } int avail = 0; int done = 0; while (!done) { serial_check(); int nbytes = serial_get_received_bytes(USB_COMM); avail = (nbytes - ringNext) & kSizeMask; if (avail > 0 || msecTimeout <= 0) done = 1; else { delay_ms(1); --msecTimeout; } } if (want > avail) want = avail; int got = 0; while (got < want) { buf[got++] = ringBuffer[ringNext++]; ringNext &= kSizeMask; } return got; }
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(); } }
// wait_for_sending_to_finish: Waits for the bytes in the send buffer to // finish transmitting on USB_COMM. We must call this before modifying // send_buffer or trying to send more bytes, because otherwise we could // corrupt an existing transmission. void wait_for_sending_to_finish() { while(!serial_send_buffer_empty(USB_COMM)) serial_check(); // USB_COMM port is always in SERIAL_CHECK mode }
void log_service() { serial_check(); }
/* cons_init - initializes the console devices */ void cons_init(void) { if (!serial_check()) { kprintf("serial port does not exist!!\n"); } }
/* serial_intr - try to feed input characters from serial port */ static void serial_intr(void) { if (serial_check()) { cons_intr(serial_proc_data); } }
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