Go Down

Topic: Power to Servo and VRBot (Read 1 time) previous topic - next topic

JCDwight

So I finally got my VRBot voice recognition module working with my Arduino Duemilanove, it lights an LED plugged into serial pin 9 and ground when I say a word, and it goes out when a different word is said.

The main goal is to get it to rotate a servo 90 degrees when one word is said, and back when the next is said in order to lock and unlock a box from the inside.  I am not familiar enough with electronics to know if I can just attach two things to the one 5V pin on the arduino and ground.  I saw a breadboard shield that allowed two servos to be powered, but space is limited in this project and id rather just hard wire it if at all possible.  I dont want to blow up my board or VRBot so I figured I'd ask for guidance first.

PaulS

Quote
I am not familiar enough with electronics to know if I can just attach two things to the one 5V pin on the arduino and ground.

You can attach any number of things to the +V pin, as long as the total current draw does not exceed the capabilities of the board. Typically, though, you should not be powering the servo from the Arduino. Servos can draw more current than the Arduino should be called on to provide.

wildbill

I recall some recentish threads where people were having trouble getting the VRBOT to work. Some of them would probably be interested to see your working code and circuit.

edit: typo

JCDwight

Where should I post my VRBot code?  It was actually a simple fix, the PDF that came with the thing was a little ambiguous as to how things worked, but I used a lot of trial and error to figure out just how the thing connects and uses SI and SD commands.

JCDwight

As far as powering the servo from a separate power supply, I kind of want to keep things as simple as possible, it will be plugged into the wall using a 5v wall wart and the USB plug.  The servo will only use power a small portion of the time, less than 3 seconds every few hours or so, if I am estimating correctly on revolution time with a load.

I could put a battery in for the servo, but I'd like to use that as a last measure, id like the whole thing to be powered by one plug right into the wall.

JCDwight

Also the servo I ordered is small and uses 5v, not sure if that is relevant.

ruandp

Hi JCD,  I am also a newby and would really appreciate it if could share your code.  I've been looking around on the net and everything I see seems to be quite complicated and people don't seem to add commentary line to explain what the seperate line mean.

JCDwight

This is my code below,  the most important thing that wasn't too obvious to me is that you need to short pins 2 & 4 and upload this code to actually train SD commands, then short pins 2 & 3 when you want to actually use the commands.

I commented specifically on functionality using ********(Comment)********, so read that, ignore the other stuff.

I'll eventually clean up this code and get rid of everything not important to SD commands that I'm using, but as for right now it's sloppy altered code.

#include <SoftwareSerial.h>
#include "protocol.h"

uint8_t _receivePin;
uint8_t _transmitPin;
long _baudRate;
int _bitPeriod;

void setup(){  
 
 // PC UART Init
 pinMode(0, INPUT);         // sets the digital pin as input
 pinMode(1, OUTPUT);        // sets the digital pin as output

 // VRbot UART Init
 pinMode(12, INPUT);        // sets the digital pin as input
 pinMode(13, OUTPUT);       // sets the digital pin as output
 // Input Output Init
 pinMode(2,INPUT);          // sets the digital pin as input
 pinMode(3,OUTPUT);         // sets the digital pin as output
 pinMode(4,OUTPUT);         // sets the digital pin as output
 
 pinMode(9,OUTPUT);         // sets the digital pin as output ********(My own pin hooked to a 1k ohm resistor and red LED for testing purposes)********
 
 
 digitalWrite(9,LOW);       // Set pin 9 low ********(LED off)********
 digitalWrite(3,HIGH);      // Set pin 3 high
 digitalWrite(4,LOW);       // Set pin 4 low
 
 // connect Di2 to Di3 to enter Normal mode - Di2 is high
 // connect Di2 to Di4 to enter Bridge mode - Di2 is low
 // bridge mode allow direct communication between VRbot module and VRbot GUI software

 if (digitalRead(2)==LOW){   // if Di2 is LOW enter Bridge mode
     
     while(1)
     {
       int pc2vr = digitalRead(0);
       digitalWrite(13, pc2vr);
   
       int vr2pc = digitalRead(12);
       digitalWrite(1, vr2pc);
    }
 }

 // if Di2 is HIGH enter Normal mode
 Serial.begin(9600);
 delay(200);
 Serial.println("Arduino 2009 VRBot control program");
 Serial.println("Normal Mode");
 
 // Set up and detect VRbot
 
 VRbot_setup();  
 
 if (!VRbot_Detect())                
    Serial.println("VRbot NA");
 else {
      Serial.println("VRbot detected");
     
      // Set VRbot timeout to 5 seconds
      Serial.println("Setting timeout to: 5 seconds");
      VRbot_SetTimeout(5);            
     
      // Set VRbot language to English      
      Serial.println("Setting Language to: English");
     
      VRbot_SetLanguage(0);  
 }  
}

void loop(){
 
 // implement speaker independent recognition
 //SI_Recognition();
 
 // or speaker dependent recognition ********Comment out 2 lines above to not use SI sets, and the only thing in the loop should be the one line below********
  SD_Recognition();  
 // according to your need
}

/*******************************************************************************
* Speaker Independent recognition function:
*
* Wait for trigger word 'Robot'
* Set wordset 1
* Wait for command MOVE or command TURN, errors or other commands are ignored
* Set wordset 2
* Wait for command FOWRWARD or command BACWARD for MOVE, errors or other commands are ignored
* or
* Wait for command LEFT or command RIGHT for TURN, errors or other commands are ignored
*
********************************************************************************/

void SI_Recognition()
{
 int cmd;
 
 Serial.println("Say Trigger Word!");
 digitalWrite(9,LOW);
 VRbot_RecognizeSI(0);       // start SI trigger word recognition and wait for trigger
 cmd = VRbot_CheckResult();  // check result
 
 if( cmd == -1) // timeout
 {
   Serial.println("Timeout");
   
   return;
 }
 
 if( cmd == -2) // error
 {
   Serial.println("Error Trigger");
   return;
 }
 
 Serial.println("Wordset 1");
 Serial.println("Say a command!");
 //digitalWrite(4,LOW);  // LED ON

 VRbot_RecognizeSI(1);        // start SI recognition of wordset 1
 cmd = VRbot_CheckResult();   // check recognition result  
 digitalWrite(9,HIGH);
 switch (cmd){
   
   case -2: // Error
     Serial.println("Error");
     break;
   
   case -1: // Timoeut
     Serial.println("Timeout");
     break;
         
   case 1: // MOVE
     Serial.println("Commmand: MOVE");
     digitalWrite(9,HIGH);
     delay (200);
     
     Serial.println("Wordset 2");
     Serial.println("Say a command!");
     
     VRbot_RecognizeSI(2);       // start SI recognition wordset 2
     cmd = VRbot_CheckResult();  // check recognition result
     
     switch (cmd){
       case -2: // Error
         Serial.println("Error");
         break;
   
       case -1: // Timoeut
         Serial.println("Timeout");
         break;
         
       case 4:  // FORWARD
         Serial.println("Commmand: FWD");          
         break;

       case 5:  // BACWARD
         Serial.println("Commmand: BWD");
         break;

       default: // Other command
         Serial.println("Commmand: other!");
         break;
     }
     
     break;
     
   case 2:  // TURN
     Serial.println("Commmand: TURN");
     delay (200);
     
     Serial.println("Wordset 2");
     Serial.println("Say a command!");
     
     VRbot_RecognizeSI(2);       // start SI recognition wordset 2    
     cmd = VRbot_CheckResult();  // check recognition result
     
     switch (cmd){
       case -2: // Error
         Serial.println("Error");
         break;
   
       case -1: // Timoeut
         Serial.println("Timeout");
         break;
       case 0: // LEFT
         Serial.println("Commmand: LEFT");
         break;
       case 1:
         Serial.println("Commmand: RIGHT");
         break;
       default: // Other command
         Serial.println("Commmand: other!");
         break;
     }
     
     break;
   
   default: // Other command
     Serial.println("Commmand: other!");
     delay(200);
     break;
 
 }
 
}

// Speaker Dependent recognition function
// Wait for user trigger word set by user with VRBot GUI
// Set wordset Group 1
// Wait for command 0 or command 1 set by user with VRBot GUI, other commands are ignored

void SD_Recognition()
{
 int cmd;
   
 Serial.println("Say Trigger!");
 
 //VRbot_RecognizeSD(0);      // start SD trigger word recognition and wait for trigger********I commented this out as I did not want to use a trigger word********
 //cmd = VRbot_CheckResult(); // check recognition result********This too, cuz there's nothing to check for********

 //if( cmd == -1) // timeout
 //{
 //  Serial.println("Timeout");
 //  return;
 //}
 
 //if( cmd == -2) // error
 //{
 //  Serial.println("Error Trigger");
 //  return;
 //}
 
 //Serial.println("Group 1");
 //Serial.println("Say a command!");
 
 VRbot_RecognizeSD(1);        // start SD recognition group 1 and wait for a command********This uses SD commands from group 1, which is the first group of custom words that aren't trigger words********
 cmd = VRbot_CheckResult();   // check recognition result

 switch (cmd){
   
   case -2: // Error
     Serial.println("Error");
     break;

   case -1: // Timoeut
     Serial.println("Timeout");
     break;
 
   case 0: // USER SD WORD 0  ********When word 0 is detected, turn the LED on********
     digitalWrite(9,HIGH);
     delay(1000); ********Delayed one second, probably not neccesary********
     break;

   case 1: // USER SD WORD 1
     digitalWrite(9,LOW);  ********When word 1 is detected, turn LED off********
     delay(1000);
     break;
   
   default: // Other command
     Serial.println("Commmand: error!");
     break;
   
 }
 ******** Everything below this you can ignore, it's setup and code to detect spoken words********
}

/*******************************************************************************/

void VRbot_setup()
{
 _baudRate = 9600;
 _bitPeriod = 1000000 / _baudRate;
 _receivePin = 12;
 _transmitPin = 13;
 digitalWrite(_transmitPin, LOW);
 delayMicroseconds( _bitPeriod);
}

unsigned char VRbot_read(void)
{
 uint8_t val = 0;
 int bitDelay = _bitPeriod - clockCyclesToMicroseconds(100);
 
 // one byte of serial data (LSB first)
 // ...--\    /--\/--\/--\/--\/--\/--\/--\/--\/--...
 //    \--/\--/\--/\--/\--/\--/\--/\--/\--/
 //   start  0   1   2   3   4   5   6   7 stop


JCDwight

 while (digitalRead(_receivePin));

 // confirm that this is a real start bit, not line noise
 if (digitalRead(_receivePin) == LOW) {
   // frame start indicated by a falling edge and low start bit
   // jump to the middle of the low start bit
   delayMicroseconds(bitDelay / 2 - clockCyclesToMicroseconds(50));
   
   // offset of the bit in the byte: from 0 (LSB) to 7 (MSB)
   for (int offset = 0; offset < 8; offset++) {
   // jump to middle of next bit
   delayMicroseconds(bitDelay);
   
   // read bit
   val |= digitalRead(_receivePin) << offset;
   }
   
   delayMicroseconds(_bitPeriod);
   
   return val;
 }
 
 return -1;
}

void VRbot_write(uint8_t b)
{
 if (_baudRate == 0)
   return;
   
 int bitDelay = _bitPeriod - clockCyclesToMicroseconds(50); // a digitalWrite is about 50 cycles
 byte mask;

 digitalWrite(_transmitPin, LOW);
 delayMicroseconds(bitDelay);

 for (mask = 0x01; mask; mask <<= 1) {
   if (b & mask){ // choose bit
     digitalWrite(_transmitPin,HIGH); // send 1
   }
   else{
     digitalWrite(_transmitPin,LOW); // send 1
   }
   delayMicroseconds(bitDelay);
 }

 digitalWrite(_transmitPin, HIGH);
 delayMicroseconds(bitDelay);
}

/*******************************************************************************/

unsigned char VRbot_Detect(void) {
 unsigned char i;
 for (i = 0; i < 5; ++i) {
   delay(100);    
   VRbot_write(CMD_BREAK);        
   if ( VRbot_read() == STS_SUCCESS)
     return 255;
 }
 return 0;
}

unsigned char VRbot_SetLanguage(unsigned char lang) {
       
 VRbot_write(CMD_LANGUAGE);
 delay(5);
 VRbot_write(ARG_ZERO + lang);

 if (VRbot_read() == STS_SUCCESS)
   return 255;
 return 0;
}

void VRbot_RecognizeSD(unsigned char group) {
 VRbot_write(CMD_RECOG_SD);
 delay(5);
 VRbot_write(ARG_ZERO + group);
}

void VRbot_RecognizeSI(unsigned char ws) {
 VRbot_write(CMD_RECOG_SI);
 delay(5);
 VRbot_write(ARG_ZERO + ws);
}

void VRbot_SetTimeout(unsigned char s) {
 VRbot_write(CMD_TIMEOUT);
 delay(5);
 VRbot_write(ARG_ZERO + s);
 delay(5);
}

signed char VRbot_CheckResult(void) {
 
 unsigned char rx;
 rx = VRbot_read();
 if (rx == STS_SIMILAR || rx == STS_RESULT) {
   delay(5);
   VRbot_write(ARG_ACK);
   return (VRbot_read() - ARG_ZERO);
 }
 if (rx == STS_TIMEOUT)
   return -1;
   
 return -2;
}

Go Up