Advertisement
patryk

CANDY_SORT

May 13th, 2015
563
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.40 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <avr/delay.h>
  3. #include <avr/interrupt.h>
  4. #include "tcs3200.h"
  5.  
  6. #define CL_PURPLE     0x701
  7. #define CL_YELLOW     0x702
  8. #define CL_GREEN      0x703
  9. #define CL_ORANGE     0x704
  10. #define CL_RED        0x705
  11.  
  12. #define POS_COLLECT   0x801
  13. #define POS_SENSOR    0x802
  14. #define POS_DROP      0x803
  15.  
  16. #define SRV_STOP      0x555
  17.  
  18. #define ERR_NO_CANDY  0x999
  19.  
  20. #define CL_PURPLE_R   102
  21. #define CL_PURPLE_G   81
  22. #define CL_PURPLE_B   103
  23. #define CL_PURPLE_C   305
  24.  
  25. #define CL_YELLOW_R   139
  26. #define CL_YELLOW_G   110
  27. #define CL_YELLOW_B   115
  28. #define CL_YELLOW_C   384
  29.  
  30. #define CL_GREEN_R    108
  31. #define CL_GREEN_G    97
  32. #define CL_GREEN_B    106
  33. #define CL_GREEN_C    331
  34.  
  35. #define CL_ORANGE_R   130
  36. #define CL_ORANGE_G   91
  37. #define CL_ORANGE_B   108
  38. #define CL_ORANGE_C   348
  39.  
  40. #define CL_RED_R      113
  41. #define CL_RED_G      82
  42. #define CL_RED_B      103
  43. #define CL_RED_C      317
  44.  
  45.  
  46. volatile int measureReady;
  47. volatile int overflowTicks;
  48. volatile int actuallArmPosition;
  49.  
  50. void moveArm (int colorID) {
  51.     DDRB |= (1 << PB1);     // PWM Output
  52.     TCCR1A = 0;
  53.     ICR1 = 19999;           // 20ms pulse width - 50Hz
  54.  
  55.     TCCR1A = (1 << WGM11);
  56.     TCCR1B = (1 << WGM13) | (1 << WGM12) | (1 << CS11);
  57.  
  58.     TCCR1A |= 2 << 6;       // PB1 as PWM Output
  59.  
  60.     actuallArmPosition = colorID;
  61.  
  62.     switch (colorID) {
  63.         case CL_PURPLE:
  64.             OCR1A = 600;        // PURPLE
  65.             break;
  66.  
  67.         case CL_YELLOW:
  68.             OCR1A = 1050;       // YELLOW
  69.             break;
  70.  
  71.         case CL_GREEN:
  72.             OCR1A = 1500;       // GREEN
  73.             break;
  74.  
  75.         case CL_ORANGE:
  76.             OCR1A = 1950;       // ORANGE
  77.             break;
  78.  
  79.         case CL_RED:
  80.             OCR1A = 2400;       // RED
  81.             break;
  82.  
  83.         case SRV_STOP:          // SERVO STOP
  84.             OCR1A = 0;
  85.             break;
  86.     }
  87. }
  88.  
  89. void spinWheel (int moveTo) {
  90.     DDRB |= (1 << PB2);
  91.     TCCR1A = 0;
  92.     ICR1 = 19999;
  93.  
  94.     TCCR1A = (1 << WGM11);
  95.     TCCR1B = (1 << WGM13) | (1 << WGM12) | (1 << CS11);
  96.  
  97.     TCCR1A |= 2 << 4;
  98.  
  99.     switch (moveTo) {
  100.         case POS_COLLECT:       // POSITION 1
  101.             OCR1B = 2350;
  102.             break;
  103.  
  104.         case POS_SENSOR:        // POSITION 2
  105.             OCR1B = 1420;
  106.             break;
  107.  
  108.         case POS_DROP:          // POSITION 3
  109.             OCR1B = 600;
  110.             break;
  111.  
  112.         case SRV_STOP:          // SERVO STOP
  113.             OCR1B = 0;
  114.             break;
  115.     }
  116. }
  117.  
  118. void collectCandy() {
  119.     spinWheel(POS_COLLECT);
  120.     _delay_ms(1500);
  121.     spinWheel(SRV_STOP);
  122. }
  123.  
  124. uint32_t checkRed() {
  125.     PORTC &= ~(1 << PC4);   // S2 - L
  126.     PORTC &= ~(1 << PC5);   // S3 - L
  127.     _delay_ms(100);
  128.     return TCSMeasure();
  129. }
  130.  
  131.  
  132. uint32_t checkBlue() {
  133.     PORTC &= ~(1 << PC4);   // S2 - L
  134.     PORTC |= (1 << PC5);    // S3 - H
  135.     _delay_ms(100);
  136.     return TCSMeasure();
  137. }
  138.  
  139. uint32_t checkGreen() {
  140.     PORTC |= (1 << PC4);    // S2 - H
  141.     PORTC |= (1 << PC5);    // S3 - H
  142.     _delay_ms(100);
  143.     return TCSMeasure();
  144. }
  145.  
  146. uint32_t checkClear(){
  147.     PORTC |= (1<<PC4);
  148.     PORTC &= ~(1<<PC5);
  149.     _delay_ms(100);
  150.     return TCSMeasure();
  151. }
  152.  
  153.  
  154. int pickColor(uint32_t R, uint32_t G, uint32_t B, uint32_t C) {
  155.     int CL_PURPLE_MISTAKE = 0;
  156.     int CL_YELLOW_MISTAKE = 0;
  157.     int CL_GREEN_MISTAKE = 0;
  158.     int CL_ORANGE_MISTAKE = 0;
  159.     int CL_RED_MISTAKE = 0;
  160.  
  161. //---PURPLE---
  162.     if (CL_PURPLE_R > R) CL_PURPLE_MISTAKE += CL_PURPLE_R - R;
  163.     else if (CL_PURPLE_R <= R) CL_PURPLE_MISTAKE += R - CL_PURPLE_R;
  164.  
  165.     if (CL_PURPLE_G > G) CL_PURPLE_MISTAKE += CL_PURPLE_G - G;
  166.     else if (CL_PURPLE_G <= G) CL_PURPLE_MISTAKE += G - CL_PURPLE_G;
  167.  
  168.     if (CL_PURPLE_B > B) CL_PURPLE_MISTAKE += CL_PURPLE_B - B;
  169.     else if (CL_PURPLE_B <= B) CL_PURPLE_MISTAKE += B - CL_PURPLE_B;
  170.  
  171.     if (CL_PURPLE_C > C) CL_PURPLE_MISTAKE += CL_PURPLE_C - C;
  172.     else if (CL_PURPLE_C <= C) CL_PURPLE_MISTAKE += C - CL_PURPLE_C;
  173.    
  174.  
  175. //---YELLOW
  176.     if (CL_YELLOW_R > R) CL_YELLOW_MISTAKE += CL_YELLOW_R - R;
  177.     else if (CL_YELLOW_R <= R) CL_YELLOW_MISTAKE += R - CL_YELLOW_R;
  178.  
  179.     if (CL_YELLOW_G > G) CL_YELLOW_MISTAKE += CL_YELLOW_G - G;
  180.     else if (CL_YELLOW_G <= G) CL_YELLOW_MISTAKE += G - CL_YELLOW_G;
  181.  
  182.     if (CL_YELLOW_B > B) CL_YELLOW_MISTAKE += CL_YELLOW_B - B;
  183.     else if (CL_YELLOW_B <= B) CL_YELLOW_MISTAKE += B - CL_YELLOW_B;
  184.  
  185.     if (CL_YELLOW_C > C) CL_YELLOW_MISTAKE += CL_YELLOW_C - C;
  186.     else if (CL_YELLOW_C <= C) CL_YELLOW_MISTAKE += C - CL_YELLOW_C;
  187.    
  188.  
  189. //---GREEN
  190.     if (CL_GREEN_R > R) CL_GREEN_MISTAKE += CL_GREEN_R - R;
  191.     else if (CL_GREEN_R <= R) CL_GREEN_MISTAKE += R - CL_GREEN_R;
  192.  
  193.     if (CL_GREEN_G > G) CL_GREEN_MISTAKE += CL_GREEN_G - G;
  194.     else if (CL_GREEN_G <= G) CL_GREEN_MISTAKE += G - CL_GREEN_G;
  195.  
  196.     if (CL_GREEN_B > B) CL_GREEN_MISTAKE += CL_GREEN_B - B;
  197.     else if (CL_GREEN_B <= B) CL_GREEN_MISTAKE += B - CL_GREEN_B;
  198.  
  199.     if (CL_GREEN_C > C) CL_GREEN_MISTAKE += CL_GREEN_C - C;
  200.     else if (CL_GREEN_C <= C) CL_GREEN_MISTAKE += C - CL_GREEN_C;
  201.  
  202.  
  203. //---ORANGE
  204.     if (CL_ORANGE_R > R) CL_ORANGE_MISTAKE += CL_ORANGE_R - R;
  205.     else if (CL_ORANGE_R <= R) CL_ORANGE_MISTAKE += R - CL_ORANGE_R;
  206.  
  207.     if (CL_ORANGE_G > G) CL_ORANGE_MISTAKE += CL_ORANGE_G - G;
  208.     else if (CL_ORANGE_G <= G) CL_ORANGE_MISTAKE += G - CL_ORANGE_G;
  209.  
  210.     if (CL_ORANGE_B > B) CL_ORANGE_MISTAKE += CL_ORANGE_B - B;
  211.     else if (CL_ORANGE_B <= B) CL_ORANGE_MISTAKE += B - CL_ORANGE_B;
  212.  
  213.     if (CL_ORANGE_C > C) CL_ORANGE_MISTAKE += CL_ORANGE_C - C;
  214.     else if (CL_ORANGE_C <= C) CL_ORANGE_MISTAKE += C - CL_ORANGE_C;
  215.  
  216. //---RED
  217.     if (CL_RED_R > R) CL_RED_MISTAKE += CL_RED_R - R;
  218.     else if (CL_RED_R <= R) CL_RED_MISTAKE += R - CL_RED_R;
  219.  
  220.     if (CL_RED_G > G) CL_RED_MISTAKE += CL_RED_G - G;
  221.     else if (CL_RED_G <= G) CL_RED_MISTAKE += G - CL_RED_G;
  222.  
  223.     if (CL_RED_B > B) CL_RED_MISTAKE += CL_RED_B - B;
  224.     else if (CL_RED_B <= B) CL_RED_MISTAKE += B - CL_RED_B;
  225.  
  226.     if (CL_RED_C > C) CL_RED_MISTAKE += CL_RED_C - C;
  227.     else if (CL_RED_C <= C) CL_RED_MISTAKE += C - CL_RED_C;
  228.  
  229.  
  230.     int minimumMistake = 1000;
  231.     int closestColor = 0;
  232.  
  233.     if (CL_PURPLE_MISTAKE < minimumMistake) {
  234.         minimumMistake = CL_PURPLE_MISTAKE;
  235.         closestColor = CL_PURPLE;
  236.     }
  237.  
  238.     if (CL_YELLOW_MISTAKE < minimumMistake) {
  239.         minimumMistake = CL_YELLOW_MISTAKE;
  240.         closestColor = CL_YELLOW;
  241.     }
  242.  
  243.     if (CL_GREEN_MISTAKE < minimumMistake) {
  244.         minimumMistake = CL_GREEN_MISTAKE;
  245.         closestColor = CL_GREEN;
  246.     }
  247.  
  248.     if (CL_ORANGE_MISTAKE < minimumMistake) {
  249.         minimumMistake = CL_ORANGE_MISTAKE;
  250.         closestColor = CL_ORANGE;
  251.     }
  252.  
  253.     if (CL_RED_MISTAKE < minimumMistake) {
  254.         minimumMistake = CL_RED_MISTAKE;
  255.         closestColor = CL_RED;
  256.     }
  257.  
  258.     return closestColor;
  259. }
  260. void checkColor() {
  261.     spinWheel(POS_SENSOR);
  262.     _delay_ms(700);
  263.     //spinWheel(SRV_STOP);
  264.     //int nextPosition;
  265.     uint32_t R=0;
  266.     uint32_t G=0;
  267.     uint32_t B=0;
  268.     uint32_t C=0;
  269.  
  270.     int i;
  271.  
  272.     for (i = 0 ; i < 5 ; i++) R += checkRed()/100;
  273.     for (i = 0 ; i < 5 ; i++) G += checkGreen()/100;
  274.     for (i = 0 ; i < 5 ; i++) B += checkBlue()/100;
  275.     for (i = 0 ; i < 5 ; i++) C += checkClear()/100;
  276.  
  277.     R = R/5;
  278.     G = G/5;
  279.     B = B/5;
  280.     C = C/5;
  281.  
  282.     int nextPosition = pickColor(R, G, B, C);
  283.  
  284.     if (nextPosition != ERR_NO_CANDY) {
  285.         moveArm(nextPosition);
  286.         _delay_ms(1000);
  287.         moveArm(SRV_STOP);
  288.     }
  289. }
  290.  
  291.  
  292. void dropCandy() {
  293.     spinWheel(POS_DROP);
  294.     _delay_ms(700);
  295. }
  296.  
  297. void moveServo(int pulseWidth) {
  298.     TCCR1A = 0;
  299.     ICR1 = 19999;
  300.  
  301.     TCCR1A = (1 << WGM11);
  302.     TCCR1B = (1 << WGM13) | (1 << WGM12) | (1 << CS11);
  303.  
  304.     TCCR1A |= 2 << 6;
  305.  
  306.     OCR1A = pulseWidth;
  307. }
  308.  
  309. void MAIN_INIT() {
  310.     DDRC |= (1 << PC5) | (1 << PC4) | (1 << PC3) | (1 << PC2) | (1 << PC1);
  311.     PORTC |= (1 << PC3);
  312.  
  313.     moveArm(CL_GREEN);
  314.     _delay_ms(1000);
  315.     moveArm(SRV_STOP);
  316.  
  317.     spinWheel(POS_COLLECT);
  318.     _delay_ms(1000);
  319.     spinWheel(SRV_STOP);
  320. }
  321.  
  322. void MAIN_LOOP() {
  323.     while (1) {
  324.         collectCandy();
  325.         checkColor();
  326.         dropCandy();
  327.     }
  328. }
  329.  
  330. int main() {
  331.  
  332.     moveArm(CL_RED);
  333.     _delay_ms(1000);
  334.     moveArm(SRV_STOP);
  335.  
  336.     MAIN_INIT();
  337.     MAIN_LOOP();
  338.  
  339.     return 0;
  340. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement