-
Notifications
You must be signed in to change notification settings - Fork 0
/
Actuator.cpp
105 lines (97 loc) · 3.34 KB
/
Actuator.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "Actuator.h"
#include <Arduino.h>
/***************************************************************************************
** Function name: Actuator
** Description: Constructor , we must stop actuator in begning
***************************************************************************************/
Actuator::Actuator(int A_pin, int B_pin, int pwm_pin, bool fb, int crnt)
{
m_Apin = A_pin;
m_Bpin = B_pin;
m_pwmPin = pwm_pin;
m_feedback = fb;
m_current = crnt;
}
int Actuator::getCurrent()
{
return m_current;
}
void Actuator::setCurrent(int crnt)
{
m_current = crnt;
}
void Actuator::actuatorInit(void)
{
pinMode(m_Apin, OUTPUT);
pinMode(m_Bpin, OUTPUT);
pinMode(m_pwmPin, OUTPUT);
ledcSetup(m_pwmChannel, m_freq, m_resolution);
ledcAttachPin(m_pwmPin, m_pwmChannel);
}
/***************************************************************************************
** Function name: setDirection
** Description: set direction of actuator
***************************************************************************************/
void Actuator::setDirection(direction_t dir)
{
if(dir > 0){
digitalWrite(m_Apin, HIGH);
digitalWrite(m_Bpin, LOW);
} else if(dir < 0){
digitalWrite(m_Apin, LOW);
digitalWrite(m_Bpin, HIGH);
} else {
digitalWrite(m_Apin, LOW);
digitalWrite(m_Bpin, LOW);
}
m_direction = dir;
}
/***************************************************************************************
** Function name: setSpeed
** Description: set speed of actuator, convert 100% to 255
***************************************************************************************/
void Actuator::actuatorSetSpeed(unsigned int actuatorSpeed)
{
m_speed = map(actuatorSpeed,0,100,0,255);
ledcWrite(m_pwmChannel, m_speed);
}
/***************************************************************************************
** Function name: getDirection
** Description: get direction of actuator
***************************************************************************************/
direction_t Actuator::getDirection()
{
return m_direction;
}
/***************************************************************************************
** Function name: setFbType
** Description: with feedback or without feedback
***************************************************************************************/
void Actuator::setFbType(bool type)
{
m_feedback = type;
}
/***************************************************************************************
** Function name: getFbData
** Description: adc data of actuator feedback
***************************************************************************************/
int Actuator::getFbData()
{
return m_feedbackData;
}
/***************************************************************************************
** Function name: move
** Description: move actuator
***************************************************************************************/
void Actuator::actuatorMove(int correction)
{
actuatorSetSpeed((correction < 0)?(correction * (-1)):correction);
if(correction > 0){
setDirection(FORWARD);
} else if(correction < 0){
setDirection(BACKWARD);
} else {
actuatorSetSpeed(0);
setDirection(STOP);
}
}