Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. /*
  2. * main.cpp
  3. *
  4. * Author: Peter
  5. * Copyright (c) 2014-2015 HKUST SmartCar Team
  6. * Refer to LICENSE for details
  7. */
  8.  
  9. #include <cassert>
  10. #include <cstring>
  11. #include <libbase/k60/mcg.h>
  12. #include <libsc/system.h>
  13. #include <libsc/st7735r.h>
  14. #include <libsc/k60/ov7725.h>
  15. #include <libsc/futaba_s3010.h>
  16. #include <libsc/alternate_motor.h>
  17.  
  18.  
  19.  
  20. namespace libbase
  21. {
  22. namespace k60
  23. {
  24.  
  25. Mcg::Config Mcg::GetMcgConfig()
  26. {
  27. Mcg::Config config;
  28. config.external_oscillator_khz = 50000;
  29. config.core_clock_khz = 150000;
  30. return config;
  31. }
  32.  
  33. }
  34. }
  35.  
  36. using namespace libsc;
  37. using namespace libbase::k60;
  38. using namespace libsc::k60;
  39.  
  40. int main(void)
  41. {
  42. System::Init();
  43.  
  44. Ov7725::Config C; //camera init;
  45. C.id = 0;
  46. C.w = 80;
  47. C.h = 60;
  48. Ov7725 cam(C);
  49.  
  50. St7735r::Config s;
  51. s.is_revert = false;
  52. s.is_bgr = false; //screen init;
  53. s.fps = 100;
  54. St7735r screen(s);
  55. Timer::TimerInt t=0;
  56.  
  57. FutabaS3010::Config servo_config;
  58. servo_config.id = 0;
  59. FutabaS3010 servo(servo_config);
  60.  
  61. AlternateMotor::Config motor_config;
  62. motor_config.multiplier=100;
  63. AlternateMotor motor(motor_config);
  64.  
  65. int v[15][80], index; //v for value
  66. int areaDiff=0, orgAreaDiff;
  67. int pControl, dControl, Kp, Kd;
  68.  
  69. cam.Start();
  70. servo.SetDegree(9.0);
  71.  
  72. while (true){
  73. while(t!=System::Time()){
  74. t = System::Time();
  75.  
  76. if(t % 10 == 0){
  77. screen.SetRegion(Lcd::Rect(0,0,80,60));
  78. screen.FillBits(St7735r::kBlack,St7735r::kWhite,cam.LockBuffer(),8*cam.GetBufferSize());
  79.  
  80. orgAreaDiff = areaDiff;
  81. areaDiff = 0;
  82. for(int line=11; line<26; line++){
  83. index=0;
  84. for(int i=0; i<10 ; i++){
  85. for(int j=7; j>=0; j--){
  86. v[line][index++] = (*(cam.LockBuffer()+line+i) >> j) & 1;
  87. }
  88. }
  89.  
  90. for(int i=1;i<79;i++){
  91. if(v[line][i-1]+v[line][i]+v[line][i+1] >= 2){
  92. if(i<40) areaDiff++;
  93. else areaDiff--;
  94. }
  95. }
  96. }
  97. pControl = areaDiff * Kp; //to be determined
  98. dControl = (areaDiff-orgAreaDiff) * Kd; //to be determined
  99. servo.SetDegree(9.0 + pControl + dControl);
  100.  
  101. motor.SetPower((10-abs(servo.GetDegree()-9))*10); //motor speed depends on servo's turning deg, to be determined(maybe pid)
  102.  
  103. cam.UnlockBuffer();
  104. }
  105. }
  106. }
  107.  
  108. cam.Stop();
  109.  
  110. return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement