-
Notifications
You must be signed in to change notification settings - Fork 1
/
Motor.cpp
163 lines (121 loc) · 4.2 KB
/
Motor.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*This file is part of the Maslow Control Software.
The Maslow Control Software is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Maslow Control Software 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 the Maslow Control Software. If not, see <http://www.gnu.org/licenses/>.
Copyright 2014-2017 Bar Smith*/
/*
The Motor module imitates the behavior of the Arduino servo module. It allows a gear motor (or any electric motor)
to be a drop in replacement for a continuous rotation servo.
*/
#include "Arduino.h"
#include "Motor.h"
Motor::Motor(){
_attachedState = 0;
}
int Motor::setupMotor(int pwmPin, int pin1, int pin2){
//store pin numbers as private variables
_pwmPin = pwmPin;
_pin1 = pin1;
_pin2 = pin2;
_attachedState = 0;
//set pinmodes
pinMode(_pwmPin, OUTPUT);
pinMode(_pin1, OUTPUT);
pinMode(_pin2, OUTPUT);
//stop the motor
digitalWrite(_pin1, HIGH);
digitalWrite(_pin2, LOW) ;
digitalWrite(_pwmPin, LOW);
return 1;
}
void Motor::attach(){
_attachedState = 1;
}
void Motor::detach(){
_attachedState = 0;
//stop the motor
digitalWrite(_pin1, HIGH);
digitalWrite(_pin2, LOW) ;
digitalWrite(_pwmPin, LOW);
}
void Motor::write(int speed){
/*
Sets motor speed from input. Speed = 0 is stopped, -255 is full reverse, 255 is full ahead.
*/
if (_attachedState == 1){
//linearize the motor
//speed = _convolve(speed);
//set direction range is 0-180
if (speed > 0){
digitalWrite(_pin1 , HIGH);
digitalWrite(_pin2 , LOW );
speed = speed;
}
else if (speed == 0){
speed = speed;
}
else{
digitalWrite(_pin1 , LOW);
digitalWrite(_pin2 , HIGH );
speed = speed;
}
//enforce range
if (speed > 255){speed = 255;}
if (speed < -255) {speed = -255; }
speed = abs(speed); //remove sign from input because direction is set by control pins on H-bridge
int pwmFrequency = round(speed);
analogWrite(_pwmPin, pwmFrequency);
}
}
void Motor::directWrite(int voltage){
/*
Write directly to the motor, ignoring if the axis is attached or any applied calibration.
*/
if (voltage > 0){
digitalWrite(_pin1 , HIGH);
digitalWrite(_pin2 , LOW );
}
else if (voltage == 0){
voltage = voltage;
}
else{
digitalWrite(_pin1 , LOW);
digitalWrite(_pin2 , HIGH );
}
analogWrite(_pwmPin, abs(voltage));
}
int Motor::attached(){
return _attachedState;
}
int Motor::_convolve(int input){
/*
This function distorts the input signal in a manner which is the inverse of the way
the mechanics of the motor distort it to give a linear response.
*/
int output = input;
int arrayLen = sizeof(_linSegments)/sizeof(_linSegments[1]);
for (int i = 0; i <= arrayLen - 1; i++){
if (input > _linSegments[i].negativeBound and input < _linSegments[i].positiveBound){
output = (input + _linSegments[i].intercept)/_linSegments[i].slope;
break;
}
}
return output;
}
void Motor::setSegment(int index, float slope, float intercept, int negativeBound, int positiveBound){
//Adds a linearizion segment to the linSegments object in location index
_linSegments[index].slope = slope;
_linSegments[index].intercept = intercept;
_linSegments[index].positiveBound = positiveBound;
_linSegments[index].negativeBound = negativeBound;
}
LinSegment Motor::getSegment(int index){
return _linSegments[index];
}