CalFlikka

velocityTestCode

Sep 18th, 2015
114
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //***************************** READ THIS ******************************
  2. //                          READ THE COMMENTS
  3. //**********************************************************************
  4. //  This code is part of TEAM FLIKKA BEAN's Intellectual Property
  5. //  Credit to those who deserve it, such as, but not limited to:
  6. //  Gordons Pi Projects - WiringPi library
  7. //  CharmedLabs - the pixy CmuCam5 & all the libaries and openSource code they have provided
  8. //  Matt Bridle - Linux Master
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <unistd.h>
  13. #include <signal.h>
  14. #include <string.h>
  15. #include <iostream>
  16. #include <pixy.h>               // camera Library
  17.  
  18. #include <time.h>
  19. #include <wiringPi.h>           //WiringPi Library for GPIO access
  20.  
  21.  
  22. using namespace std;
  23.  
  24. // Flikka Initialisation Function Prototypes //
  25. void initPixy(void);
  26. void initGPIO(void);
  27.  
  28. // FLIKKA BEAN FUNCTION PROTOTYPES //
  29. void kickBall(void);
  30. void unKickBall(void);
  31. void goalieMove(int stepsToMove);
  32. void distToMatch(void);
  33.  
  34. void fieldRep(void);
  35. void fieldPrint(void);
  36.  
  37. void ballTrackGoalie(void);
  38. void goalieKick(void);
  39.  
  40. void rampForKick(void);
  41. void spinMotor(void);
  42.  
  43. void xVelocity(void);
  44.  
  45. #define kPULSE 0    //PIN that sends LINEAR MOVEMENT PULSE to driver
  46. #define kDIR 1      //PIN that controls direction
  47. #define lPULSE 25
  48. #define lDIR0 2 //PIN that controls KICK direction
  49.  
  50. // Flikka Motor Defines //
  51. //~ #define kPULSE 20   //KICK pulse PIN - KICK PINS 20 downto 15
  52. //~ #define lPULSE 0    //LINEAR pulse PIN - LINEAR PINS 0 to 8
  53. //~
  54. //~ //** DIRECTION PINS **//
  55. //~ //LINEAR//
  56. //~ #define lDIR0 1     //PIN that controls LINEAR direction
  57. //~ #define lDIR1 2
  58. //~ #define lDIR2 3
  59. //~ #define lDIR3 4
  60. //~
  61. //~ //KICK//
  62. //~ #define kDIR 19     //PIN that controls KICK direction
  63.                     //~ //Common to all KICK drivers
  64. //~
  65. //~ //** ENABLE PINS **//
  66. //~ //LINEAR//
  67. //~ #define enLIN0 5
  68. //~ #define enLIN1 6
  69. //~ #define enLIN2 7
  70. //~ #define enLIN3 8
  71. //~
  72. //~ //KICK//
  73. //~ #define enKICK0 18      //PIN that sends KICK MOVEMENT PULSE to driver
  74. //~ #define enKICK1 17
  75. //~ #define enKICK2 16
  76. //~ #define enKICK3 15
  77.  
  78. #define pulsesKick 50           //pulses given to motor that kicks 200 pulses = 1 revolution
  79. #define pulsesLin 10            //Pulses for the Linear Motor to drive movement. 1 Full Step Mode revolution = 200 pulses = 100mm
  80. #define mmXPerPixel 0.0038125       //Given by length of playing field divded by X resolution
  81.  
  82. //** LIMIT SWITCH PINS **//
  83. #define LIMITkick 21
  84. #define LIMITlinear 22
  85.  
  86. //Zone Defines // These can be used to calibrate the various zones once the camera is properly installed in its final location //
  87.  
  88. #define halfway 160
  89.  
  90. // foosGoalie DEFINEs //
  91. #define goalieKickCalibration 5
  92. #define goalieAxleX 25
  93. #define goalieThirdLowerLimit 65
  94. #define goalieThirdUpperLimit 135
  95. #define goalieKickRangeUpperLimit 12
  96.  
  97. // foosDefender ZONE //
  98.  
  99.  
  100. //xVelocity Declarations
  101. //This global variable will be used based on the velocity of the ball
  102. int goalieKickRangeInnerLimit = 25;
  103.  
  104. int goalieKickCoordinate;
  105.  
  106. //Goalie Track Varibale Declarations
  107. int goaliePos = 100;
  108. int pixToMove;
  109. int distToMove;
  110. int stepsToMove;
  111.  
  112. int counterVelo = 0;
  113.  
  114. int locXprev = 0;   //COPY previous blocks into locXprev for trajectory calculations
  115. int locYprev = 0;
  116.  
  117. int pixFromGoalie;
  118. int wait = 1000;
  119. int defaultWait = 1000;
  120.  
  121. int distFromZero = 0;                       // Used to calculate the distance the goalie has moved
  122. int goalFLAG = 0;
  123. int counter = 0;
  124. int kickFlag = 0;
  125.  
  126. // Flikka Pixy Declarations //
  127. int lastY = 100;
  128.  
  129. //  Flikka Delays   //
  130. void delayMicrosecondsNoSleep(int);
  131.  
  132. #define ROW 32
  133. #define COLUMN 21
  134.  
  135.  
  136. // Pixy Block buffer //
  137. #define BLOCK_BUFFER_SIZE    25
  138. struct Block blocks[BLOCK_BUFFER_SIZE];
  139.  
  140. static bool run_flag = true;
  141.  
  142. void handle_SIGINT(int unused)
  143. {
  144.   // On CTRL+C - abort! //
  145.  
  146.   run_flag = false;
  147. }
  148.  
  149. int main(int argc, char * argv[])
  150. {
  151.     {
  152.     int kickAttempts = 0;
  153.     int i = 0;
  154.     initPixy();
  155.     initGPIO();
  156.    
  157.     //Catch CTRL+C (SIGINT) signals //
  158.     signal(SIGINT, handle_SIGINT);
  159.  
  160.  
  161.     //below this the program is stuck in loop until pixy.close() is executed and run_flag is set low
  162.     printf("\n**Team Flikka Bean 2015 FoosYourself Project**\nTracking ball...\n");
  163.    
  164.     while(run_flag)
  165.     {
  166.  
  167.     // Wait for new blocks to be available //
  168.     while(!pixy_blocks_are_new() && run_flag);
  169.  
  170.     // Get blocks from Pixy //
  171.     pixy_get_blocks(BLOCK_BUFFER_SIZE, &blocks[0]);
  172.     //~ printf("%d\n", counter++);
  173.     printf("locX: %3d locY: %3d \n", blocks[0].x, blocks[0].y);
  174.     printf("locXprev: %3d locYprev: %3d \n", locXprev, locYprev);
  175.    
  176.     xVelocity(); //also controls kicking
  177.    
  178.     if(blocks[0].x >= locXprev+1 | blocks[0].x <= locXprev-1){
  179.         ballHasNotMoved++;
  180.         if(ballHasNotMoved > 5){
  181.             printf("ball is stuck or in goal /n");
  182.         }
  183.     }
  184.    
  185. #if 0   //toggle to enable/disbale goalieKick program
  186.     //~ if(blocks[0].x < goalieKickCoordinate && kickAttempts < 2){
  187.             //~ printf("Attempt Kick %d\n", (kickAttempts+1));
  188.             //~ goalieKick();
  189.         //~ }
  190.         //~
  191.     //~ //Did ball get kicked?
  192.     //~ if(blocks[0].x < goalieKickCoordinate && (kickAttempts >= 1 && kickAttempts < 2)){
  193.             //~ kickAttempts++;
  194.             //~ printf("Attempting to kick again\n");
  195.             //~ goalieKick();
  196.             //~ printf("Attempts = %d\n", kickAttempts);
  197.             //~ }
  198.             //~
  199.     //~ if(blocks[0].x < goalieKickCoordinate && kickAttempts >= 2 && locXprev == blocks[0].x){
  200.         //~ printf("Ball is stuck or in the goal\n");
  201.             //~ }
  202.     //~
  203.     //~ if(blocks[0].x > goalieKickCoordinate && locXprev < goalieKickCoordinate){
  204.             //~ kickAttempts = 0;
  205.             //~ printf("Ball Kicked\n");
  206.             //~ }
  207.  
  208. //linear ball location matching for the goalie
  209.  
  210. //~ if(blocks[0].x < 260 && blocks[0].y < goalieThirdUpperLimit && blocks[0].y > goalieThirdLowerLimit && ((blocks[0].y < lastY-2) | (blocks[0].y > lastY+2))){
  211. //~
  212.         //~ ballTrackGoalie();
  213.         //~ lastY = blocks[0].y;
  214.         //~ if(blocks[0].x < goalieKickCoordinate){
  215.             //~ printf("goalieKick\n");
  216.             //~ goalieKick();
  217.         //~ }
  218.     //~ }
  219. #endif 
  220.     i++;
  221.     locXprev = blocks[0].x; //COPY previous blocks into locXprev for trajectory calculations
  222.     locYprev = blocks[0].y;
  223.   }
  224. }
  225.   pixy_close();
  226.  
  227. }
  228.  
  229.  
  230.  
  231. void xVelocity(){
  232.     // This calculates the velocity of the ball along the x axis,
  233.     int pixelsMoved = blocks[0].x-locXprev;
  234.     float distanceMoved = mmXPerPixel*pixelsMoved;
  235.     float veloX= distanceMoved*50;
  236.    
  237.     int xCoordinateToKick = goalieAxleX+veloX+goalieKickCalibration;
  238.     if(xCoordinateToKick >= blocks[0].x){
  239.         goalieKick();
  240.     }
  241.     //~ printf("\n************************\npixelsMoved: %d \ndistanceMoved : %f mmXPerPixel\nveloX : %f", pixelsMoved, distanceMoved, veloX);
  242.     }
  243.    
  244.    
  245. void initPixy(){
  246.     int pixy_init_status;
  247.     // Connect to Pixy //
  248.     pixy_init_status = pixy_init();
  249.  
  250.     // Was there an error initializing pixy? //
  251.     if(!pixy_init_status == 0)
  252.     {
  253.         // Error initializing Pixy //
  254.         printf("pixy_init(): ");
  255.         pixy_error(pixy_init_status);
  256.  
  257.         pixy_init_status = 1;
  258.     }
  259.     }
  260.  
  261. void initGPIO(){
  262.     //initialise GPIO
  263.     wiringPiSetup();
  264.    
  265.     //~ pinMode(enKICK0, OUTPUT);
  266.     //~ pinMode(enKICK1, OUTPUT);
  267.     //~ pinMode(enKICK2, OUTPUT);
  268.     //~ pinMode(enKICK3, OUTPUT);
  269.     pinMode(kPULSE, OUTPUT);
  270.     pinMode(kDIR, OUTPUT);
  271.     pinMode(lPULSE, OUTPUT);
  272.     pinMode(lDIR0, OUTPUT);
  273.    
  274.     }
  275.  
  276. void goalieKick(){
  277.     digitalWrite(kDIR, 1);
  278.     for(int i = 0; i < pulsesKick; i++){            //LOOP through i iterations
  279.             digitalWrite(kPULSE, HIGH);
  280.             delayMicrosecondsNoSleep(wait);
  281.             digitalWrite(kPULSE, LOW);
  282.             delayMicrosecondsNoSleep(wait);
  283.             if(wait > 700){
  284.                 wait = wait-10;
  285.             }
  286.         }
  287.            
  288.     for(int i = 0; i < pulsesKick; i++){            //LOOP through i iterations
  289.             digitalWrite(kPULSE, HIGH);
  290.             delayMicrosecondsNoSleep(wait);
  291.             digitalWrite(kPULSE, LOW);
  292.             delayMicrosecondsNoSleep(wait);
  293.             if(wait < 1000){
  294.                 wait = wait+10;
  295.             }
  296.             }
  297.     wait = defaultWait;
  298.     digitalWrite(kDIR, 0);
  299.     for(int i = 0; i < pulsesKick; i++){            //LOOP through i iterations
  300.             digitalWrite(kPULSE, HIGH);
  301.             delayMicrosecondsNoSleep(wait);
  302.             digitalWrite(kPULSE, LOW);
  303.             delayMicrosecondsNoSleep(wait);
  304.             if(wait > 700){
  305.                 wait = wait-10;
  306.             }
  307.             }
  308.     for(int i = 0; i < pulsesKick; i++){            //LOOP through i iterations
  309.             digitalWrite(kPULSE, HIGH);
  310.             delayMicrosecondsNoSleep(wait);
  311.             digitalWrite(kPULSE, LOW);
  312.             delayMicrosecondsNoSleep(wait);
  313.             if(wait > 1000){
  314.                 wait = wait+10;
  315.             }
  316.             }
  317.         wait = defaultWait;
  318.         }
  319.  
  320.    
  321.    
  322. void kickBall(){
  323.     //KICK
  324.     kickFlag = 1;
  325.     digitalWrite(kDIR, LOW); //ensure DIR is LOW first
  326.     for(int i = 0; i < pulsesKick; i++){            //LOOP through i iterations
  327.         digitalWrite(kPULSE, HIGH);
  328.         delayMicrosecondsNoSleep(wait);
  329.         digitalWrite(kPULSE, LOW);
  330.         delayMicrosecondsNoSleep(wait);
  331.         }
  332.     }
  333.  
  334. void unKickBall(){
  335.     //UN-KICK
  336.     kickFlag = 0;
  337.     digitalWrite(kDIR,HIGH);
  338.  
  339.     for(int i = 0; i < pulsesKick; i++){            //LOOP through i iterations
  340.         digitalWrite(kPULSE, HIGH);
  341.         delayMicrosecondsNoSleep(wait);
  342.         digitalWrite(kPULSE, LOW);
  343.         delayMicrosecondsNoSleep(wait);
  344.         }
  345.     }
  346.  
  347. void goalieMove(int stepsToMove){
  348.     printf("goalie moving\n");
  349.     for(int i = 0; i < stepsToMove; i++){           //LOOP through stepsToMove iterations
  350.         digitalWrite(lPULSE, HIGH);
  351.         delayMicrosecondsNoSleep(wait);
  352.         digitalWrite(lPULSE, LOW);
  353.         delayMicrosecondsNoSleep(wait);
  354.         }
  355.         printf("goalie still \n");
  356.     }
  357.  
  358. void ballTrackGoalie(){
  359.     pixFromGoalie = abs(goaliePos-blocks[0].y);
  360.     distToMove = pixFromGoalie*3.425;
  361.     int stepsToMoveReal = distToMove*2;
  362.     stepsToMove = stepsToMoveReal*.5;
  363.    
  364.     printf("\n ----------------\n pixFromGoalie = %d pixels\n", pixFromGoalie);
  365.     printf("distToMove = %d mm \n", distToMove);
  366.     printf("stepsToMove = %d steps\n", stepsToMove);
  367.     printf("locY = %d \n", blocks[0].y);
  368.     if(blocks[0].y > lastY){
  369.         printf("locY > lastY\n");
  370.         digitalWrite(lDIR0,0);
  371.         goalieMove(stepsToMove);
  372.         goaliePos = goaliePos+pixFromGoalie;
  373.         printf("goaliePos = %d \n", goaliePos);
  374.         stepsToMove = 0;
  375.     }
  376.     if(blocks[0].y == goaliePos){
  377.         printf("On point\n");
  378.     }
  379.     if(blocks[0].y < lastY){
  380.         printf("locY < lastY\n");
  381.         digitalWrite(lDIR0,1);
  382.         goalieMove(stepsToMove);
  383.         goaliePos = goaliePos-pixFromGoalie;
  384.         printf("goaliePos = %d \n", goaliePos);
  385.         stepsToMove = 0;
  386.     }
  387. }
  388. void fieldPrint(void){
  389.     char field[ROW][COLUMN] = { {'x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x'},{'x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x'},{'x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x'},{'x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x'},
  390.                                 {'x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x'},{'x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x'},{'x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x'},{'x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x'},
  391.                                 {'x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x'},{'x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x'},{'x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x'},{'x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x'},
  392.                                 {'x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x'},{'x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x'},{'x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x'},{'x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x'},
  393.                                 {'x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x'},{'x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x'},{'x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x'},{'x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x'},
  394.                                 {'x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x'},{'x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x'},{'x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x'},{'x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x'},
  395.                                 {'x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x'},{'x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x'},{'x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x'},{'x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x'},
  396.                                 {'x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x'},{'x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x'},{'x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x'},{'x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x','x'},
  397.                             };
  398.     //Hold the (X & Y)/10 location of ball
  399.     int fieldX1 = (0.1*blocks[0].x);
  400.     int fieldY1 = (0.1*blocks[0].y);
  401.  
  402.     //Writes an O to the location the ball is in
  403.     field[fieldX1][fieldY1] = 'O';
  404.  
  405.     // Prints each array contained in the 2D array 'field'
  406.     for(int w = 0; w != 32; w++){
  407.             printf("%s \n", field[w]);
  408.     }
  409. }
  410.  
  411. void delayMicrosecondsNoSleep(int delay_us){
  412.         long int start_time;
  413.         long int time_difference;
  414.         struct timespec gettime_now;
  415.         clock_gettime(CLOCK_REALTIME, &gettime_now);
  416.         start_time = gettime_now.tv_nsec;
  417.         while(1)
  418.         {
  419.             clock_gettime(CLOCK_REALTIME, &gettime_now);
  420.             time_difference = gettime_now.tv_nsec-start_time;
  421.             if(time_difference < 0){
  422.                 time_difference += 1000000000;
  423.                 //~ printf("TD = %ld \n"  , time_difference);
  424.             }
  425.             if(time_difference >(delay_us*1000))
  426.             //~ printf("TD = %ld \n"  , time_difference);
  427.             break;
  428.         }
  429.         }
  430.    
  431. void spinMotor(){
  432.     int run = 0;
  433.    
  434.     if(run == 0){
  435.     digitalWrite(kDIR, HIGH);
  436.         for(int k = 0; k < pulsesKick; k++){            //LOOP through k iterations
  437.             digitalWrite(kPULSE, 1);
  438.             delayMicrosecondsNoSleep(wait);
  439.             digitalWrite(kPULSE, 0);
  440.             delayMicrosecondsNoSleep(wait);
  441.             }
  442.    
  443.     digitalWrite(kDIR, LOW);
  444.     for(int h = 0; h < pulsesKick; h++){            //LOOP through h iterations
  445.             digitalWrite(kPULSE, 1);
  446.             delayMicrosecondsNoSleep(wait);
  447.             digitalWrite(kPULSE, 0);
  448.             delayMicrosecondsNoSleep(wait);
  449.             }
  450. }
  451. }
  452.  
  453. int setupLimit(){
  454.     while(LIMITkick != 0){
  455.         delayMicrosecondsNoSleep(1000);
  456.         digitalWrite(kPULSE, 0);
  457.         delayMicrosecondsNoSleep(1000);
  458.         digitalWrite(kPULSE, 1);
  459.         delayMicrosecondsNoSleep(1000);
  460.         if(LIMITkick == 0 ){
  461.             printf("ZEROED\n");
  462.             return 1;
  463.         }
  464.     }
  465. }
RAW Paste Data