-
Notifications
You must be signed in to change notification settings - Fork 0
/
IntentProcessor.cpp
212 lines (201 loc) · 5.61 KB
/
IntentProcessor.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <Arduino.h>
#include "IntentProcessor.h"
#include "Speaker.h"
#include <dotstar_wing.h>
IntentProcessor::IntentProcessor(Speaker *speaker)
{
m_speaker = speaker;
}
/**
* @brief Plays an animal sounds, based on the received intent.
*
* @param intent receveid intent.
* @return IntentResult result of intent processing.
*/
IntentResult IntentProcessor::animalSound(const Intent &intent)
{
// Prints the intent information message
Serial.printf(
"animalSound: confidence=%.f%%; device_name=%s; trait_value=%s; trait_confidence=%.f%%\n",
100 * intent.intent_confidence, intent.device_name.c_str(),
intent.trait_value.c_str(), 100 * intent.trait_confidence
);
// Checks intent confidence is high enough
if (intent.intent_confidence < 0.4)
{
Serial.printf("Only %.f%% certain on intent\n", 100 * intent.intent_confidence);
return FAILED;
}
// Checks intent has a sound name associated
if (intent.device_name.empty())
{
Serial.println("No sound found");
return FAILED;
}
// Checks intent sound name confidence is high enough
if (intent.device_confidence < 0.4)
{
Serial.printf("Only %.f%% certain on sound\n", 100 * intent.device_confidence);
return FAILED;
}
// If all checks passed, plays the relevant sound
// Duck sound
if(intent.device_name == "duck")
{
m_speaker->playDuck();
return SILENT_SUCCESS;
}
// Cow sound
if(intent.device_name == "cow")
{
m_speaker->playCow();
return SILENT_SUCCESS;
}
// Cat sound
if(intent.device_name == "cat")
{
m_speaker->playCat();
return SILENT_SUCCESS;
}
// Dog sound
if(intent.device_name == "dog")
{
m_speaker->playDog();
return SILENT_SUCCESS;
}
// Pig sound
if(intent.device_name == "pig")
{
m_speaker->playPig();
return SILENT_SUCCESS;
}
// Dolphin sound
if(intent.device_name == "dolphin")
{
m_speaker->playDolphin();
return SILENT_SUCCESS;
}
// Lion sound
if(intent.device_name == "lion")
{
m_speaker->playLion();
return SILENT_SUCCESS;
}
// Chicken sound
if(intent.device_name == "chicken")
{
m_speaker->playChicken();
return SILENT_SUCCESS;
}
// Sheep sound
if(intent.device_name == "sheep")
{
m_speaker->playSheep();
return SILENT_SUCCESS;
}
// Crow sound
if(intent.device_name == "crow")
{
m_speaker->playCrow();
return SILENT_SUCCESS;
}
// If no matching animal was found, informs that Marvin can't imitate this animal
else
{
return FAILED;
}
}
IntentResult IntentProcessor::turnOnDevice(const Intent &intent)
{
Serial.printf(
"turnOnDevice: confidence=%.f%%; device_name=%s; trait_value=%s; trait_confidence=%.f%%\n",
100 * intent.intent_confidence, intent.device_name.c_str(),
intent.trait_value.c_str(), 100 * intent.trait_confidence
);
if (intent.intent_confidence < 0.4)
{
Serial.printf("Only %.f%% certain on intent\n", 100 * intent.intent_confidence);
return FAILED;
}
if (intent.device_name.empty())
{
Serial.println("No device found");
return FAILED;
}
if (intent.device_confidence < 0.4)
{
Serial.printf("Only %.f%% certain on device\n", 100 * intent.device_confidence);
return FAILED;
}
if (intent.trait_value.empty())
{
Serial.println("Can't work out the intent action");
return FAILED;
}
if (intent.trait_confidence < 0.4)
{
Serial.printf("Only %.f%% certain on trait\n", 100 * intent.trait_confidence);
return FAILED;
}
bool is_turn_on = intent.trait_value == "on";
// global device name "lights"
if (intent.device_name == "lights")
{
for (const auto &dev_pin : m_device_to_pin)
{
digitalWrite(dev_pin.second, is_turn_on);
}
}
else if (intent.device_name == "matrix") {
if (is_turn_on)
dotstar_wing_on();
else
dotstar_wing_off();
}
else
{
// see if the device name is something we know about
if (m_device_to_pin.find(intent.device_name) == m_device_to_pin.end())
{
Serial.printf("Don't recognise the device '%s'\n", intent.device_name.c_str());
return FAILED;
}
digitalWrite(m_device_to_pin[intent.device_name], is_turn_on);
}
// success!
return SUCCESS;
}
IntentResult IntentProcessor::processIntent(const Intent &intent)
{
Serial.printf(
"processIntent: name=%s; confidence=%.f%%; trait_value=%s; trait_confidence=%.f%%\n",
intent.intent_name.c_str(), 100 * intent.intent_confidence,
intent.trait_value.c_str(), 100 * intent.trait_confidence
);
if (intent.text.empty())
{
Serial.println("No text recognised");
return FAILED;
}
Serial.printf("I heard \"%s\"\n", intent.text.c_str());
if (intent.intent_name.empty())
{
Serial.println("Can't work out what you want to do with the device...");
return FAILED;
}
Serial.printf("Intent is %s\n", intent.intent_name.c_str());
if (intent.intent_name == "Turn_on_device")
{
return turnOnDevice(intent);
}
if (intent.intent_name == "Animal_sound")
{
return animalSound(intent);
}
return FAILED;
}
void IntentProcessor::addDevice(const std::string &name, int gpio_pin)
{
m_device_to_pin.insert(std::make_pair(name, gpio_pin));
pinMode(gpio_pin, OUTPUT);
}