Project 2 - Integrate following sensors to the Dev Board
GPS : Consumer grade off-the-shelf GPS sensors, also known as GPS mice
(e.g., Garmin GPS), are usually connected over USB and implement a
serial-to-USB connection with the help of FTDI devices. The Xsens GPS device can
be connected through a serial-to-USB connection.
Code :
1. #include <iostream>
2. #include <stdio.h> // standard input / output functions
3. #include <stdlib.h>
4. #include <string.h> // string function definitions
5. #include <unistd.h> // UNIX standard function definitions
6. #include <fcntl.h> // File control definitions
7. #include <errno.h> // Error number definitions
8. #include <termios.h> // POSIX terminal control definitions
9. #include <string>
10. #include <sstream>
11. #include <vector>
12. char * testSerialComm(int USB)
13. {14./* *** Configure Port *** */
15.structtermiostty;
16.memset(&tty,0,sizeoftty);
17./* Error Handling */
18.if(tcgetattr( USB, &tty ) !=0)
19. {20. std::cout <<"Error "<< errno <<" from tcgetattr: "<<strerror(errno) << std::endl;
21. }22./* Set Baud Rate */
23.cfsetospeed(&tty, B9600);
24.cfsetispeed(&tty, B9600);
25. /* Setting other Port Stuff */26. tty.c_cflag &= ~PARENB;// Make 8n1
27. tty.c_cflag &= ~CSTOPB;28. tty.c_cflag &= ~CSIZE;29. tty.c_cflag |= CS8;30. tty.c_cflag &= ~CRTSCTS;// no flow control
31. tty.c_lflag =0;// no signaling chars, no echo, no canonical processing
32. tty.c_oflag =0;// no remapping, no delays
33. tty.c_cc[VMIN] =0;// read doesn't block
34. tty.c_cc[VTIME] =15;// 0.5 seconds read timeout
35. tty.c_cflag |= CREAD | CLOCAL;// turn on READ & ignore ctrl lines
36. tty.c_iflag &= ~(IXON | IXOFF | IXANY);// turn off s/w flow ctrl
37. tty.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);// make raw
38. tty.c_oflag &= ~OPOST;// make raw
39./* Flush Port, then applies attributes */
40.tcflush( USB, TCIFLUSH );
41.if(tcsetattr( USB, TCSANOW, &tty ) !=0)
42. {43. std::cout <<"Error "<< errno <<" from tcsetattr"<< std::endl;
44. }45./* *** WRITE *** */
46.unsignedcharcmd[] = {'I','N','I','T',' ','\r','\0'};
47.intn_written =write( USB, cmd,sizeof(cmd)-1);
48./* Allocate memory for read buffer */
49.charbuf [1024];
50.memset(&buf,'\0',sizeofbuf);
51./* *** READ *** */
52.intn =read( USB, &buf ,sizeofbuf );
53./* Error Handling */
54.if(n <0)
55. {56. std::cout <<"Error reading: "<<strerror(errno) << std::endl;
57. }58./* Print what I read... */
59.returnbuf;
60.close(USB);
61. }62. int main() {63./* Open File Descriptor */
64.intUSB =open("/dev/tty.usbmodem14201", O_RDONLY | O_NDELAY | O_NONBLOCK);
65./* Error Handling */
66.if( USB <0)
67. {68. std::cout <<"Error "<< errno <<" opening "<<"/dev/ttyUSB0"<<": "<<strerror(errno) << std::endl;
69. }70.while(true){
71. std::string g =testSerialComm(USB);
72.std::stringstream stringStream(g);
73. std::string segment;74. std::vector<std::string> seglist;75.while(std::getline(stringStream, segment))
76. {77. seglist.push_back(segment);
78. }79.for(inti =0; i < seglist.size(); i++){
80. std::string s = seglist[i];81.if( g !=""){
82.if(s.find("$GNGLL") != std::string::npos){
83. std::cout << g ;84.if(s.find(",,,,,") != std::string::npos){
85. std::cout <<"Keine Position erkannt"<< std::endl;
86. }87.else{
88. std::cout <<"s = "<< s;
89.std::stringstream stringStream(s);
90. std::string segment2; 91. std::vector<std::string> seglist2;92.while(std::getline(stringStream, segment))
93. {94. seglist2.push_back(segment2);
95. }96. std::cout <<"Lat: "<< seglist2[1] <<" "<< seglist2[2] << std::endl;
97. std::cout <<"Lon: "<< seglist2[3] <<" "<< seglist2[4] << std::endl;
98. std::cout <<"UTC Time: "<< seglist2[5] << std::endl;
99. }100. }101. }102. }103. }104.return0;
105. }
IMU :
* IMU, meaning for Inertial Measurement Unit is defined as a 9-axis sensor that measures orientation, velocity, and gravitational forces by combining Accelerometer, Gyroscope, and Magnetometer into one.
* IMUs typically come in large packages, but with recent developments like MEMS technology, they are now more commonly seen as miniaturized sensors designed for easy integration with Arduino or other
microcontrollers.
Code : 1. #define VERSION_STR_LENG 35
2. #define MFX_DELTATIME 10
3. […]
4. /*** Initialization ***/
5. char lib_version[VERSION_STR_LENG];
6. char acc_orientation[3];
7. MFX_knobs_t iKnobs;
8. /* Sensor Fusion API initialization function */
9. MotionFX_initialize();
10. /* Optional: Get version */
11. MotionFX_GetLibVersion(lib_version);
12. MotionFX_getKnobs(&iKnobs);
13. /* Modify knobs settings */
14. MotionFX_setKnobs(&iKnobs);
15. /* Enable 9-axis sensor fusion */
16. MotionFX_enable_9X(MFX_ENGINE_ENABLE);
17. […]
18. /*** Using Sensor Fusion algorithm ***/
19. Timer_OR_DataRate_Interrupt_Handler(
20. MFX_input_t data_in;
21. MFX_output_t data_out;
22. /* Get acceleration X/Y/Z in g */
23. MEMS_Read_AccValue(data_in.acc[0], data_in.acc[1], data_in.acc[2]);
24. /* Get angular rate X/Y/Z in dps */
25. MEMS_Read_GyroValue(data_in.gyro[0], data_in.gyro[1], data_in.gyro[2]); /*
26. Get magnetic field X/Y/Z in uT/50 */
27. MEMS_Read_MagValue(data_in.mag[0], data_in.mag[1], &data_in.mag[2]); /*
28. Run Sensor Fusion algorithm */
29. MotionFX_propagate(&data_out, &data_in, MFX_DELTATIME);
30. MotionFX_update(&data_out, &data_in, MFX_DELTATIME, NULL);
31. }