Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.52 KB | None | 0 0
  1. #include "mbed.h"
  2. #include "C12832.h"
  3. #include "QEI.h"
  4. DigitalOut enable(PB_2);
  5. class motormovement
  6. {
  7. private:
  8. PwmOut pwm1, pwm2;
  9. DigitalOut bipolar1, bipolar2;
  10. public:
  11. motormovement(PinName a, PinName b, PinName c, PinName d) :pwm1(a), pwm2(b), bipolar1(c), bipolar2(d)
  12. {
  13. bipolar1.write(1);
  14. bipolar2.write(1);
  15. };
  16. void moveleft(float lpwm)
  17. {
  18.  
  19.  
  20. pwm1.period(0.00005);
  21. pwm1.write(lpwm);
  22.  
  23. }
  24. void moveright(float rpwm)
  25. {
  26.  
  27. pwm2.period(0.00005);
  28. pwm2.write(rpwm);
  29. }
  30. void stop()
  31. {
  32. enable.write(0);
  33. }
  34.  
  35. };
  36. class velocity
  37. {
  38. private:
  39. Ticker ticker;//ticker for encoder;
  40. QEI*encoderl;
  41. QEI*encoderr;
  42. float T, cetl, etrl, petl, aml, awl;
  43. float cetr, ertr, petr, amr, awr;
  44. float vl, vr, rav;
  45. public:
  46. velocity(QEI*e1, QEI*e2, float t) :encoderl(e1), encoderr(e2), T(t)
  47. {
  48. petl = 0;
  49. petr = 0;
  50. //enable.write(1);
  51. ticker.attach(callback(this, &velocity::VELO), T);
  52. };
  53.  
  54. void VELO()//caculate encoer ticker rate
  55. {
  56.  
  57. cetl = encoderl->getPulses(); //current encoder ticker(left)
  58. etrl = (cetl - petl) / T;//encoder ticker rate(left)
  59. petl = cetl;//previous encoder ticker (left)
  60. aml = 2 * 3.14159*etrl / 256; //get the angular speed for motor(left)
  61. awl = aml * 10 * 16 / 50 / 60; //convert the angular speed for motor to angular speed for wheel(left)
  62. vl = awl * 0.045f; //coverted to line velocity(left)
  63. cetr = encoderr->getPulses();
  64. ertr = (cetr - petr) / T;
  65. petr = cetr;
  66. amr = 2 * 3.14159*ertr / 256;
  67. awr = amr * 10 * 16 / 50 / 60;
  68. vr = awr * 0.045f;
  69. rav = (vr - vl) / 0.2f;//calculate robert angular velocity
  70. //lcd.cls(); //clear the screen
  71.  
  72. }
  73.  
  74. float get_lpulse()
  75. {
  76. return cetl;
  77. }
  78. float get_rpulse()
  79. {
  80. return cetr;
  81. }
  82. float get_vl()
  83. {
  84. return vl; //get the line velocity for wheel
  85. }
  86. float get_vr()
  87. {
  88. return vr;
  89. }
  90. float get_rav()
  91. {
  92. return rav;
  93. }
  94. };
  95. class display
  96. {
  97. private:
  98. C12832*lcd;
  99. float m_vl, m_vr;
  100. public:
  101. display(C12832*c1, float vl, float vr) :lcd(c1), m_vl(vl), m_vr(vr) {}
  102. void disvel()
  103. {
  104. lcd->locate(10, 1); //locate at (0,3)
  105. lcd->printf("left velocity is:%.3f ", m_vl);
  106. lcd->locate(10, 11); //locate at (12,3)
  107. lcd->printf("right velocity is:%.3f ", m_vr);
  108. }
  109. };
  110. class Pspeed
  111. {
  112. private:
  113. float readvalue;
  114. float Kp;
  115. float setpoint, tp;
  116. float error;
  117. float perror;
  118. float kd;
  119. float Turn, wishV;
  120. float pwmr, pwml, difl, difr, vellp, velrp, vell, velr, maxpwm, minpwm;
  121. public:
  122. Pspeed()
  123. {
  124. error = 0;
  125. perror=0;
  126. Kp = 2.5;
  127. kd= 10;
  128. setpoint = 0.035;//the normal velocity
  129. tp = 0.01;//the pwm for the normal velocity
  130. difl=0;
  131. difr=0;
  132. pwml= 0.65;
  133. pwmr= 0.65;
  134. velrp=0;
  135. vellp=0;
  136. wishV= 0.7;
  137. }
  138. /* void calpid(float readvalue)
  139. {
  140.  
  141. error = setpoint - readvalue;
  142. Turn = Kp * error+ (error-perror)*kd;
  143. perror= error;
  144. pwm = Tp + Turn;
  145.  
  146. }
  147. */
  148. void drive(float vell, float velr)
  149. {
  150.  
  151. difl= vell - vellp;
  152. difr= velr - velrp;
  153. vellp= vell;
  154. velrp=velr;
  155.  
  156. if(( (vell||velr)< wishV)) {
  157.  
  158. pwml += tp;
  159. pwmr += tp;
  160.  
  161. }
  162.  
  163. else if (( (vell||velr)> wishV)) {
  164. pwml -= tp;
  165. pwmr -= tp;
  166. } else {
  167.  
  168. if (difl> difr) {
  169. pwml -= tp;
  170. pwmr += tp;
  171. } else if (difl< difr) {
  172. pwml += tp;
  173. pwmr -= tp;
  174. }
  175. }
  176.  
  177. }
  178. float getpwml()
  179. {
  180.  
  181. return pwml;
  182. }
  183.  
  184. float getpwmr()
  185. {
  186.  
  187. return pwmr;
  188. }
  189.  
  190.  
  191. };
  192. int main()
  193. {
  194. float vl, vr, rav, pulse,rpwm,lpwm;
  195. //vl:left velocity, vr:right velocity ,st:straghtline time ,tt:time needed to turn pi/2 ,tat:time needed to turn pi
  196. motormovement*wheel = new motormovement(PB_14, PB_15, PC_14, PC_15);//connect pwm1 to PB_14,pwm2 to PB_15 ,bipolar1 to PC_14,bipolar2 to PC_15 )
  197. QEI*e1 = new QEI(PC_10, PC_12, NC, 256);//left encoder
  198. QEI*e2 = new QEI(PC_11, PD_2, NC, 256);//right encoder
  199. C12832* c1 = new C12832(D11, D13, D12, D7, D10);
  200.  
  201. velocity encoder(e1, e2, 0.01);
  202. Pspeed esl, esr;
  203.  
  204. enable.write(1);
  205. while (1) {
  206. vl = encoder.get_vl();
  207. vr = encoder.get_vr();
  208. display mylcd(c1,vl,vr);
  209. mylcd.disvel();
  210.  
  211. esl.drive(vl,vr);
  212.  
  213. lpwm=esl.getpwml();
  214. rpwm = esr.getpwmr();
  215. wheel->moveleft(lpwm);
  216. wheel->moveright(rpwm);
  217.  
  218.  
  219. /* esr.calpid(vr);
  220.  
  221.  
  222. if (((vl&&vr)<0.0035)) {
  223. lpwm=esl.getpwm()+0.01;
  224. rpwm = esr.getpwm()+0.01;
  225. wheel->moveleft(lpwm);
  226. wheel->moveright(rpwm);
  227. } else if (((vl&&vr)>0.0035)) {
  228. lpwm=esl.getpwm()-0.01;
  229. rpwm = esr.getpwm()-0.01;
  230. wheel->moveleft(lpwm);
  231. wheel->moveright(rpwm);
  232. } else if(((vl&&vr)==0.0035)){
  233. lpwm=esl.getpwm();
  234. rpwm = esr.getpwm();
  235. wheel->moveleft(lpwm);
  236. wheel->moveright(rpwm);
  237. }*/
  238. }
  239.  
  240.  
  241.  
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement