-
Notifications
You must be signed in to change notification settings - Fork 3
/
servo-object.js
146 lines (135 loc) · 3.56 KB
/
servo-object.js
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
/**
* Espruino by Examples < http://git.io/ebe >
*
* Copyright (c) 2014 Anar Software LLC. < http://anars.com >
*
* This program 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.
*
* This program 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
* this program. If not, see < http://www.gnu.org/licenses/ >
*/
/*
Servo Object
by Kay Anar in July 13, 2014
*/
var Servo = {
"_minPulse" : 0.1,
"_maxPulse" : 2.3,
"connect" : function(servoPin, feedbackPin)
{
if(typeof servoPin == "number")
this.setServoPin(servoPin);
if(typeof feedbackPin == "number")
this.setFeedbackPin(feedbackPin);
return(this);
},
"onMoveStart" : function()
{
print("onMoveStart");
},
"setMoveStart" : function(moveStart)
{
if(typeof moveStart == "function")
this.onMoveStart = moveStart;
return(this);
},
"onMoveEnd" : function()
{
print("onMoveEnd");
},
"setMoveEnd" : function(moveEnd)
{
if(typeof moveEnd == "function")
this.onMoveEnd = moveEnd;
return(this);
},
"setPosition" : function(position, time)
{
if (typeof time != "number" || time < 500)
time = 500;
if (typeof this._position != "number")
this._position = this._minPulse;
this._requestedPostion = position;
this._stepAmount = (this._requestedPostion - this._position) / (time / 25);
this.onMoveStart();
var servoObj = this;
servoObj._interval = setInterval(function()
{
servoObj._position += servoObj._stepAmount;
if ((servoObj._stepAmount > 0 && (servoObj._position >= servoObj._requestedPostion || servoObj._position >= servoObj._maxPulse)) || (servoObj._stepAmount < 0 && (servoObj._position <= servoObj._requestedPostion || servoObj._position <= servoObj._minPulse)))
{
clearInterval(servoObj._interval);
delete servoObj._interval;
delete servoObj._requestedPostion;
delete this._stepAmount;
servoObj.onMoveEnd();
servoObj = null;
}
else
{
digitalPulse(servoObj._servoPin, 1, E.clip(servoObj._position, servoObj._minPulse, servoObj._maxPulse));
}
}, 25);
return(this);
},
"getPosition" : function()
{
return(_position);
},
"setServoPin" : function(servoPin)
{
this._servoPin = servoPin;
return(this);
},
"getServoPin" : function()
{
return(this._servoPin);
},
"setFeedbackPin" : function(feedbackPin)
{
this._feedbackPin = feedbackPin;
return(this);
},
"getFeedbackPin" : function()
{
return(this._feedbackPin);
},
"getFeedbackValue" : function()
{
if (this._feedbackPin !== undefined)
return(analogRead(this._feedbackPin));
},
"setMinPulse" : function(minPulse)
{
if(typeof minPulse == "number")
this._minPulse = minPulse;
return(this);
},
"getMinPulse" : function()
{
return(this._minPulse);
},
"setMaxPulse" : function(maxPulse)
{
if(typeof maxPulse == "number")
this._maxPulse = maxPulse;
return(this);
},
"getMaxPulse" : function()
{
return(this._maxPulse);
}
};
Servo.connect(C5, C4);
//print(Servo.getServoPin());
Servo.setPosition(0);
Servo.setMoveEnd(function() { Servo.setPosition(Servo.setMinPulse(), 5000); });
//Servo.setPosition(Servo.setMaxPulse(), 5000);
//Servo.setMoveEnd(function() { Servo.setPosition(Servo.setMinPulse(), 5000); });