Projektarbeit Line Following Robot bei Prof. Chowanetz im WS22/23
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

control_module.cpp 2.5KB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include "control_module.h"
  2. ControlModule::ControlModule(): ControlModule(0.0, 1.0, 1.0)
  3. {
  4. }
  5. ControlModule::ControlModule(double maxSpeed, double rotateSpeed, double moveSideSpeed)
  6. {
  7. motors[0] = 0.0;
  8. motors[1] = 0.0;
  9. motors[2] = 0.0;
  10. motors[3] = 0.0;
  11. this->maxSpeed = maxSpeed;
  12. this->rotateSpeed = rotateSpeed;
  13. this->moveSideSpeed = moveSideSpeed;
  14. }
  15. ControlModule::~ControlModule()
  16. {
  17. }
  18. void ControlModule::adjustSpeed(){
  19. double factor = 0.0;
  20. if(maxSpeed > 1.0)
  21. {
  22. factor = 1.0;
  23. }
  24. else if(maxSpeed < -1.0)
  25. {
  26. factor = -1.0;
  27. }
  28. else
  29. {
  30. factor = maxSpeed;
  31. }
  32. for(int i = 0; i < 4; i++)
  33. {
  34. motors[i] *= maxSpeed;
  35. }
  36. };
  37. void ControlModule::moveSide(int imageColumsMiddle, int contourColumsMiddle){
  38. double speed = moveSideSpeed * static_cast<double>(contourColumsMiddle - imageColumsMiddle)/static_cast<double>(imageColumsMiddle);
  39. motors[0] += speed;
  40. motors[1] -= speed;
  41. motors[2] -= speed;
  42. motors[3] += speed;
  43. }
  44. void ControlModule::drive(double ratio)
  45. {
  46. motors[0] += ratio;
  47. motors[1] += ratio;
  48. motors[2] += ratio;
  49. motors[3] += ratio;
  50. }
  51. void ControlModule::rotate(double ratio)
  52. {
  53. motors[0] += ratio;
  54. motors[1] -= ratio;
  55. motors[2] += ratio;
  56. motors[3] -= ratio;
  57. }
  58. void ControlModule::unit(){
  59. double max = 1.0E-6;
  60. for(int i = 0; i <= sizeof(motors)/sizeof(int); i++){
  61. if(motors[i] > max)
  62. max = motors[i];
  63. }
  64. //Avoid dividing by zero
  65. if (max > 0.001)
  66. {
  67. for(int i = 0; i < 4; i++){
  68. motors[i] /= max;
  69. }
  70. }
  71. }
  72. void ControlModule::calcSpeeds(int imageColumsMiddle, int contourColumsMiddle, double angle){
  73. std::unique_lock<std::mutex> lock(mtx);
  74. double rotationRatio = ratio(angle);
  75. rotate(rotationRatio);
  76. drive(1.0-rotationRatio);
  77. unit();
  78. adjustSpeed();
  79. }
  80. std::vector<double> ControlModule::readMotors()
  81. {
  82. std::unique_lock<std::mutex> lock(mtx);
  83. return std::vector<double> {motors[0], motors[1], motors[2], motors[3]};
  84. }
  85. double ControlModule::ratio(double angle)
  86. {
  87. double minAngularSpeed = -1.0;
  88. double maxAngularSpeed = 1.0;
  89. double speedRange = maxAngularSpeed - minAngularSpeed;
  90. double minAngle = -90.0;
  91. double maxAngle = 90.0;
  92. double angularRange = maxAngle - minAngle;
  93. double progress = angle - minAngle;
  94. double progressPercent = progress/angularRange;
  95. double speed = minAngularSpeed + progressPercent*speedRange;
  96. return abs(speed);
  97. }