actuator.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #ifndef ACTUATOR_MODULE
  2. #define ACTUATOR_MODULE
  3. #include "Arduino.h"
  4. #include "definitions.h"
  5. struct Actuator
  6. {
  7. public:
  8. enum State {
  9. STILL,
  10. PUSH,
  11. PULL
  12. };
  13. private:
  14. PIN_TYPE motor_push_pin;
  15. PIN_TYPE motor_pull_pin;
  16. int max_distance_pwm;
  17. State current_state;
  18. bool change_required;
  19. GlobalState* global_state;
  20. public:
  21. CONST_PIN_TYPE ANALOUGE_PIN = 1;
  22. Actuator (PIN_TYPE motor_push_pin, PIN_TYPE motor_pull_pin,
  23. int max_distance_pwm, GlobalState* global_state)
  24. {
  25. this->current_state = STILL;
  26. this->motor_push_pin = motor_push_pin;
  27. this->motor_pull_pin = motor_pull_pin;
  28. this->max_distance_pwm = max_distance_pwm;
  29. this->global_state = global_state;
  30. }
  31. void setup()
  32. {
  33. this->global_state->actuator_at_max_push = false;
  34. // Serial.println("Setting up actiator");
  35. this->setState(PULL);
  36. // printState();
  37. while (this->current_state != STILL)
  38. {
  39. this->motorLoop();
  40. //Serial.print("Setup loop - current state: ");
  41. //Serial.println(this->current_state);
  42. }
  43. //Serial.println("Setup loop done");
  44. }
  45. void setMaxDistance(int max_distance_pwm)
  46. {
  47. this->max_distance_pwm = max_distance_pwm;
  48. }
  49. void printState()
  50. {
  51. Serial.print("Actuator current state: ");
  52. Serial.print(this->current_state);
  53. Serial.print(" - and PWM value: ");
  54. Serial.println(analogRead(ANALOUGE_PIN));
  55. }
  56. void setState(State state)
  57. {
  58. // Serial.print("Current state: ");
  59. // Serial.println(this->current_state);
  60. if (state != this->current_state)
  61. {
  62. this->current_state = state;
  63. this->change_required = true;
  64. }
  65. }
  66. int getMotorDist()
  67. {
  68. return analogRead(ANALOUGE_PIN);
  69. }
  70. void motorLoop()
  71. {
  72. // Serial.print("Motor loop - ");
  73. //printState();
  74. if (this->current_state == STILL)
  75. {
  76. return;
  77. }
  78. #define MIN_DISTANCE 20
  79. int sensor_value = this->getMotorDist();
  80. // if (sensor_value >= this->max_distance_pwm - 30) // Leave a bit of buffer
  81. if (sensor_value >= this->max_distance_pwm)
  82. {
  83. this->global_state->actuator_at_max_push = true;
  84. }
  85. else
  86. {
  87. this->global_state->actuator_at_max_push = false;
  88. if (sensor_value <= MIN_DISTANCE)
  89. {
  90. this->global_state->actuator_at_min_pull = true;
  91. }
  92. else
  93. {
  94. this->global_state->actuator_at_min_pull = false;
  95. }
  96. }
  97. if (this->current_state == PUSH)
  98. {
  99. if (sensor_value >= this->max_distance_pwm)
  100. {
  101. this->enableStill();
  102. this->current_state = STILL;
  103. this->change_required = false;
  104. }
  105. if (this->change_required)
  106. {
  107. this->enablePush();
  108. this->change_required = false;
  109. }
  110. }
  111. else if (this->current_state == PULL)
  112. {
  113. if (sensor_value <= MIN_DISTANCE) // Doesn't have to be completely retracted
  114. {
  115. this->enableStill();
  116. this->current_state = STILL;
  117. this->change_required = false;
  118. }
  119. if (this->change_required)
  120. {
  121. this->enablePull();
  122. this->change_required = false;
  123. }
  124. }
  125. }
  126. void enablePush()
  127. {
  128. digitalWrite(this->motor_push_pin, HIGH);
  129. digitalWrite(this->motor_pull_pin, LOW);
  130. }
  131. void enablePull()
  132. {
  133. digitalWrite(this->motor_push_pin, LOW);
  134. digitalWrite(this->motor_pull_pin, HIGH);
  135. }
  136. void enableStill()
  137. {
  138. digitalWrite(this->motor_push_pin, LOW);
  139. digitalWrite(this->motor_pull_pin, LOW);
  140. }
  141. };
  142. #endif