Advertisement
KRITSADA

PIXY and IPST-SE Example Codes

Jul 29th, 2017
615
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.43 KB | None | 0 0
  1. //
  2. // begin license header
  3. //
  4. // This file is part of Pixy CMUcam5 or "Pixy" for short
  5. //
  6. // All Pixy source code is provided under the terms of the
  7. // GNU General Public License v2 (http://www.gnu.org/licenses/gpl-2.0.html).
  8. // Those wishing to use Pixy source code, software and/or
  9. // technologies under different licensing terms should contact us at
  10. // cmucam@cs.cmu.edu. Such licensing terms are available for
  11. // all portions of the Pixy codebase presented here.
  12. //
  13. // end license header
  14. //
  15.  
  16. #include <SPI.h>  
  17. #include <Pixy.h>
  18. #include <ipst.h>
  19.  
  20. #define X_CENTER    160L
  21. #define Y_CENTER    100L
  22. #define RCS_MIN_POS     0L
  23. #define RCS_MAX_POS     1000L
  24. #define RCS_CENTER_POS  ((RCS_MAX_POS-RCS_MIN_POS)/2)
  25.  
  26. class ServoLoop
  27. {
  28. public:
  29.   ServoLoop(int32_t pgain, int32_t dgain);
  30.  
  31.   void update(int32_t error);
  32.    
  33.   int32_t m_pos;
  34.   int32_t m_prevError;
  35.   int32_t m_pgain;
  36.   int32_t m_dgain;
  37. };
  38.  
  39.  
  40. ServoLoop panLoop(500, 800);
  41. ServoLoop tiltLoop(700, 900);
  42.  
  43. ServoLoop::ServoLoop(int32_t pgain, int32_t dgain)
  44. {
  45.   m_pos = RCS_CENTER_POS;
  46.   m_pgain = pgain;
  47.   m_dgain = dgain;
  48.   m_prevError = 0x80000000L;
  49. }
  50.  
  51. void ServoLoop::update(int32_t error)
  52. {
  53.   long int vel;
  54.   char buf[32];
  55.   if (m_prevError!=0x80000000)
  56.   {
  57.     vel = (error*m_pgain + (error - m_prevError)*m_dgain)>>10;
  58.     //sprintf(buf, "%ld\n", vel);
  59.     //Serial.print(buf);
  60.     m_pos += vel;
  61.     if (m_pos>RCS_MAX_POS)
  62.       m_pos = RCS_MAX_POS;
  63.     else if (m_pos<RCS_MIN_POS)
  64.       m_pos = RCS_MIN_POS;
  65.  
  66.     //cprintf("%d %d %d\n", m_axis, m_pos, vel);
  67.   }
  68.   m_prevError = error;
  69. }
  70.  
  71.  
  72. Pixy pixy;
  73.  
  74. unsigned long time = 0;
  75.  
  76. void setup()
  77. {
  78.   Serial.begin(9600);
  79.   Serial.print("Starting...\n");
  80.  
  81.   pixy.init();
  82. }
  83.  
  84. void loop()
  85. {
  86.   static int i = 0;
  87.   int j;
  88.   uint16_t blocks;
  89.   char buf[32];
  90.   int32_t panError, tiltError;
  91.  
  92.   blocks = pixy.getBlocks();
  93.  
  94.   if (blocks)
  95.   {
  96.     panError = X_CENTER-pixy.blocks[0].x;
  97.     tiltError = pixy.blocks[0].y-Y_CENTER;
  98.    
  99.     panLoop.update(panError);
  100.     tiltLoop.update(tiltError);
  101.    
  102.     pixy.setServos(panLoop.m_pos, tiltLoop.m_pos);
  103.     i++;
  104.    
  105.     if (i%5==0)
  106.     {
  107.       if(pixy.blocks[j].x > 190) {
  108.         sr(20);
  109.         time = millis();
  110.       } else if(pixy.blocks[j].x < 130) {
  111.         sl(20);
  112.         time = millis();
  113.       } else {
  114.         ao();  
  115.       }
  116.     }
  117.   }  
  118.  
  119.   if(millis()  - time > 200) {
  120.     ao();
  121.     time = 0;  
  122.   }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement