/** * Uart Constructor, takes a string to the path of the serial * interface that is needed. * * @param uart the index of the uart set to use */ Uart(std::string path) { m_uart = mraa_uart_init_raw(path.c_str()); if (m_uart == NULL) { throw std::invalid_argument("Error initialising UART"); } }
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; }
// uart tty init rn2903_context rn2903_init_tty(const char *uart_tty, unsigned int baudrate) { rn2903_context dev; if (!(dev = _rn2903_preinit())) return NULL; // initialize the MRAA context // uart, default should be 8N1 if (!(dev->uart = mraa_uart_init_raw(uart_tty))) { printf("%s: mraa_uart_init_raw() failed.\n", __FUNCTION__); rn2903_close(dev); return NULL; } return _rn2903_postinit(dev, baudrate); }
mraa_uart_context mraa_uart_init(int index) { if (plat == NULL) { syslog(LOG_ERR, "uart: platform not initialised"); return NULL; } if (mraa_is_sub_platform_id(index)) { syslog(LOG_NOTICE, "uart: Using sub platform is not supported"); return NULL; } if (plat->adv_func->uart_init_pre != NULL) { if (plat->adv_func->uart_init_pre(index) != MRAA_SUCCESS) { syslog(LOG_ERR, "uart: failure in pre-init platform hook"); return NULL; } } if (plat->uart_dev_count == 0) { syslog(LOG_ERR, "uart: platform has no UARTs defined"); return NULL; } if (plat->uart_dev_count <= index) { syslog(LOG_ERR, "uart: platform has only %i", plat->uart_dev_count); return NULL; } if (!plat->no_bus_mux) { int pos = plat->uart_dev[index].rx; if (pos >= 0) { if (plat->pins[pos].uart.mux_total > 0) { if (mraa_setup_mux_mapped(plat->pins[pos].uart) != MRAA_SUCCESS) { syslog(LOG_ERR, "uart: failed to setup muxes for RX pin"); return NULL; } } } pos = plat->uart_dev[index].tx; if (pos >= 0) { if (plat->pins[pos].uart.mux_total > 0) { if (mraa_setup_mux_mapped(plat->pins[pos].uart) != MRAA_SUCCESS) { syslog(LOG_ERR, "uart: failed to setup muxes for TX pin"); return NULL; } } } } mraa_uart_context dev = mraa_uart_init_raw((char*)plat->uart_dev[index].device_path); if (dev == NULL) { return NULL; } dev->index = index; //Set the board Index. if (IS_FUNC_DEFINED(dev, uart_init_post)) { mraa_result_t ret = dev->advance_func->uart_init_post(dev); if (ret != MRAA_SUCCESS) { free(dev); return NULL; } } return dev; }
/** * 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; }
int main() { int sampleCount = 0; int sampleRate = 0; uint64_t rateTimer; uint64_t displayTimer; uint64_t now; // Using RTIMULib here allows it to use the .ini file generated by RTIMULibDemo. // Or, you can create the .ini in some other directory by using: // RTIMUSettings *settings = new RTIMUSettings("<directory path>", "RTIMULib"); // where <directory path> is the path to where the .ini file is to be loaded/saved RTIMUSettings *settings = new RTIMUSettings("RTIMULib"); RTIMU *imu = RTIMU::createIMU(settings); if ((imu == NULL) || (imu->IMUType() == RTIMU_TYPE_NULL)) { printf("No IMU found\n"); exit(1); } // This is an opportunity to manually override any settings before the call IMUInit // set up IMU imu->IMUInit(); // this is a convenient place to change fusion parameters imu->setSlerpPower(0.02); imu->setGyroEnable(true); imu->setAccelEnable(true); imu->setCompassEnable(true); // Set up serial out UART ports mraa_uart_context uart; //uart = mraa_uart_init(0); uart=mraa_uart_init_raw("/dev/ttyGS0"); mraa_uart_set_baudrate(uart, 9600); if (uart == NULL) { fprintf(stderr, "UART failed to setup\n"); return 0; } char buffer[20]={}; // To hold output while (1) { while (imu->IMURead()) { RTIMU_DATA imuData = imu->getIMUData(); //sleep(1); printf("%s\r", RTMath::displayDegrees("Gyro", imuData.fusionPose) ); //printf("\nx %f ",imuData.gyro.x()*(180.0/3.14)); //printf("\ny %f ",imuData.gyro.y()*(180.0/3.14)); //printf("\nz %f\n",imuData.gyro.z()*(180.0/3.14)); sprintf(buffer,"%4f\n",imuData.fusionPose.x()*(180.0/3.14)); mraa_uart_write(uart, buffer, sizeof(buffer)); printf("\n\n"); //imuData. fflush(stdout); //displayTimer = now; } } mraa_uart_stop(uart); mraa_deinit(); }
int main() { mraa_uart_context gps; load_device_tree("ADAFRUIT-UART1"); gps = mraa_uart_init_raw("/dev/ttyO1"); mraa_uart_set_baudrate(gps, 9600); char buf[1000]; char search[7]; gprmc_t readGPS; // char *p = buf; while(1){//gprmc_t readGPS; int i=0; mraa_uart_read(gps, search, 1); if(search[0] == '$'){ for(i=1; i<7;i++){ mraa_uart_read(gps, search+i, 1); } if(strstr(search, "$GPRMC,")){ for(i=0; i<100;i++){ mraa_uart_read(gps, buf+i, 1); if(buf[i] == '\n'){ buf[i]='\0'; break; } } // printf("%s\n", buf); nmea_parse_gprmc(buf, &readGPS); gps_convert_deg_to_dec(&(readGPS.latitude), readGPS.lat, &(readGPS.longitude), readGPS.lon); printf("%d\n", readGPS.state); printf("%f\n", readGPS.latitude); printf("%f\n", readGPS.longitude); } // mraa_uart_read(gps, buf+1, 1); // mraa_uart_read(gps, buf+2, 1); // mraa_uart_read(gps, buf+3, 1); // mraa_uart_read(gps, buf+4, 1); // mraa_uart_read(gps, buf+5, 1); // mraa_uart_read(gps, buf+6, 1); // mraa_uart_read(gps, buf+7, 1); // mraa_uart_read(gps, buf+8, 1); // mraa_uart_read(gps, buf+9, 1); // mraa_uart_read(gps, buf+10, 1); // mraa_uart_read(gps, buf+11, 1); // mraa_uart_read(gps, buf+12, 1); // mraa_uart_read(gps, buf+13, 1); // mraa_uart_read(gps, buf+14, 1); // mraa_uart_read(gps, buf+15, 1); // mraa_uart_read(gps, buf+16, 1); // printf("%s\n",buf); // if(buf[0]=='$') // { // printf("%s\n", buf); //buf = strchr(buf, ',')+1; //printf("%s\n", buf); // } // if (strchr(buf, '$GPRMC')!=NULL){ // //buf = strchr(buf, ',')+1; // //printf("%s\n", buf); // } //buf = strchr(buf, ',')+1; //printf("%s\n", buf); //mraa_uart_read(gps, buf, 1); // mraa_uart_read(gps, buf+1, 1); // mraa_uart_read(gps, buf+2, 1); // mraa_uart_read(gps, buf+3, 1); // mraa_uart_read(gps, buf+4, 1); // mraa_uart_read(gps, buf+5, 1); // mraa_uart_read(gps, buf+6, 1); // mraa_uart_read(gps, buf+7, 1); // mraa_uart_read(gps, buf+8, 1); // mraa_uart_read(gps, buf+9, 1); // mraa_uart_read(gps, buf+10, 1); // mraa_uart_read(gps, buf+11, 1); // mraa_uart_read(gps, buf+12, 1); // mraa_uart_read(gps, buf+13, 1); // mraa_uart_read(gps, buf+14, 1); // mraa_uart_read(gps, buf+15, 1); // mraa_uart_read(gps, buf+16, 1); // mraa_uart_read(gps, buf+17, 1); //int i=0; //for(i = 0; i<100) // if(nmea_get_message_type(buf)==NMEA_GPRMC){ // nmea_parse_gprmc(buf, &readGPS); // printf("%d\n", readGPS.speed);} // } } } }