///////////////////////////////////////////////////////////////////////////////
// Include files
///////////////////////////////////////////////////////////////////////////////
#include "Arduino.h"
#if !defined(SERIAL_PORT_MONITOR)
#error "Arduino version not supported. Please update your IDE to the latest version."
#endif
#if defined(__SAMD21G18A__)
// Shield Jumper on HW (for Zero, use Programming Port)
#define port SERIAL_PORT_HARDWARE
#define pcSerial SERIAL_PORT_MONITOR
#elif defined(SERIAL_PORT_USBVIRTUAL)
// Shield Jumper on HW (for Leonardo and Due, use Native Port)
#define port SERIAL_PORT_HARDWARE
#define pcSerial SERIAL_PORT_USBVIRTUAL
#else
// Shield Jumper on SW (using pins 12/13 or 8/9 as RX/TX)
#include "SoftwareSerial.h"
SoftwareSerial port(12, 13);
#define pcSerial SERIAL_PORT_MONITOR
#endif
#include "EasyVR.h"
EasyVR easyvr(port);
///////////////////////////////////////////////////////////////////////////////
// Variables globales
///////////////////////////////////////////////////////////////////////////////
//Grammars and Words
enum Wordsets
{
SET_1 = -1,
SET_2 = -2,
SET_3 = -3,
};
enum Wordset1 // SET_1
{
S1_ACTION = 0,
S1_MOVE = 1,
S1_TURN = 2,
S1_RUN = 3,
S1_LOOK = 4,
S1_ATTACK = 5,
S1_STOP = 6,
S1_HELLO = 7,
};
enum Wordset2 // SET_2
{
S2_LEFT = 0,
S2_RIGHT = 1,
S2_UP = 2,
S2_DOWN = 3,
S2_FORWARD = 4,
S2_BACKWARD = 5,
};
enum Wordset3 // SET_3
{
S3_ZERO = 0,
S3_ONE = 1,
S3_TWO = 2,
S3_THREE = 3,
S3_FOUR = 4,
S3_FIVE = 5,
S3_SIX = 6,
S3_SEVEN = 7,
S3_EIGHT = 8,
S3_NINE = 9,
S3_TEN = 10,
};
// use negative group for wordsets
int8_t group, idx;
int etat=0;
boolean sw_stop;
///////////////////////////////////////////////////////////////////////////////
// Initialisation
///////////////////////////////////////////////////////////////////////////////
void setup()
{
analogWrite(11, 0);
// setup PC serial port
pcSerial.begin(9600);
bridge:
// bridge mode?
int mode = easyvr.bridgeRequested(pcSerial);
switch (mode)
{
case EasyVR::BRIDGE_NONE:
// setup EasyVR serial port
port.begin(9600);
// run normally
pcSerial.println(F("Bridge not requested, run normally"));
pcSerial.println(F("---"));
break;
case EasyVR::BRIDGE_NORMAL:
// setup EasyVR serial port (low speed)
port.begin(9600);
// soft-connect the two serial ports (PC and EasyVR)
easyvr.bridgeLoop(pcSerial);
// resume normally if aborted
pcSerial.println(F("Bridge connection aborted"));
pcSerial.println(F("---"));
break;
case EasyVR::BRIDGE_BOOT:
// setup EasyVR serial port (high speed)
port.begin(115200);
pcSerial.end();
pcSerial.begin(115200);
// soft-connect the two serial ports (PC and EasyVR)
easyvr.bridgeLoop(pcSerial);
// resume normally if aborted
pcSerial.println(F("Bridge connection aborted"));
pcSerial.println(F("---"));
break;
}
// initialize EasyVR
while (!easyvr.detect())
{
pcSerial.println(F("EasyVR not detected!"));
for (int i = 0; i < 10; ++i)
{
if (pcSerial.read() == '?')
goto bridge;
delay(100);
}
}
pcSerial.print(F("EasyVR detected, version "));
pcSerial.print(easyvr.getID());
if (easyvr.getID() < EasyVR::EASYVR3)
easyvr.setPinOutput(EasyVR::IO1, LOW); // Shield 2.0 LED off
if (easyvr.getID() < EasyVR::EASYVR)
pcSerial.print(F(" = VRbot module"));
else if (easyvr.getID() < EasyVR::EASYVR2)
pcSerial.print(F(" = EasyVR module"));
else if (easyvr.getID() < EasyVR::EASYVR3)
pcSerial.print(F(" = EasyVR 2 module"));
else
pcSerial.print(F(" = EasyVR 3 module"));
pcSerial.print(F(", FW Rev."));
pcSerial.println(easyvr.getID() & 7);
//easyvr.setDelay(0); // speed-up replies
easyvr.setTimeout(31);
easyvr.setLanguage(5);
easyvr.setMicDistance(2);
easyvr.setKnob(4);
// Entrées
pinMode(2, INPUT); // SW stop
pinMode(5, OUTPUT); // Moteur ch A
pinMode(6, OUTPUT); // Frein ch A
pinMode(7, OUTPUT); // Direction ch A
pinMode(A1, INPUT); // Lecture analog I
group = -1;
analogWrite(11, 0);
}
void action();
///////////////////////////////////////////////////////////////////////////////
// Programme principal
///////////////////////////////////////////////////////////////////////////////
void loop()
{
// Led ON
if (easyvr.getID() < EasyVR::EASYVR3)
easyvr.setPinOutput(EasyVR::IO1, HIGH); // LED on (listening)
// Reconnaissance Wordset
// Ecriture du n° de Wordet dans group
if (group < 0)
{
pcSerial.print("Say a word in Wordset ");
pcSerial.println(-group);
easyvr.recognizeWord(-group);
}
while (!easyvr.hasFinished());
// Led OFF
if (easyvr.getID() < EasyVR::EASYVR3)
easyvr.setPinOutput(EasyVR::IO1, LOW);
// Ecriture dans l'index du mot
idx = easyvr.getWord();
if (idx >= 0)
{
// Indication du mot reconnu
uint8_t flags = 0, num = 0;
char name[32];
pcSerial.print("Word: ");
//pcSerial.print(idx);
if (easyvr.dumpGrammar(-group, flags, num))
{
for (uint8_t pos = 0; pos < num; ++pos)
{
if (!easyvr.getNextWordLabel(name))
break;
if (pos != idx)
continue;
pcSerial.print(F(" = "));
pcSerial.println(name);
break;
}
}
action();
return;
}
// Si erreur
else
{
if (easyvr.isTimeout())
pcSerial.println("Timed out, try again...");
int16_t err = easyvr.getError();
if (err >= 0)
{
pcSerial.print("Error ");
pcSerial.println(err, HEX);
}
}
}
void action()
{
switch(group)
{
case SET_1:
switch(idx)
{
// Si user dit "Salut"
case S1_HELLO:
if(etat==0)
{
Serial.println("Salut ok");
easyvr.playSound(2, EasyVR::VOL_FULL);
group=SET_2;
etat=1;
break;
}
// Si user dit "Action"
case S1_ACTION:
if(etat==2)
{
Serial.println("Action ok");
easyvr.playSound(3, EasyVR::VOL_FULL);
digitalWrite(7, LOW); // Direction ch B (haut)
digitalWrite(6, LOW); // Désactive frein ch B
do
{
analogWrite(5, 255); // Channel B = full
Serial.print("Courant boucle = ");
Serial.println(analogRead(A0));
}while(analogRead(A0)>17);
Serial.print("Courant out = ");
Serial.println(analogRead(A0));
analogWrite(5, 0);
etat=3;
group=SET_2;
break;
}
}
break;
case SET_2:
switch(idx)
{
// Si user dit "Vers le haut"
case S2_UP:
if(etat==1)
{
Serial.println("Vers le haut ok");
easyvr.playSound(1, EasyVR::VOL_FULL);
etat=2;
group=-1;
break;
}
case S2_DOWN:
if(etat==3)
{
Serial.println("Vers le bas ok");
digitalWrite(7, HIGH); // Direction ch B (bas)
digitalWrite(6, LOW); // Désactive frein ch B
do
{
analogWrite(5, 255); // Channel B = full
sw_stop = digitalRead(2);
}while(!sw_stop);
analogWrite(5, 0);
etat=0;
group=-1;
break;
}
}
break;
}
}