Advertisement
j0h

pinePhonePro accelorometer visuallzer

j0h
Mar 12th, 2023 (edited)
756
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.47 KB | None | 0 0
  1. /* g++ -g -Wall stDev.cpp -o stDev  `pkg-config --cflags --libs opencv4`  */
  2.  
  3. #include <iostream>
  4. #include <fstream>
  5. #include <string>
  6. #include <cmath>
  7. #include <unistd.h>
  8. #include <opencv2/core/core.hpp>
  9.  
  10. // Library to include for
  11. // drawing shapes
  12. #include <opencv2/highgui/highgui.hpp>
  13. #include <opencv2/imgproc.hpp>
  14. using namespace cv;
  15. using namespace std;
  16.  
  17.  int num=0;
  18.  int val=0;
  19.  double sumxyz=0.0;
  20.  double avg=0.0;
  21.  double stDev=0.0;
  22.  double err=0.0;
  23.  ifstream myFile_Handler;
  24. int ReadSensor(string FileName);
  25. int main(){
  26. string FileX="/sys/bus/iio/devices/iio:device2/in_accel_x_raw";
  27. string FileY="/sys/bus/iio/devices/iio:device2/in_accel_y_raw";
  28. string FileZ="/sys/bus/iio/devices/iio:device2/in_accel_z_raw";
  29.  
  30.     Mat image(300, 700, CV_8UC3,Scalar(0, 0, 0)); //creates a new blank image
  31.  
  32.     // Check if the image is created successfully
  33.     if (!image.data) {
  34.         cout << "Could not open or find" << " the image";
  35.  
  36.         return 0;
  37.     }
  38. //assume variant coords +/- 50
  39. /*
  40. for (;;){
  41. sumxyz=ReadSensor(FileX) + ReadSensor(FileY) + ReadSensor(FileZ) ;
  42.     //cout << " X: " << ReadSensor(FileX) << " Y: " << ReadSensor(FileY) << " Z: "<< ReadSensor(FileZ) << endl;
  43.     cout << " XYZ : " <<sumxyz << endl;
  44.     sleep(1);
  45.     cout << "\x1b[A" << "\33[2K\r" << std::flush;
  46. }*/
  47. for (;;){
  48. //init lines at p1 (0,0) abs() because I only care about magnitude
  49.  
  50.     Point p1(0, 0);
  51.     Point p2(5,  abs(ReadSensor(FileX)));  //blue
  52.     Point p3(10, abs(ReadSensor(FileY ))/100); //red
  53.     Point p4(15, abs(ReadSensor(FileZ))/100); //green
  54.     int thickness = 2;
  55.  
  56. //  sleep(1);
  57. image.setTo(Scalar(0,0,0)); clears the old image by writing zeros
  58.  
  59.     // write new Antialiased lines to image
  60.     line(image, p1, p2, Scalar(0, 0, 255), thickness, LINE_AA);
  61.     line(image, p1, p3, Scalar(255, 0, 0), thickness, LINE_AA);
  62.     line(image, p1, p4, Scalar(0,  255, 0), thickness, LINE_AA);
  63.        
  64.     // Show our image inside window
  65.     imshow("Output", image);
  66.     waitKey(1);  //loops every 1 ms
  67.  
  68.     }
  69.             return 0;
  70. }
  71. //reads a file handle String for whatever sensor and returns its value
  72.  int ReadSensor(string FileName){
  73.          int val;
  74.          ifstream myFile_Handler;
  75.     string myLine;
  76.     myFile_Handler.open(FileName);
  77. // /sys/bus/iio/devices/iio\:device2/in_accel_x_raw
  78.     if(myFile_Handler.is_open())    {
  79.         while(getline(myFile_Handler, myLine)){
  80.                val = std::stoi(myLine);
  81.         }
  82.     myFile_Handler.close();
  83.     }    else    {
  84.         cout << "Unable to open the file!";
  85.     }
  86.  
  87.   return val;
  88.      }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement