thatsfrankie

Multiskills' OpenRoby Project

May 26th, 2020
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.23 KB | None | 0 0
  1. ///CodyColor 3.0 A.S. 2019/20
  2. // Team Multiskill
  3.  
  4. /* PINOUT ARDUINO - CodyColor 3.0
  5.  * [~] PWM
  6.  * [A] Analog Input
  7.  * [D] Digital Input
  8.  * [@] Used by
  9.  *
  10.  * [D] 13
  11.  * [D] 12
  12.  * [~] 11 @ MD Forward
  13.  * [~] 10 @ MS Backward
  14.  * [~] 9  @ MS Forward
  15.  * [D] 8
  16.  * [D] 7  @ S0
  17.  * [~] 6  @ S3
  18.  * [~] 5  @ S2
  19.  * [D] 4  @ OUT
  20.  * [~] 3  @ MD Backward
  21.  * [D] 2
  22.  * [TX] 1
  23.  * [RX] 0
  24.  *
  25.  */
  26.  
  27.  
  28. /*
  29.  * Funzioni di movimento:
  30.  *
  31.  * go(passo) -> esegue passo avanti
  32.  * stopM(M[2]); -> ferma il motore M
  33.  * dx(passo) -> gira soltanto a destra (senza passo avanti) (passo in ms)
  34.  * sx(passo) -> gira soltanto a sinistra (idem)
  35.  *
  36.  * front(M[2], speed) -> manda avanti il motore M con velocita speed
  37.  * back(M[2], speed) -> manda indietro
  38.  */
  39.  
  40. // Controllo sensore
  41. #define S0 7
  42. #define S2 5
  43. #define S3 6
  44. #define out 4
  45.  
  46.  
  47. // Controllo motori
  48.  
  49. int MD[2] = {11,3};
  50. int MS[2] = {9, 10};
  51.  
  52.  
  53.  
  54.  
  55. int pulse, R, G, B, toll, ColorR, ColorG, ColorB;
  56.  
  57.  
  58. ///////
  59. /////Parametri
  60.  
  61.  
  62.  
  63. int passo = 500;
  64. int pausa = 1000;
  65.  
  66. int velMD = 72;
  67. int velMS = 86.5;
  68.  
  69.  
  70. ////////
  71.  
  72. void setup(){  
  73.  
  74.   Serial.begin(9600);  // Comunicazione Seriale per Test
  75.  
  76.   for(int i = S0; i<= S3; i++){
  77.     pinMode(i,OUTPUT);
  78.   }
  79.  
  80.   pinMode(out, INPUT);
  81.   digitalWrite(S0, HIGH); //Scaling colore a 20%
  82.  
  83.   pinMode(MD[0], OUTPUT);
  84.   pinMode(MD[1], OUTPUT);
  85.   pinMode(MS[0], OUTPUT);
  86.   pinMode(MS[1], OUTPUT);
  87.  
  88.   go(passo);
  89. }
  90.  
  91.  
  92. void loop(){
  93.    delay(1000);
  94.   /* Combinazioni
  95.    *     S2   S3
  96.    * R: LOW  | LOW
  97.    * G: HIGH | HIGH
  98.    * B: LOW  | HIGH */
  99.  
  100.    //R
  101.    digitalWrite(S2, LOW);
  102.    digitalWrite(S3, LOW);
  103.    pulse = pulseIn(out, LOW); //frequenza
  104.    R = map(pulse,25,72,255,0);
  105.  
  106.    //G
  107.    digitalWrite(S2, HIGH);
  108.    digitalWrite(S3, HIGH);
  109.    pulse = pulseIn(out, LOW);
  110.    G = map(pulse,30,90,255,0);
  111.  
  112.    //B
  113.    digitalWrite(S2, LOW);
  114.    digitalWrite(S3, HIGH);
  115.    pulse = pulseIn(out, LOW);
  116.    B = map(pulse,25,70,255,0);
  117.  
  118. // Serie if/else per definire come muoversi
  119.  
  120.    if(isWhite(R,G,B) == true){  
  121.       ///FERMA TUTTO
  122.       stopAll(MD, MS);
  123.    }
  124.    else{
  125.       if(isGray(R,G,B) == true) {
  126.       ///VAI AVANTI
  127.       go(passo);
  128.      
  129.       Serial.println("GRIGIO");  // Comunicazione Seriale per Test
  130.       }
  131.       else if (isYellow(R,G,B) == true){
  132.         //SINISTRA
  133.         sx(passo);
  134.         delay(pausa);
  135.         go(passo);
  136.        
  137.         Serial.println("GIALLO");  // Comunicazione Seriale per Test
  138.         }  
  139.       else if (isRed(R,G,B) == true){
  140.           //DESTRA
  141.           dx(passo);
  142.           delay(pausa);
  143.           go(passo);
  144.          
  145.           Serial.println("ROSSO");  // Comunicazione Seriale per Test
  146.          }  
  147.         else
  148.           {
  149.           Serial.print("COLORE NON RILEVATO: ");  // Comunicazione Seriale per Test
  150.           Serial.print(R);
  151.           Serial.print(G);
  152.           Serial.println(B);
  153.           }  
  154.       }
  155.  
  156. }
  157.  
  158. //
  159. ////Funzioni di movimento
  160. //
  161.  
  162. void go(int timing){
  163.   moveM(MS, MD, true, true, velMS, velMD);
  164.   delay(timing);
  165.   stopAll(MD, MS);
  166.  
  167. }
  168. void dx(int timing){
  169.   moveM(MS, MD, true, false, velMS, velMD);
  170.   delay(timing);
  171.   stopAll(MS, MD);
  172. }
  173.  
  174. void sx(int timing){
  175.   moveM(MS, MD, false, true, velMS, velMD);
  176.   delay(timing);
  177.   stopAll(MS, MD);
  178. }
  179.  
  180. //Core for Movement
  181.  
  182. void moveM(int M1[2], int M2[2], bool fob1, bool fob2, int vel1, int vel2){  
  183.   /// inputs
  184.   /// M1 @ {int, int} (pin motore1)
  185.   /// M2 @ {int, int} (pin motore2)
  186.   /// fob1 @ bool (1 front, 0 back per M1)
  187.   /// fob2 @ bool (1 front, 0 back per M2)
  188.   /// vel1 @ int (velocita M1)
  189.   /// vel1 @ int (velocita M2)
  190.  
  191.   if(fob1 == true){
  192.     digitalWrite(M1[1], LOW);
  193.     analogWrite(M1[0], vel1);
  194.   }
  195.   else if(fob1 == false){
  196.     digitalWrite(M1[0], LOW);
  197.     analogWrite(M1[1], vel1);
  198.   }
  199.  
  200.   if(fob2 == true){
  201.     digitalWrite(M2[1], LOW);
  202.     analogWrite(M2[0], vel2);
  203.   }
  204.   else if(fob2== false){
  205.     digitalWrite(M2[0], LOW);
  206.     analogWrite(M2[1], vel2);
  207.   }
  208.  
  209. }
  210.  
  211. void stopAll(int M1[2], int M2[2]){
  212.   digitalWrite(M1[0], LOW);
  213.   digitalWrite(M1[1], LOW);
  214.   digitalWrite(M2[0], LOW);
  215.   digitalWrite(M2[1], LOW);
  216. }
  217.  
  218.  
  219.  
  220. //
  221. // Riconoscimento del Colore
  222. //
  223.  
  224. bool isGray(int R, int G, int B){
  225. //  352 352 368
  226.  
  227.   ColorR = 352;
  228.   ColorG = 352;
  229.   ColorB = 368;
  230.   bool iscolor = isColor(ColorR, ColorG, ColorB, R ,G ,B);
  231.   return iscolor;
  232.  }
  233.  
  234. bool isYellow(int R, int G, int B){
  235. // 374 365 357
  236.   ColorR = 374;
  237.   ColorG = 365;
  238.   ColorB = 357;
  239.   bool iscolor = isColor(ColorR, ColorG, ColorB, R ,G ,B);
  240.   return iscolor;
  241.  }
  242.  
  243. bool isRed(int R, int G, int B){
  244.   //352 293 311
  245.   ColorR = 352;
  246.   ColorG = 293;
  247.   ColorB = 311;
  248.   bool iscolor = isColor(ColorR, ColorG, ColorB, R ,G ,B);
  249.   return iscolor;
  250.  }
  251.  
  252.  bool isWhite(int R, int G, int B){
  253.   ColorR = 255;
  254.   ColorG = 255;
  255.   ColorB = 255;
  256.   bool iscolor = isColor(ColorR, ColorG, ColorB, R ,G ,B);
  257.   return iscolor;
  258.  }
  259.  
  260. bool isColor(int ColorR, int ColorG, int ColorB, int R, int G, int B){
  261.   toll = 15; //tolleranza
  262.  
  263.   if((R <= ColorR+toll) && (R>= ColorR-toll)){
  264.     if((G <= ColorG+toll) &&(G>= ColorG-toll)){
  265.       if((B <= ColorB+toll) && (B>= ColorB-toll)){
  266.       return true;
  267.       } else return false;
  268.     } else return false;
  269.   } else return false;
  270.  
  271. }
Add Comment
Please, Sign In to add comment