Advertisement
hoahoctro

DRV8833.cpp

Mar 24th, 2020
1,102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.49 KB | None | 0 0
  1. #include "Arduino.h"
  2. #include "DRV8833.h"
  3. //Motors Group A
  4.     //const int m1_a=33;
  5.     //const int m1_b=32;
  6.     //const int m1_pwm=25;
  7. //Motors Group B
  8.     //const int m2_a=27;
  9.     //const int m2_b=26;
  10.     //const int m2_pwm=14;
  11. // setting PWM properties
  12. const int freq = 5000;
  13. const int resolution = 8;
  14. const int channel_1 = 0;
  15. const int channel_2 = 1;
  16. const int channel_3 = 3;
  17. const int channel_4 = 4;
  18. DRV8833::DRV8833(int _m1_a, int _m1_b, int _m2_a, int _m2_b) {
  19.     m1_a=_m1_a;
  20.     m1_b=_m1_b;
  21.     m2_a=_m2_a;
  22.     m2_b=_m2_b;
  23.  
  24.     pinMode(m1_a,OUTPUT);
  25.     pinMode(m1_b,OUTPUT);
  26.     pinMode(m2_a,OUTPUT);
  27.     pinMode(m2_b,OUTPUT);
  28.      // configure LED PWM functionalitites
  29.     ledcSetup(channel_1, freq, resolution);
  30.     ledcSetup(channel_2, freq, resolution);
  31.     ledcSetup(channel_3, freq, resolution);
  32.     ledcSetup(channel_4, freq, resolution);
  33.     // attach the channel to the GPIO to be controlled
  34.     ledcAttachPin(m1_a, channel_1);
  35.     ledcAttachPin(m1_b, channel_2);
  36.     ledcAttachPin(m2_a, channel_3);
  37.     ledcAttachPin(m2_b, channel_4);
  38. }  
  39. void DRV8833::turn_left(int spd){
  40.     move(0,spd,1);
  41.     move(1,spd,0);
  42.     delay(20);
  43.     stop();
  44. }
  45. void DRV8833::turn_right(int spd){
  46.     //move(0,spd,0);
  47.     move(1,spd,1);
  48.     delay(20);
  49.     stop();
  50. }
  51. void DRV8833::left_1wd(int spd){
  52.     move(0,spd,0);
  53.     //move(1,spd,0);
  54.     delay(20);
  55.     stop();
  56. }
  57. void DRV8833::right_1wd(int spd){
  58.     //move(0,spd,1);
  59.     move(1,spd,0);
  60.     delay(20);
  61.     stop();
  62. }
  63. void DRV8833::forward(int spd){
  64.     move(0,spd,0);
  65.     move(1,spd,0);
  66.     delay(20);
  67.     stop();
  68. }
  69. void DRV8833::backward(int spd){
  70.     move(0,spd,1);
  71.     move(1,spd,1);
  72.     delay(20);
  73.     stop();
  74. }
  75. void DRV8833::move(int motor, int speed, int direction){
  76. //Move specific motor at speed and direction
  77. //motor: 0 for B 1 for A
  78. //speed: 0 is off, and 255 is full speed
  79. //direction: 0 clockwise, 1 counter-clockwise
  80.     if (motor == 1){
  81.         if (direction == 1) {
  82.             digitalWrite(m1_a, LOW);
  83.             //digitalWrite(m1_b, HIGH);
  84.             ledcWrite(channel_2, speed);
  85.             //ledcDetachPin(pin);
  86.         } else {
  87.             //digitalWrite(m1_a, HIGH);
  88.             digitalWrite(m1_b, LOW);
  89.             ledcWrite(channel_1, speed);
  90.             //ledcDetachPin(pin);
  91.         }
  92.     } else {
  93.         if (direction == 1) {
  94.             //digitalWrite(m2_a, HIGH);
  95.             digitalWrite(m2_b, LOW);
  96.             ledcWrite(channel_3, speed);
  97.             //ledcDetachPin(pin);
  98.         } else {
  99.             digitalWrite(m2_a, LOW);
  100.             //digitalWrite(m2_b, HIGH);
  101.             ledcWrite(channel_4, speed);
  102.             //ledcDetachPin(pin);
  103.         }
  104.     }
  105. }
  106. void DRV8833::stop(){
  107.     digitalWrite(m1_a,LOW);
  108.     digitalWrite(m1_b,LOW);
  109.     digitalWrite(m2_a,LOW);
  110.     digitalWrite(m2_b,LOW);
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement