Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.05 KB | None | 0 0
  1. /*
  2.  * Copyright 2016 <Admobilize>
  3.  * MATRIX Labs  [http://creator.matrix.one]
  4.  * This file is part of MATRIX Creator HAL
  5.  *
  6.  * MATRIX Creator HAL is free software: you can redistribute it and/or
  7.  * modify it under the terms of the GNU General Public License as
  8.  * published by the Free Software Foundation, either version 3 of the
  9.  * License, or (at your option) any later version.
  10.  * This program is distributed in the hope that it will be useful, but
  11.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  * General Public License for more details.
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  16.  */
  17.  
  18. #include <math.h>
  19. #include <iostream>
  20. #include <limits>
  21. #include <vector>
  22.  
  23. #include "../cpp/driver/everloop.h"
  24. #include "../cpp/driver/everloop_image.h"
  25. #include "../cpp/driver/imu_data.h"
  26. #include "../cpp/driver/imu_sensor.h"
  27. #include "../cpp/driver/wishbone_bus.h"
  28.  
  29. namespace hal = matrix_hal;
  30.  
  31. int main() {
  32.   float mag_max_x = std::numeric_limits<float>::min();
  33.   float mag_max_y = std::numeric_limits<float>::min();
  34.   float mag_min_x = std::numeric_limits<float>::max();
  35.   float mag_min_y = std::numeric_limits<float>::max();
  36.  
  37.   float mag_off_x = 0;
  38.   float mag_off_y = 0;
  39.   float mag_x = 0;
  40.   float mag_y = 0;
  41.  
  42.   bool xy_valid = false;
  43.  
  44.   float amp_distance_xy = 0;
  45.   float offset_distance_xy = 0;
  46.   float xy_rot = 0;
  47.   int xy_angle_index = 0;
  48.  
  49.   hal::WishboneBus bus;
  50.   bus.SpiInit();
  51.  
  52.   hal::IMUSensor imu_sensor;
  53.   imu_sensor.Setup(&bus);
  54.   hal::IMUData imu_data;
  55.  
  56.   hal::Everloop everloop;
  57.   everloop.Setup(&bus);
  58.   hal::EverloopImage image1d;
  59.  
  60.   while (true) {
  61.     imu_sensor.Read(&imu_data);
  62.  
  63.     mag_x = imu_data.mag_x;
  64.     mag_y = imu_data.mag_y;
  65.  
  66.     mag_max_x = (mag_x > mag_max_x) ? mag_x : mag_max_x;
  67.     mag_min_x = (mag_x < mag_min_x) ? mag_x : mag_min_x;
  68.     mag_max_y = (mag_y > mag_max_y) ? mag_y : mag_max_y;
  69.     mag_min_y = (mag_y < mag_min_y) ? mag_y : mag_min_y;
  70.  
  71.     mag_off_x = (mag_max_x + mag_min_x) / 2;
  72.     mag_off_y = (mag_max_y + mag_min_y) / 2;
  73.  
  74.     // validating : Waiting to have enough values to start calculating angle.
  75.     amp_distance_xy = sqrt(pow(mag_min_x - mag_max_x, 2) + pow(mag_min_y - mag_max_y, 2));
  76.     offset_distance_xy = sqrt(pow(mag_off_x, 2) + pow(mag_off_y, 2));
  77.     xy_valid = offset_distance_xy / amp_distance_xy < 5;
  78.  
  79.     if (xy_valid) {
  80.       // from rad to 0-360 deg
  81.       xy_rot = atan2(mag_y - mag_off_y, mag_x - mag_off_x) * 180 / M_PI + 180;
  82.       // saturation in 360 range
  83.       xy_angle_index = fmin(xy_rot, 359);
  84.       // saturation in 0 range
  85.       xy_angle_index = fmax(xy_rot, 0);
  86.       // making 24 slots of 360/24 = 15 deg
  87.       xy_angle_index = xy_angle_index / 10;
  88.     }
  89.  
  90.     for (hal::LedValue& led : image1d.leds)
  91.       led.red = led.green = led.blue = led.white = 0;
  92.  
  93.     image1d.leds[((35 - xy_angle_index)+17)%34].red = 100;
  94.     everloop.Write(&image1d);
  95.   }
  96.   return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement