-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_motors.js
166 lines (152 loc) · 2.81 KB
/
test_motors.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
var SerialPort = require("serialport");
// this is the openImmediately flag [default is true]
var my_uart = new SerialPort
(
"/dev/ttyS0",
{
baudRate: 256000
},
false
);
//Test cycle settings
var num_motors = 4;
var max_speed = 55;
var speed_increment = 20;
//Cycle counters
var ramp = 0;
var dir = 0;
var motor = 0;
var speed = 0;
my_uart.on
(
"open",
function()
{
console.log("Port is open!");
//Periodically send the current server time to the client in string form
setInterval
(
function()
{
//----------------------------------------------
// Motor Ramp Generator
//----------------------------------------------
//if: accelerate
if (ramp == 0)
{
//increase speed
speed += speed_increment;
//if maximum speed
if (speed >= max_speed)
{
//clip speed
speed = max_speed;
//decelerate
ramp = 1;
}
}
//if: decelerate
else
{
//decrease speed
speed -= speed_increment;
//if minimum
if (speed <= 0)
{
//clip speed
speed = 0;
//accelerate
ramp = 0;
//change direction
dir = 1 -dir;
//if i did a full cycle
if (dir == 0)
{
//Scan motors
motor++;
//clip to maximum number of motors
if (motor >= num_motors)
{
motor = 0;
}
}
}
}
//----------------------------------------------
// Command sender
//----------------------------------------------
//temp counter
var t;
//Scan all motors
for (t=0;t<num_motors;t++)
{
//If i'm controlling the indexed motor
if (motor == t)
{
if (dir == 0)
{
//operate right motor
set_dc_motor_pwm( t, speed );
}
else
{
//operate right motor
set_dc_motor_pwm( t, -speed );
}
}
//if i'm adressing an idle motor
else
{
set_dc_motor_pwm( t, 0 );
}
} //End For: scan motors
},
//Send every * [ms]
300
);
}
);
my_uart.on
(
'data',
function(data)
{
console.log('data received: ' + data);
}
);
//Send ping message to keep the connection alive
function send_ping( )
{
my_uart.write
(
"P\0",
function(err, res)
{
if (err)
{
console.log("err ", err);
}
}
);
}
//Compute the speed message to send to maze runner
function set_dc_motor_pwm( motor_index, vel )
{
var msg;
msg = "M" + motor_index + "PWM" + vel + "\0";
my_uart.write
(
msg,
function(err, res)
{
if (err)
{
console.log("err ", err);
}
else
{
console.log("Sent: ", msg);
}
}
);
}