mraa_uart_context uart_setup() { mraa_uart_context uart0 = mraa_uart_init(0); mraa_uart_set_baudrate(uart0, 9600); mraa_uart_set_mode(uart0, 8, MRAA_UART_PARITY_NONE, 1); return uart0; }
int main(){ load_device_tree("ADAFRUIT-UART4"); mraa_uart_context bbb; bbb = mraa_uart_init_raw("/dev/ttyO4"); mraa_uart_set_baudrate(bbb, 38400); mraa_uart_set_mode(bbb, 8,MRAA_UART_PARITY_NONE , 1); char buf[31] = "~4121|p123.324321|n053.989876$"; buf[30] = '\0'; while (1) { mraa_uart_write(bbb, buf, 30); usleep(10000); } return 0; }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////// Modem related functions ////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// int init_modem(){ mraa_init(); if( (uart = mraa_uart_init(0)) == NULL ){ writeToFile(src, "error INITIATING UART\n"); exit(UART_ERROR); } /*set uart boud rate [bps]*/ if ( mraa_uart_set_baudrate(uart, UART_BOUD_RATE)!=MRAA_SUCCESS) { writeToFile(src, "error seting baudrate\n"); exit(UART_ERROR); } if ( mraa_uart_set_mode(uart, 8,MRAA_UART_PARITY_NONE , 1)!=MRAA_SUCCESS) { writeToFile(src, "error seting mode\n"); } mraa_uart_set_flowcontrol(uart, 0, 0); writeToFile(src, "init modem\n"); }
/** * Set the transfer mode * For example setting the mode to 8N1 would be * "dev.setMode(8,UART_PARITY_NONE , 1)" * * @param bytesize data bits * @param parity Parity bit setting * @param stopbits stop bits * @return Result of operation */ Result setMode(int bytesize, UartParity parity, int stopbits) { return (Result) mraa_uart_set_mode(m_uart, bytesize, (mraa_uart_parity_t) parity, stopbits); }
/** * n_direction_flag: 0 from edison to beaglebone * 1 from beaglebone to edison * check https://github.com/peidong/drone/blob/master/Edison/main/edison-bbb-communication-code.md for commands */ int communication_with_beaglebone_uart(int nflag_direction, struct T_drone *pT_drone, int nflag_receive_success){ /** * check if uart available */ while (pT_drone->nflag_enable_uart != 1){ usleep(1300); } pT_drone->nflag_enable_uart = 0; mraa_uart_context beaglebone_uart; if (nflag_direction == 1){ /** * From beaglebone to edison */ beaglebone_uart = mraa_uart_init_raw("/dev/ttyO4"); mraa_uart_set_baudrate(beaglebone_uart, 38400); mraa_uart_set_mode(beaglebone_uart, 8, MRAA_UART_PARITY_NONE , 1); while (mraa_uart_data_available(beaglebone_uart, 10000) != 1){ printf("data not available\n"); /*usleep(10000);*/ } /** * Start receive */ char c_flag[1]; char arrc_buffer[20]; int nflag_find_beginning = 0; int nflag_find_end = 0; int n_index = 0; /** * Read the message array */ while (nflag_find_beginning != 1){ mraa_uart_read(beaglebone_uart, c_flag, 1); if (c_flag[0] == '~'){ nflag_find_beginning = 1; n_index = 0; while (nflag_find_end != 1){ mraa_uart_read(beaglebone_uart, arrc_buffer + n_index, 1); if (arrc_buffer[n_index] == '$'){ arrc_buffer[n_index] = '\0'; nflag_find_end = 1; //break; }else if (arrc_buffer[n_index] == '~'){ nflag_find_end = -1; nflag_find_beginning = 1; n_index = 0; //continue; }else{ n_index++; } } } } /** * Process the message */ int n_command_index = -1; if (arrc_buffer[0] == '0'){ /** * stop */ n_command_index = 0; printf("stop received\n"); }else if (arrc_buffer[0] == '1'){ /** * auto control */ char arrc_command_index[4]; int n_temp_index; for (n_temp_index = 0; n_temp_index <= 2; n_temp_index++){ arrc_command_index[n_temp_index] = arrc_buffer[n_temp_index]; } arrc_command_index[3] = '\0'; n_command_index = atoi(arrc_command_index); printf("auto control received: %d\n", n_command_index); }else if (arrc_buffer[0] == '2'){ /** * manual control command */ char arrc_command_index[4]; int n_temp_index; for (n_temp_index = 0; n_temp_index <= 2; n_temp_index++){ arrc_command_index[n_temp_index] = arrc_buffer[n_temp_index]; } arrc_command_index[3] = '\0'; n_command_index = atoi(arrc_command_index); printf("manual control received: %d\n", n_command_index); }else if (arrc_buffer[0] == '3'){ /** * pid tuning */ char arrc_command_index[4]; int n_temp_index; for (n_temp_index = 0; n_temp_index <= 2; n_temp_index++){ arrc_command_index[n_temp_index] = arrc_buffer[n_temp_index]; } arrc_command_index[3] = '\0'; n_command_index = atoi(arrc_command_index); printf("pid tuning received: %d\n", n_command_index); char arrc_pid_value[9]; for (n_temp_index = 0; n_temp_index <= 7; n_temp_index++){ arrc_pid_value[n_temp_index] = arrc_buffer[n_temp_index + 3]; } arrc_buffer[8] = '\0'; if (n_command_index == 301){ pT_drone->d_kp_pitch = atof(arrc_pid_value); }else if (n_command_index == 302){ pT_drone->d_ki_pitch = atof(arrc_pid_value); }else if (n_command_index == 303){ pT_drone->d_kd_pitch = atof(arrc_pid_value); }else if (n_command_index == 304){ pT_drone->d_kp_roll = atof(arrc_pid_value); }else if (n_command_index == 305){ pT_drone->d_ki_roll = atof(arrc_pid_value); }else if (n_command_index == 306){ pT_drone->d_kd_roll = atof(arrc_pid_value); }else if (n_command_index == 307){ pT_drone->d_kp_yaw = atof(arrc_pid_value); }else if (n_command_index == 308){ pT_drone->d_kd_yaw = atof(arrc_pid_value); }else if (n_command_index == 309){ pT_drone->d_ki_yaw = atof(arrc_pid_value); }else if (n_command_index == 310){ pT_drone->d_kp_second_pitch = atof(arrc_pid_value); }else if (n_command_index == 311){ pT_drone->d_kd_second_pitch = atof(arrc_pid_value); }else if (n_command_index == 312){ pT_drone->d_kp_second_roll = atof(arrc_pid_value); }else if (n_command_index == 313){ pT_drone->d_kd_second_roll = atof(arrc_pid_value); }else if (n_command_index == 314){ pT_drone->d_kp_second_yaw = atof(arrc_pid_value); }else if (n_command_index == 315){ pT_drone->d_kd_second_yaw = atof(arrc_pid_value); } printf("pid tuning value: %lf\n", atof(arrc_pid_value)); }else if (arrc_buffer[0] == '4'){ /** * feedback */ printf("feedback received\n"); } }else if (nflag_direction == 0){ /** * From edison to beaglebone */ } pT_drone->nflag_enable_uart = 1; return 0; }