Advertisement
Guest User

excercise

a guest
Jan 19th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.56 KB | None | 0 0
  1.  
  2. // -----------------------------------------------------------------
  3. // Exercise 4
  4. // -----------------------------------------------------------------
  5.  
  6. #include <FrameStream.h>
  7. #include <Frameiterator.h>
  8. #include <avr/io.h>
  9. #include <avr/interrupt.h>
  10. #include "Odometry.h"
  11. #include "MotorControl.h"
  12. #define OUTPUT__BAUD_RATE 57600
  13. #include "everytime.h"
  14. #include "Distance.h"
  15. FrameStream frm(Serial1);
  16.  
  17. // Forward declarations
  18. void InitGUI();
  19.  
  20. OdomData d;
  21.  
  22.  
  23. // hierarchical runnerlist, that connects the gui elements with
  24. // callback methods
  25. declarerunnerlist(GUI);
  26.  
  27. // First level will be called by frm.run (if a frame is recived)
  28. beginrunnerlist();
  29. fwdrunner(!g, GUIrunnerlist); //forward !g to the second level (GUI)
  30. callrunner(!!, InitGUI);
  31. fwdrunner(ST, stickdata);
  32. endrunnerlist();
  33.  
  34. // GUI level
  35. beginrunnerlist(GUI);
  36. callrunner(es, CallbackSTOP);
  37. callrunner(ms, CallbackSTART);
  38. endrunnerlist();
  39.  
  40.  
  41. void stickdata(char* str, size_t length)
  42. {
  43. // str contains a string of two numbers ranging from -127 to +127
  44. // indicating left and right motor values
  45. //TODO: interprete str, set motors
  46. char* left = strtok(str, ",");
  47. char* right = strtok(NULL, ",");
  48. setMotors(atoi(left),atoi(right));
  49. }
  50.  
  51. void CallbackSTOP()
  52. {
  53. deactivateMotors();
  54. // call stopMotors();
  55. }
  56.  
  57.  
  58. void CallbackSTART()
  59. {
  60. activateMotors();
  61. // call startMotors()
  62. }
  63.  
  64.  
  65.  
  66.  
  67. /*
  68. * @brief initialises the GUI of ArduinoView
  69. *
  70. * In this function, the GUI, is configured. For this, Arduinoview shorthand,
  71. * HTML as well as embedded JavaScript can be used.
  72. */
  73. void InitGUI()
  74. {
  75. delay(500);
  76.  
  77. frm.print(F("!SbesvSTOP"));
  78. frm.end();
  79.  
  80. frm.print(F("!SbmsvSTART"));
  81. frm.end();
  82.  
  83.  
  84. //this implements a joystick field using HTML SVG and JS
  85. //and some info divs next to it
  86. frm.print(F("!H"
  87. "<div><style>svg * { pointer-events: none; }</style>\n"
  88. "<div style='display:inline-block'> <div id='state'> </div>\n"
  89. "<svg id='stick' width='300' height='300' viewBox='-150 -150 300 300' style='background:rgb(200,200,255)' >\n"
  90. "<line id='pxy' x1='0' y1='0' x2='100' y2='100' style='stroke:rgb(255,0,0);stroke-width:3' />\n"
  91. "<circle id='cc' cx='0' cy='0' r='3' style='stroke:rgb(0,0,0);stroke-width:3' />\n"
  92. "</svg></div>"
  93. "<div style='display:inline-block'>"
  94. "<div id='info0'></div>"
  95. "<div id='info1'></div>"
  96. "<div id='info2'></div>"
  97. "</div></div>\n"
  98. "<script>\n"
  99. "var getEl=function(x){return document.getElementById(x)};\n"
  100. "var setEl=function(el,attr,val){(el).setAttributeNS(null,attr,val)};\n"
  101. "var stick=getEl('stick');\n"
  102. "function sticktransform(x,y){\n"
  103. "x = x-150;\n"
  104. "y = -(y-150);\n"
  105. "setstick(x,y);\n"
  106. "}\n"
  107. "function setstick(x,y){\n"
  108. "setpointer(x,y);\n"
  109. "l = Math.floor(Math.min(127,Math.max(-127,y + x/2)));\n"
  110. "r = Math.floor(Math.min(127,Math.max(-127,y - x/2)));\n"
  111. "setStateDisplay(x,y,l,r);\n"
  112. "sendframe(\"ST\"+l +\",\"+r);\n"
  113. "}\n"
  114. "function setStateDisplay(x,y,l,r){\n"
  115. "msg=getEl('state');\n"
  116. "msg.innerHTML= 'x= '+ x +' y= '+ y +' l= '+ l +' r= '+ r ;\n"
  117. "}\n"
  118. "function setpointer(x,y){\n"
  119. "pxy=getEl('pxy');\n"
  120. "setEl(pxy,'x2',x);\n"
  121. "setEl(pxy,'y2',-y);\n"
  122. "}\n"
  123. "stick.onmousemove=function(e){\n"
  124. "if( e.buttons == 1 ){\n"
  125. "sticktransform(e.offsetX,e.offsetY);\n"
  126. "}};\n"
  127. "stick.onmousedown=function(e){\n"
  128. "if( e.buttons == 1 ){\n"
  129. "sticktransform(e.offsetX,e.offsetY);\n"
  130. "}};\n"
  131. "stick.onmouseup=function(e){\n"
  132. "setstick(0,0);\n"
  133. "};\n"
  134. "stick.onmouseleave=function(e){\n"
  135. "setstick(0,0);\n"
  136. "};\n"
  137. "function sendframe(msg){\n"
  138. "ArduinoView.sendMessage(ArduinoView._createSerialFrame(msg))\n"
  139. "}\n"
  140. "setstick(0,0);\n"
  141. "</script>\n"));
  142. frm.end();
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149. }
  150.  
  151. /*
  152. * @brief Initialisation
  153. *
  154. * Implement basic initialisation of the program.
  155. */
  156.  
  157.  
  158. void setup()
  159. {
  160. //prepare Serial interfaces
  161.  
  162. Serial.begin(OUTPUT__BAUD_RATE);
  163. Serial1.begin(OUTPUT__BAUD_RATE);
  164.  
  165. Serial.println(F("Willkommen zur PKeS Übung"));
  166.  
  167. Serial.println(F("PKes4"));
  168.  
  169. //request reset of GUI
  170. frm.print("!!");
  171. frm.end();
  172.  
  173. initMotors();
  174. initOdom();
  175. sei();
  176. delay(500);
  177.  
  178. // TODO initialize Odometry, Sensors, Motors, etc.
  179.  
  180. }
  181.  
  182. /*
  183. * @brief Main loop
  184. *
  185. * This function will be called repeatedly and shall therefore implement the
  186. * main behavior of the program.
  187. */
  188.  
  189.  
  190.  
  191.  
  192. void loop()
  193. {
  194. // read & run ArduinoView Frames
  195. while(frm.run());
  196.  
  197. odomTicks(d);
  198. every(500){
  199. /*Serial.print("right: ");
  200. Serial.println(odomDistance(d.right),500);
  201. Serial.print("left: ");
  202. Serial.println(odomDistance(d.left),500);*/
  203.  
  204. Serial.print("right: ");
  205. Serial.println(d.right);
  206. Serial.print("left: ");
  207. Serial.println(d.left);
  208. }
  209.  
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement