Pages: [1]   Go Down
Author Topic: Voice control Servo (easyvr)  (Read 1418 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey,

so iv got an arduino with an easyvr sheild on top. iv been trying to get it to rotate a servo a certain amount of degrees when i say a certain command. my code works fine with leds but when i try putting in code for a servo it just freezes after the first command

here is my code

Code:
#if defined(ARDUINO) && ARDUINO >= 100
  #include "Arduino.h"
  #include "SoftwareSerial.h"
  SoftwareSerial port(12,13);
#else // Arduino 0022 - use modified NewSoftSerial
  #include "WProgram.h"
  #include "NewSoftSerial.h"
  NewSoftSerial port(12,13);
#endif

#include "EasyVR.h"
#include <Servo.h>
EasyVR easyvr(port);
Servo myservo;

//Groups and Commands
enum Groups
{
  GROUP_0  = 0,
  GROUP_1  = 1,
};

enum Group0
{
  G0_WATSON = 0,
};

enum Group1
{
  G1_LIGHTS_ON = 0,
  G1_ITS_DARK = 1,
  G1_FORGETTING_SOMETHING = 2,
  G1_LIGHTS_OFF = 3,
  G1_IM_GOING_OUT = 4,
  G1_GOOD_NIGHT = 5,
};


EasyVRBridge bridge;

int8_t group, idx;

void setup()
{
  
  // bridge mode?
  if (bridge.check())
  {
    cli();
    bridge.loop(0, 1, 12, 13);
  }
  // run normally
  Serial.begin(9600);
  port.begin(9600);

  if (!easyvr.detect())
  {
    Serial.println("EasyVR not detected!");
    for (;;);
  }

  easyvr.setPinOutput(EasyVR::IO1, LOW);
  Serial.println("EasyVR detected!");
  easyvr.setTimeout(5);
  easyvr.setLanguage(0);

  group = EasyVR::TRIGGER; //<-- start group (customize)
}

void action();

void loop()
{
  easyvr.setPinOutput(EasyVR::IO1, HIGH); // LED on (listening)

  Serial.print("Say a command in Group ");
  Serial.println(group);
  easyvr.recognizeCommand(group);

  do
  {
    // can do some processing while waiting for a spoken command
  }
  while (!easyvr.hasFinished());
  
  easyvr.setPinOutput(EasyVR::IO1, LOW); // LED off

  idx = easyvr.getWord();
  if (idx >= 0)
  {
    // built-in trigger (ROBOT)
    // group = GROUP_X; <-- jump to another group X
    return;
  }
  idx = easyvr.getCommand();
  if (idx >= 0)
  {
    // print debug message
    uint8_t train = 0;
    char name[32];
    Serial.print("Command: ");
    Serial.print(idx);
    if (easyvr.dumpCommand(group, idx, name, train))
    {
      Serial.print(" = ");
      Serial.println(name);
    }
    else
      Serial.println();
    easyvr.playSound(0, EasyVR::VOL_FULL);
    // perform some action
    action();
  }
  else // errors or timeout
  {
    if (easyvr.isTimeout())
      Serial.println("Timed out, try again...");
    int16_t err = easyvr.getError();
    if (err >= 0)
    {
      Serial.print("Error ");
      Serial.println(err, HEX);
    }
  }
}

void action()
{
    switch (group)
    {
    case GROUP_0:
      switch (idx)
      {
      case G0_WATSON:
        // write your action code here
        group = GROUP_1; // group = GROUP_X; <-- or jump to another group X for composite commands
        break;
      }
      break;
    case GROUP_1:
      switch (idx)
      {
      case G1_LIGHTS_ON:
        myservo.attach(8);
        myservo.write(180);// write your action code here
         group = GROUP_0;// group = GROUP_X; <-- or jump to another group X for composite commands
        break;
      case G1_ITS_DARK:
        myservo.attach(8);
        myservo.write(180);// write your action code here
         group = GROUP_0;// group = GROUP_X; <-- or jump to another group X for composite commands
        break;
      case G1_FORGETTING_SOMETHING:
        myservo.attach(8);
        myservo.write(180);// write your action code here
         group = GROUP_0;// group = GROUP_X; <-- or jump to another group X for composite commands
        break;
      case G1_LIGHTS_OFF:
        myservo.attach(8);
        myservo.write(90);// write your action code here
         group = GROUP_0;// group = GROUP_X; <-- or jump to another group X for composite commands
        break;
      case G1_IM_GOING_OUT:
        myservo.attach(8);
        myservo.write(90);// write your action code here
         group = GROUP_0;// group = GROUP_X; <-- or jump to another group X for composite commands
        break;
      case G1_GOOD_NIGHT:
        myservo.attach(8);
        myservo.write(90);// write your action code here
         group = GROUP_0;// group = GROUP_X; <-- or jump to another group X for composite commands
        break;
      }
      break;
    }
}

Moderator edit: And there is your code without smileys smiley-wink
« Last Edit: January 29, 2013, 06:29:50 am by AWOL » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50115
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why are you attaching the servos in action()? Servos should be attached in setup().

Do you know what pins the easyvr shield uses? Or, what timers? Perhaps you need to post a link to the shield.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

when i attatch the servos in setup() it wont respond at all compared to if they r in action() it responds once before freezing.

here is a link to the sheild as requested:

http://www.veear.eu/products/easyvr-arduino-shield/
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi there,

First, let me preface this by saying I'm very new to arduino and programming in general.  It seems you know a lot more about it than I do, but I might have some info that would be of use.

I recently was working on a project to control a servo using an Arduino Uno to control a servo and was using some serial communication just for debugging purposes (the system would be stand alone once complete)

I was having some of the same problems you were with freezes in my first attempt at writing the software, as well as random servo jumps and movement while searching.  After a significant amount of googling, I found that there was an interupt problem with the serial communication which I believe was interfereing with the PWM pins ability to constantly refresh the servo position.  I'm not overly sure how to help out in your case, but maybe this will put you on the right track.  I expect that is the reason that attaching your sevo in the action section leads to freezing after commands instead of freezing in the set up. 

Hopefully this sets you on the right track!

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50115
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'd be investigating how much (or little) free memory you have. Not enough would be my guess.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

how would i go about fixing the pwm. I'm pretty sure i have enough memory because the easyvr and arduino can handle up to 32 commands
Logged

Pages: [1]   Go Up
Jump to: