Advertisement
Guest User

Untitled

a guest
May 19th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.37 KB | None | 0 0
  1. #include "nnxt.h"
  2. #define q0 0
  3. #define q1 1
  4. #define q2 2
  5. #define q3 3
  6. #define q4 4
  7. #define q5 5
  8. #define q6 6
  9. #define q7 7
  10. #define q8 8
  11. #define q9 9
  12. #define q10 10
  13. #define q11 11
  14. #define q12 12
  15. #define tasterR 13
  16. #define tasterL 14
  17. #define goback 15
  18.  
  19. volatile uint32_t events = 0;
  20. volatile tasterL = 0;
  21. volatile tasterR = 0;
  22.  
  23. //Events
  24.  
  25. void SetEvent (uint32_t shift)
  26. {
  27.     taskENTER_CRITICAL();
  28.     uint32_t maske = 0x0001;
  29.     maske = maske << shift;
  30.     events = events | maske;
  31.     taskEXIT_CRITICAL();
  32. }
  33.  
  34. int EventIsSet (uint32_t shift)
  35. {
  36.     taskENTER_CRITICAL();
  37.     uint32_t tempevents = 0;
  38.     uint32_t maske = 0x00001;
  39.     maske <<= shift;
  40.     tempevents = events;
  41.     tempevents &= maske;
  42.     tempevents >>= shift;
  43.     taskEXIT_CRITICAL();
  44.  
  45.     if (tempevents==1)
  46.         return 1;
  47.     else
  48.         return 0;
  49. }
  50.  
  51. void ClearEvent(uint32_t shift)
  52. {
  53.     taskENTER_CRITICAL();
  54.     uint32_t maske = 0x0001;
  55.     maske <<= shift;
  56.     maske ^= 0xFFFF;
  57.     events &= maske;
  58.  
  59.     taskEXIT_CRITICAL();
  60. }
  61.  
  62.  
  63. //Timer
  64.  
  65. volatile uint16_t timeamount;
  66. volatile uint16_t event_timer;
  67. volatile uint32_t timerstart;
  68. volatile uint16_t customsystime;
  69.  
  70. void SetTimer (int time, uint32_t event){
  71.  
  72.     taskENTER_CRITICAL();
  73.     timeamount = time;
  74.     event_timer = event;
  75.     timerstart = 0;
  76.     taskEXIT_CRITICAL();
  77. }
  78.  
  79. void StartTimer(){
  80.  
  81.     taskENTER_CRITICAL();
  82.     timerstart = 1;
  83.     customsystime = GetSysTime();
  84.     taskEXIT_CRITICAL();
  85. }
  86.  
  87. void CancelTimer()
  88. {
  89.     taskENTER_CRITICAL();
  90.     timerstart = 0;
  91.     taskEXIT_CRITICAL();
  92. }
  93.  
  94. void TimerTask(){
  95.     while(1){
  96.  
  97.         if(timerstart == 1)
  98.         {
  99.             if(customsystime+timeamount <= GetSysTime())
  100.             {
  101.                 CancelTimer();
  102.                 SetEvent(event_timer);
  103.             }
  104.         }
  105.     }
  106.     Delay(10);
  107. }
  108.  
  109. //Funktions
  110.  
  111. void driving(){
  112.     motorDir = Motor_dir_forward;
  113.     Motor_Drive(Port_A,motorDir,35);
  114.     Motor_Drive(Port_B,motorDir,35);
  115.     Delay(1000);
  116. }
  117.  
  118. void rotateL(){
  119.  
  120.     Motor_Drive(Port_A,Motor_drive_forward,35);
  121.     Motor_Drive(Port_B,Motor_drive_backward,35);
  122.     Delay(330);
  123. }
  124.  
  125. void rotateR(){
  126.  
  127.     Motor_Drive(Port_A,Motor_drive_backward,35);
  128.     Motor_Drive(Port_B,Motor_drive_forward,35);
  129.     Delay(330);
  130. }
  131.  
  132. //Tasks
  133. void task1()
  134. {
  135.     SetEvent(q0);
  136.    
  137.     while(1)
  138.     {
  139.  
  140.     if(EventIsSet(tasterR) && EventIsSet(q0)){
  141.     ClearEvent(q0);
  142.     ClearEvent(tasterR);
  143.     rotateR();
  144.     driving();
  145.     SetTimer(5000, goback);
  146.     StartTimer();
  147.     SetEvent(q10);
  148.     }
  149.    
  150.     if(EventisSet(tasterL) && EventIsSet(q0)){  
  151.     ClearEvent(q0);
  152.     ClearEvent(tasterL);
  153.     rotateL();
  154.     driving();
  155.     SetTimer(5000, goback);
  156.     StartTimer();
  157.     SetEvent(q3);
  158.     }
  159.    
  160.     if(tasterR && EventIsSet(q1)){  
  161.     CancelTimer();
  162.     ClearEvent(q1);
  163.     ClearEvent(tasterR);
  164.     rotateR();
  165.     rotateR();
  166.     driving();
  167.     SetTimer(5000, goback);
  168.     StartTimer();
  169.     SetEvent(q7);
  170.     }
  171.    
  172.     if(EventIsSet(tasterL) && EventIsSet(q1)){  
  173.     CancelTimer();
  174.     ClearEvent(q1);
  175.     ClearEvent(tasterL);
  176.     SetTimer(5000, goback);
  177.     StartTimer();
  178.     SetEvent(q1);
  179.     }
  180.    
  181.     if(EventIsSet(goback) && EventIsSet(q1)){  
  182.     CancelTimer();
  183.     ClearEvent(q1);
  184.     ClearEvent(goback);
  185.     rotateL();
  186.     rotateL();
  187.     driving();
  188.     driving();
  189.     driving();
  190.     rotateL();
  191.     SetEvent(q0);
  192.     }
  193.    
  194.     if(tasterR && EventIsSet(q2)){  
  195.     CancelTimer();
  196.     ClearEvent(q2);
  197.     ClearEvent(tasterR);
  198.     rotateR();
  199.     rotateR();
  200.     driving();
  201.     SetTimer(5000, goback);
  202.     StartTimer();
  203.     SetEvent(q8);
  204.     }
  205.    
  206.     if(EventIsSet(tasterL) && EventIsSet(q2)){  
  207.     CancelTimer();
  208.     ClearEvent(q2);
  209.     ClearEvent(tasterL);
  210.     driving();
  211.     SetTimer(5000, goback);
  212.     StartTimer();
  213.     SetEvent(q1);
  214.     }
  215.    
  216.     if(EventIsSet(goback) && EventIsSet(q2)){  
  217.     CancelTimer();
  218.     ClearEvent(q2);
  219.     ClearEvent(goback);
  220.     rotateL();
  221.     rotateL();
  222.     driving();
  223.     driving();
  224.     rotateL();
  225.     SetEvent(q0);
  226.     }
  227.    
  228.     if(tasterR && EventIsSet(q3)){  
  229.     CancelTimer();
  230.     ClearEvent(q3);
  231.     ClearEvent(tasterR);
  232.     rotateR();
  233.     rotateR();
  234.     driving();
  235.     SetTimer(5000, goback);
  236.     StartTimer();
  237.     SetEvent(q9);
  238.     }
  239.    
  240.     if(EventIsSet(tasterL) && EventIsSet(q3)){  
  241.     CancelTimer();
  242.     ClearEvent(q3);
  243.     ClearEvent(tasterL);
  244.     driving();
  245.     SetTimer(5000, goback);
  246.     StartTimer();
  247.     SetEvent(q2);
  248.     }
  249.    
  250.     if(EventIsSet(goback) && EventIsSet(q3)){  
  251.     CancelTimer();
  252.     ClearEvent(q3);
  253.     ClearEvent(goback);
  254.     rotateL();
  255.     rotateL();
  256.     driving();
  257.     rotateL();
  258.     SetEvent(q0);
  259.     }
  260.    
  261.     if(tasterR && EventIsSet(q4)){  
  262.     CancelTimer();
  263.     ClearEvent(q4);
  264.     ClearEvent(tasterR);
  265.     rotateR();
  266.     rotateR();
  267.     driving();
  268.     SetTimer(5000, goback);
  269.     StartTimer();
  270.     SetEvent(q10);
  271.     }
  272.    
  273.     if(EventIsSet(tasterL) && EventIsSet(q4)){  
  274.     CancelTimer();
  275.     ClearEvent(q4);
  276.     ClearEvent(tasterL);
  277.     driving();
  278.     SetTimer(5000, goback);
  279.     StartTimer();
  280.     SetEvent(q3);
  281.     }
  282.    
  283.     if(EventIsSet(goback) && EventIsSet(q4)){  
  284.     CancelTimer();
  285.     ClearEvent(q1);
  286.     ClearEvent(goback);
  287.     rotateR();
  288.     SetEvent(q0);
  289.     }
  290.    
  291.     if(tasterR && EventIsSet(q5)){  
  292.     CancelTimer();
  293.     ClearEvent(q5);
  294.     ClearEvent(tasterR);
  295.     rotateR();
  296.     rotateR();
  297.     driving();
  298.     SetTimer(5000, goback);
  299.     StartTimer();
  300.     SetEvent(q11);
  301.     }
  302.    
  303.     if(EventIsSet(tasterL) && EventIsSet(q5)){  
  304.     CancelTimer();
  305.     ClearEvent(q5);
  306.     ClearEvent(tasterL);
  307.     driving();
  308.     SetTimer(5000, goback);
  309.     StartTimer();
  310.     SetEvent(q4);
  311.     }
  312.    
  313.     if(EventIsSet(goback) && EventIsSet(q5)){  
  314.     CancelTimer();
  315.     ClearEvent(q1);
  316.     ClearEvent(goback);
  317.     driving();
  318.     rotateR();
  319.     SetEvent(q0);
  320.     }
  321.    
  322.     if(tasterR && EventIsSet(q6)){  
  323.     CancelTimer();
  324.     ClearEvent(q6);
  325.     ClearEvent(tasterR);
  326.     rotateR();
  327.     rotateR();
  328.     driving();
  329.     SetTimer(5000, goback);
  330.     StartTimer();
  331.     SetEvent(q12);
  332.     }
  333.    
  334.     if(EventIsSet(tasterL) && EventIsSet(q6)){  
  335.     CancelTimer();
  336.     ClearEvent(q6);
  337.     ClearEvent(tasterL);
  338.     driving();
  339.     SetTimer(5000, goback);
  340.     StartTimer();
  341.     SetEvent(q5);
  342.     }
  343.    
  344.     if(EventIsSet(goback) && EventIsSet(q6)){  
  345.     CancelTimer();
  346.     ClearEvent(q1);
  347.     ClearEvent(goback);
  348.     driving();
  349.     driving();
  350.     rotateR();
  351.     SetEvent(q0);
  352.     }
  353.    
  354.     if(tasterR && EventIsSet(q7)){  
  355.     CancelTimer();
  356.     ClearEvent(q7);
  357.     ClearEvent(tasterR);
  358.     driving();
  359.     SetTimer(5000, goback);
  360.     StartTimer();
  361.     SetEvent(q8);
  362.     }
  363.    
  364.     if(EventIsSet(tasterL) && EventIsSet(q7)){  
  365.     CancelTimer();
  366.     ClearEvent(q7);
  367.     ClearEvent(tasterL);
  368.     rotateL();
  369.     rotateL();
  370.     driving();
  371.     SetTimer(5000, goback);
  372.     StartTimer();
  373.     SetEvent(q1);
  374.     }
  375.    
  376.     if(EventIsSet(goback) && EventIsSet(q7)){  
  377.     CancelTimer();
  378.     ClearEvent(q7);
  379.     ClearEvent(goback);
  380.     driving();
  381.     driving();
  382.     rotateL();
  383.     SetEvent(q0);
  384.     }
  385.    
  386.     if(tasterR && EventIsSet(q8)){  
  387.     CancelTimer();
  388.     ClearEvent(q8);
  389.     ClearEvent(tasterR);
  390.     driving();
  391.     SetTimer(5000, goback);
  392.     StartTimer();
  393.     SetEvent(q9);
  394.     }
  395.    
  396.     if(EventIsSet(tasterL) && EventIsSet(q8)){  
  397.     CancelTimer();
  398.     ClearEvent(q8);
  399.     ClearEvent(tasterL);
  400.     rotateL();
  401.     rotateL();
  402.     driving();
  403.     SetTimer(5000, goback);
  404.     StartTimer();
  405.     SetEvent(q2);
  406.     }
  407.    
  408.     if(EventIsSet(goback) && EventIsSet(q8)){  
  409.     CancelTimer();
  410.     ClearEvent(q8);
  411.     ClearEvent(goback);
  412.     driving();
  413.     rotateL();
  414.     SetEvent(q0);
  415.     }
  416.    
  417.     if(tasterR && EventIsSet(q9)){  
  418.     CancelTimer();
  419.     ClearEvent(q9);
  420.     ClearEvent(tasterR);
  421.     driving();
  422.     SetTimer(5000, goback);
  423.     StartTimer();
  424.     SetEvent(q10);
  425.     }
  426.    
  427.     if(EventIsSet(tasterL) && EventIsSet(q9)){  
  428.     CancelTimer();
  429.     ClearEvent(q9);
  430.     ClearEvent(tasterL);
  431.     rotateL();
  432.     rotateL();
  433.     driving();
  434.     SetTimer(5000, goback);
  435.     StartTimer();
  436.     SetEvent(q3);
  437.     }
  438.    
  439.     if(EventIsSet(goback) && EventIsSet(q9)){  
  440.     CancelTimer();
  441.     ClearEvent(q9);
  442.     ClearEvent(goback);
  443.     rotateL();
  444.     SetEvent(q0);
  445.     }
  446.    
  447.     if(tasterR && EventIsSet(q10)){    
  448.     CancelTimer();
  449.     ClearEvent(q10);
  450.     ClearEvent(tasterR);
  451.     driving();
  452.     SetTimer(5000, goback);
  453.     StartTimer();
  454.     SetEvent(q11);
  455.     }
  456.    
  457.     if(EventIsSet(tasterL) && EventIsSet(q10)){    
  458.     CancelTimer();
  459.     ClearEvent(q10);
  460.     ClearEvent(tasterL);
  461.     rotateL();
  462.     rotateL();
  463.     driving();
  464.     SetTimer(5000, goback);
  465.     StartTimer();
  466.     SetEvent(q4);
  467.     }
  468.    
  469.     if(EventIsSet(goback) && EventIsSet(q10)){  
  470.     CancelTimer();
  471.     ClearEvent(q7);
  472.     ClearEvent(goback);
  473.     rotateR();
  474.     rotateR();
  475.     driving();
  476.     rotateR();
  477.     SetEvent(q0);
  478.     }
  479.    
  480.     if(tasterR && EventIsSet(q11)){    
  481.     CancelTimer();
  482.     ClearEvent(q11);
  483.     ClearEvent(tasterR);
  484.     driving();
  485.     SetTimer(5000, goback);
  486.     StartTimer();
  487.     SetEvent(q12);
  488.     }
  489.    
  490.     if(EventIsSet(tasterL) && EventIsSet(q11)){    
  491.     CancelTimer();
  492.     ClearEvent(q11);
  493.     ClearEvent(tasterL);
  494.     rotateL();
  495.     rotateL();
  496.     driving();
  497.     SetTimer(5000, goback);
  498.     StartTimer();
  499.     SetEvent(q5);
  500.     }
  501.    
  502.     if(EventIsSet(goback) && EventIsSet(q11)){  
  503.     CancelTimer();
  504.     ClearEvent(q11);
  505.     ClearEvent(goback);
  506.     rotateR();
  507.     rotateR();
  508.     driving();
  509.     driving();
  510.     rotateR();
  511.     SetEvent(q0);
  512.     }
  513.    
  514.     if(tasterR && EventIsSet(q12)){    
  515.     CancelTimer();
  516.     ClearEvent(q12);
  517.     ClearEvent(tasterR);
  518.     SetTimer(5000, goback);
  519.     StartTimer();
  520.     SetEvent(q12);
  521.     }
  522.    
  523.     if(EventIsSet(tasterL) && EventIsSet(q12)){    
  524.     CancelTimer();
  525.     ClearEvent(q12);
  526.     ClearEvent(tasterL);
  527.     rotateL();
  528.     rotateL();
  529.     driving();
  530.     SetTimer(5000, goback);
  531.     StartTimer();
  532.     SetEvent(q6);
  533.     }
  534.    
  535.     if(EventIsSet(goback) && EventIsSet(q12)){  
  536.     CancelTimer();
  537.     ClearEvent(q12);
  538.     ClearEvent(goback);
  539.     rotateR();
  540.     rotateR();
  541.     driving();
  542.     driving();
  543.     driving();
  544.     rotateR();
  545.     SetEvent(q0);
  546.     }
  547.    
  548.    
  549.     }
  550.    
  551. }
  552.    
  553.  
  554.  
  555. void task2()
  556. {
  557.     uint8_t flankrecognitionR = 0;
  558.     sensor_touch_clicked_t touch1;
  559.     SensorConfig(Port_0, SensorTouch);
  560.  
  561.     while(1)
  562.     {
  563.     Touch_Clicked(Port_0, &touch1);
  564.  
  565.     if(touch1 == SensorTouch_clicked || flankrecognitionR == 1)
  566.     {
  567.         flankrecognitionR = 1;
  568.         Touch_Clicked(Port_0, &touch1);
  569.  
  570.         if(touch1 == SensorTouch_released)
  571.         {
  572.         flankrecognitionR = 0;
  573.         setEvent(tasterR);
  574.         }
  575.     }
  576.  
  577.     Delay(100);
  578.     }
  579. }
  580.  
  581. void task3()
  582. {
  583.     uint8_t flankrecognitionL = 0;
  584.     sensor_touch_clicked_t touch2;
  585.     SensorConfig(Port_1, SensorTouch);
  586.  
  587.     while(1)
  588.     {
  589.     Touch_Clicked(Port_1, &touch2);
  590.  
  591.     if(touch2 == SensorTouch_clicked || flankrecognitionL == 1)
  592.     {
  593.         flankrecognitionL = 1;
  594.         Touch_Clicked(Port_1, &touch2);
  595.  
  596.         if(touch2 == SensorTouch_released)
  597.         {
  598.         flankrecognitionL = 0;
  599.         SetEvent(tasterL);
  600.         }
  601.     }
  602.  
  603.     Delay(100);
  604.     }
  605. }
  606.  
  607. int main()
  608. {
  609.  
  610.     CreateAndStartTask(task1);
  611.     CreateAndStartTask(task2);
  612.     CreateAndStartTask(task3);
  613.     CreateAndStartTask(TimerTask);
  614.     StartScheduler();
  615. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement