-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_motors_platform.js
157 lines (144 loc) · 2.41 KB
/
test_motors_platform.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
var SerialPort = require("serialport");
// this is the openImmediately flag [default is true]
var my_uart = new SerialPort
(
"/dev/ttyS0",
{
baudRate: 256000
},
false
);
//maximum speed is 13 in this demo version
var ramp = 0;
var dir = 0;
var motor = 0;
var max_speed = 55;
var speed_increment = 10;
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()
{
//Speed Processor
//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)
{
//Use the other motor
motor = 1 -motor;
}
}
}
//PWM Assign
//if: right motor
if (motor == 0)
{
if (dir == 0)
{
//operate right motor
set_dc_motor_pwm( speed, 0 );
}
else
{
//operate right motor
set_dc_motor_pwm( -speed, 0 );
}
}
//if: left motor
else
{
if (dir == 0)
{
//operate right motor
set_dc_motor_pwm( 0, speed );
}
else
{
//operate right motor
set_dc_motor_pwm( 0, -speed );
}
}
},
//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( vel_r, vel_l )
{
var msg;
msg = "PWMR" + vel_r + "L" + vel_l + "\0";
my_uart.write
(
msg,
function(err, res)
{
if (err)
{
console.log("err ", err);
}
else
{
console.log("Sent: ", msg);
}
}
);
}