-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmotors.cpp
66 lines (58 loc) · 1.69 KB
/
motors.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* lidar project
*
* Copyright (C) 2017-2020 Gabor Szita
*
* This file is part of lidar project.
*
* Lidar project is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as published by
* the Free Software Foundation.
*
* Lidar project is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with lidar project. If not, see https://www.gnu.org/licenses/
*/
#include <stdio.h>
//#include "AdafruitStepperMotorHAT_CPP/Adafruit_MotorHAT.h"
#include "motors.h"
AdafruitMotorsDriver::AdafruitMotorsDriver()
{
M1= &hat.getDC(1);
M2= &hat.getDC(2);
M3= &hat.getDC(3);
M4= &hat.getDC(4);
}
void AdafruitMotorsDriver::setMotorDir(int *speed, Adafruit_DCMotor& Motor){
if (*speed<0) {
Motor.run(BACKWARD);
*speed = -*speed;
}
else{
Motor.run(FORWARD);
}
}
void AdafruitMotorsDriver::motorControl(int speeds[4]){
//printf("%d %d %d %d\n", speeds[0], speeds[1], speeds[2], speeds[3]);
setMotorDir(&speeds[0], *M1);
M1->setSpeed(speeds[0]);
setMotorDir(&speeds[1], *M2);
M2->setSpeed(speeds[1]);
setMotorDir(&speeds[2], *M3);
M3->setSpeed(speeds[2]);
setMotorDir(&speeds[3], *M4);
M4->setSpeed(speeds[3]);
if (speeds[0]!=0 || speeds[1]!=0 || speeds[2]!=0 || speeds[3]!=0) {
robotMoving=true;
}
else{
robotMoving=false;
}
}
void AdafruitMotorsDriver::Mhatreset(){
hat.resetAll();
}