-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
140 lines (128 loc) · 3.54 KB
/
index.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
var APP_ID = undefined;
var ROSLIB = require('roslib');
var Alexa = require('alexa-sdk');
var EMMITER = require('eventemitter2');
var HELP_REPROMPT = "What can I help you with?";
var HELP_MESSAGE = "You can say forward, backward turn left or turn right... What can I help you with?";
var FORWARD = "forward";
var BACKWARD = "backward";
var TURN_LEFT = "turn left";
var TURN_RIGHT = "turn right";
var STOP_MESSAGE = "Goodbye!";
var RECIPE_NOT_FOUND_MESSAGE = "I\'m sorry, I currently do not know that command. Please try again.";
var RECIPE_NOT_FOUND_REPROMPT = "What else can I help with?";
//ROS functionallity
var ros = new ROSLIB.Ros({
url: 'ws://localhost:9090'
});
ros.on('connection', function () {
console.log('Connected to websocket server.');
});
ros.on('error', function (error) {
console.log('Error connecting to websocket server: ', error);
});
ros.on('close', function () {
console.log('Connection to websocket server closed.');
});
var cmdVel = new ROSLIB.Topic({
ros: ros,
name: '/turtle1/cmd_vel',
messageType: 'geometry_msgs/Twist'
});
var twistf = new ROSLIB.Message({
linear: {
x: 2.0,
y: 0.0,
z: 0.0
},
angular: {
x: 0.0,
y: 0.0,
z: 0.0
}
});
var twistb = new ROSLIB.Message({
linear: {
x: -2.0,
y: 0.0,
z: 0.0
},
angular: {
x: 0.0,
y: 0.0,
z: 0.0
}
});
var twistl = new ROSLIB.Message({
linear: {
x: 0.0,
y: 0.0,
z: 0.0
},
angular: {
x: 0.0,
y: 0.0,
z: 1.6
}
});
var twistr = new ROSLIB.Message({
linear: {
x: 0.0,
y: 0.0,
z: 0.0
},
angular: {
x: 0.0,
y: 0.0,
z: -1.5
}
});
//Amazon Alexa handlers
var handlers = {
'LaunchRequest': function () {
console.log('In Launch Request');
talkmsg = 'Where do you want to go?';
this.emit(':tell', talkmsg);
},
'MessageIntent': function () {
var cmd = this.event.request.intent.slots.Item.value;
if (cmd === FORWARD) {
cmdVel.publish(twistf);
this.emit(':ask', 'Moving forward...Please say another command or cancel if you want to exit');
}
else if (cmd === BACKWARD) {
cmdVel.publish(twistb);
this.emit(':ask', 'Moving backward...Please say another command or cancel if you want to exit');
}
else if (cmd === TURN_LEFT) {
cmdVel.publish(twistl);
this.emit(':ask', 'Turning left...Please say another command or cancel if you want to exit');
}
else if (cmd === TURN_RIGHT) {
cmdVel.publish(twistr);
this.emit(':ask', 'Turning right...Please say another command or cancel if you want to exit');
}
else {
let speechOutput = RECIPE_NOT_FOUND_MESSAGE;
const repromptSpeech = RECIPE_NOT_FOUND_REPROMPT;
this.emit(':ask', speechOutput, repromptSpeech);
}
},
'AMAZON.HelpIntent': function () {
var speechOutput = HELP_MESSAGE;
var reprompt = HELP_REPROMPT;
this.emit(':ask', speechOutput, reprompt);
},
'AMAZON.CancelIntent': function () {
this.emit(':tell', STOP_MESSAGE);
},
'AMAZON.StopIntent': function () {
this.emit(':tell', STOP_MESSAGE);
}
};
exports.handler = function (event, context, callback) {
var alexa = Alexa.handler(event, context);
alexa.APP_ID = APP_ID;
alexa.registerHandlers(handlers);
alexa.execute();
};