loading
 
Roborealm with Sound Module (MCU)
ronn0011  [73 posts]
5 years
Hi, I am trying to program sound module with MCU communicator, with case as followed in arduino
case 100: // play "0000.ad4"
              sendCommand(0x0000); //delay(3000);
              break;
case 101 : // play "0001.ad4"
              sendCommand(0x0001);//delay(200);
              break;
case 102 : // play "0001.ad4"
              sendCommand(STOP);//delay(200);
              break;  
case 103 : // play "0001.ad4"
              sendCommand(PLAY_PAUSE);//delay(200);
              break;              
    default : break;

However the respond in the sound module is not as expected, when clicking s, by right rr corresponding send value 100, it does send by monitoring the " Watch variable" it does send value at my keyboard however upon realising it goes to 0, so i assumed the value has been sent to arduino trigger the sound 1 which is

case 100: // play "0000.ad4"
              sendCommand(0x0000);
              break;

however, my observation, i need to hold the 's' for 5 second in order the sound to be played, how do I improve on this so just by pressing 's' or other function key it will plays the sounds right away

http://youtu.be/fBq5Rl2olsw

http://youtu.be/iAVzeXl3KO8


I am also having issue on the roborealm freeze quite often so i can close it or do some adjustment I need to force closed with windows task manager

http://youtu.be/DW2CyXdYRMo

below is the robo files and arduino files



#include <Servo.h>

Servo myservo;  // create servo object to control a servo
              

/* SETTING FOR MCU COMMUNICATOR */
#define MCU_GET_ID 1
#define MCU_END_FRAME 2
#define MCU_SET_NUMBER_MAILBOX 3
#define MCU_SET_TEXT_MAILBOX 4
#define MCU_SET_LOGIC_MAILBOX 5
#define MCU_GET_NUMBER_MAILBOX 6
#define MCU_GET_TEXT_MAILBOX 7
#define MCU_GET_LOGIC_MAILBOX 8

unsigned char mailboxToSend[20];
unsigned char mailboxSendFlag[20];

unsigned long intValue[20];
unsigned char logicValue[20];
unsigned char *textValue[20];

unsigned char textStore[1024];
int textStoreTop=0;
/* SETTING FOR SOUND CARD,
   using pin 2,3,8 , with functionality as follows */
const int clockPin = 2;  // the pin number of the clock pin
const int dataPin = 3;  // the pin number of the data pin
const int resetPin = 4;  // the pin number of the reset pin

const unsigned int VOLUME_0 = 0xFFF0;
const unsigned int VOLUME_1 = 0xFFF1;
const unsigned int VOLUME_2 = 0xFFF2;
const unsigned int VOLUME_3 = 0xFFF3;
const unsigned int VOLUME_4 = 0xFFF4;
const unsigned int VOLUME_5 = 0xFFF5;
const unsigned int VOLUME_6 = 0xFFF6;
const unsigned int VOLUME_7 = 0xFFF7;

const unsigned int PLAY_PAUSE = 0xFFFE;
const unsigned int STOP = 0xFFFF;
/* send command to sound card for playing audio */
void sendCommand(unsigned int command) {
  // start bit
  digitalWrite(clockPin, LOW);
  delay(2);

  // bit15, bit14, ... bit0
  for (unsigned int mask = 0x8000; mask > 0; mask >>= 1) {
    if (command & mask) {
      digitalWrite(dataPin, HIGH);
    }
    else {
      digitalWrite(dataPin, LOW);
    }
    // clock low
    digitalWrite(clockPin, LOW);
    delayMicroseconds(200);

    // clock high
    digitalWrite(clockPin, HIGH);
    delayMicroseconds(200);
  }

  // stop bit
  delay(2);
}


void setup()
{
  Serial.begin(115200);
  myservo.attach(9);  
  
  int i;  
  for (i=0;i<20;i++) { mailboxToSend[i]=0; mailboxSendFlag[i]=0; }

  // set pin 13 to output so we can flash the LED  
  pinMode(13, OUTPUT);
  
  //setting for driving two dc motors
  for(i=4;i<=7;i++)
  pinMode(i, OUTPUT);
  
  //Setting for sound card
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(resetPin, OUTPUT);

  digitalWrite(clockPin, HIGH);
  digitalWrite(dataPin, LOW);

  // reset the module
  digitalWrite(resetPin, HIGH);
  delay(100);
  digitalWrite(resetPin, LOW);
  delay(10);
  digitalWrite(resetPin, HIGH);
  delay(100);

  sendCommand(VOLUME_1);
}

unsigned char getByte()
{
  while (Serial.available() <= 0) continue;
  return Serial.read();
}

void loop()
{
  unsigned char buffer[8];

  while (Serial.available()>0)
  {
    unsigned char command;
    unsigned char mailbox;
    unsigned char crc;
    int hold;

    // command is the only byte that should have bit 7 set.  
    do
    {
      command = getByte();
    }
    while ((command&128)==0);

    crc = command;

    command^=128;
    
    switch (command)
    {
      // init
      case  MCU_GET_ID:
        Serial.print("MCUC");
      break;
      case  MCU_SET_NUMBER_MAILBOX:
        if ((buffer[0] = getByte())&128) continue;
        crc ^= buffer[0];
        if ((buffer[1] = getByte())&128) continue;
        crc ^= buffer[1];
        if ((buffer[2] = getByte())&128) continue;
        crc ^= buffer[2];
        if ((buffer[3] = getByte())&128) continue;
        crc ^= buffer[3];
        if ((buffer[4] = getByte())&128) continue;
        crc ^= buffer[4];
        if ((buffer[5] = getByte())&128) continue;
        crc ^= buffer[5];
        if ((buffer[6] = getByte())&128) continue;
        if ((crc&127)!=buffer[6]) continue;
        intValue[buffer[0]] = (unsigned long)buffer[1]|((unsigned long)buffer[2]<<7)|((unsigned long)buffer[3]<<14)|((unsigned long)buffer[4]<<21)|((unsigned long)buffer[5]<<28);
        mailboxToSend[buffer[0]]=0;
      break;
      case  MCU_SET_LOGIC_MAILBOX:
        if ((buffer[0] = getByte())&128) continue;
        crc ^= buffer[0];
        if ((buffer[1] = getByte())&128) continue;
        crc ^= buffer[1];
        if ((buffer[2] = getByte())&128) continue;
        if ((crc&127)!=buffer[2]) continue;
        logicValue[buffer[0]] = buffer[1];
        mailboxToSend[buffer[0]]=0;
      break;
      case  MCU_SET_TEXT_MAILBOX:
        if ((mailbox = getByte())&128) continue;
        crc ^= mailbox;
        hold = textStoreTop;
        textStoreTop--;
        do
        {
          textStoreTop++;
          textStore[textStoreTop] = getByte();
          crc^=textStore[textStoreTop];
        }
        while ((textStoreTop<1024)&&(textStore[textStoreTop]));
        textStoreTop++;
        if ((crc&127)!=getByte()) { textStoreTop=hold; continue; }
        textValue[mailbox] = &textStore[hold];
        mailboxToSend[mailbox]=0;
      break;
      //stream values to mailbox
      case  MCU_GET_NUMBER_MAILBOX:
        mailbox = getByte();
        if (((crc^mailbox)&127)!=getByte()) continue;
        mailboxToSend[mailbox]=MCU_GET_NUMBER_MAILBOX;
      break;
      case  MCU_GET_LOGIC_MAILBOX:
        mailbox = getByte();
        if (((crc^mailbox)&127)!=getByte()) continue;
        mailboxToSend[mailbox]=MCU_GET_LOGIC_MAILBOX;
      break;
      case  MCU_GET_TEXT_MAILBOX:
        mailbox = getByte();
        if (((crc^mailbox)&127)!=getByte()) continue;
        mailboxToSend[mailbox]=MCU_GET_TEXT_MAILBOX;
      break;
      case  MCU_END_FRAME:
        processMailboxes();
        // note that Text values are lost after being processed as we
        // will reuse the buffer for the next frame
        textStoreTop=0;
      break;
    }
  }
  
  int i,j;
  unsigned long lvalue;
  unsigned char *tvalue;
  unsigned char crc;

  for (i=0;i<20;i++)
  {
    if (mailboxToSend[i]&&mailboxSendFlag[i])
    {
      switch (mailboxToSend[i])
      {
        case  MCU_GET_NUMBER_MAILBOX:
          lvalue = intValue[i];
          
          buffer[0]=MCU_GET_NUMBER_MAILBOX|128;
          buffer[1]=i;
          buffer[2]=lvalue&127;
          buffer[3]=(lvalue>>7)&127;
          buffer[4]=(lvalue>>14)&127;
          buffer[5]=(lvalue>>21)&127;
          buffer[6]=(lvalue>>28)&127;
          buffer[7]=(buffer[0]^buffer[1]^buffer[2]^buffer[3]^buffer[4]^buffer[5]^buffer[6])&127;

          Serial.write(buffer, 8);
        break;
        case  MCU_GET_LOGIC_MAILBOX:
          buffer[0]=MCU_GET_LOGIC_MAILBOX|128;
          buffer[1]=i;
          buffer[2]=logicValue[i]&127;
          buffer[3]=(buffer[0]^buffer[1]^buffer[2])&127;

          Serial.write(buffer, 4);
        break;
        case  MCU_GET_TEXT_MAILBOX:
          tvalue = textValue[i];

          buffer[0]=MCU_GET_TEXT_MAILBOX|128;
          buffer[1]=i;
          crc = buffer[0]^buffer[1];
          j=0;
          while ((tvalue[j]!=0)&&(j<254))
          {
            tvalue[j] &= 127;
            crc ^= tvalue[j];
            j++;
          }
          j++;
          
          buffer[2] = crc&127;

          Serial.write(buffer, 2);
          Serial.write(tvalue, j);          
          Serial.write(&buffer[2], 1);
        break;
      }
      mailboxSendFlag[i]=0;
    }
  }
}



/* PWM CONTROL,
   using pin 4,5,6,7
*/
//Standard PWM DC control
int E1 = 5;     //M1 Speed Control
int E2 = 6;     //M2 Speed Control
int M1 = 4;    //M1 Direction Control
int M2 = 7;    //M1 Direction Control

///For previous Romeo, please use these pins.
//int E1 = 6;     //M1 Speed Control
//int E2 = 9;     //M2 Speed Control
//int M1 = 7;    //M1 Direction Control
//int M2 = 8;    //M1 Direction Control


void stop(void)                    //Stop
{
  analogWrite(E1,0);  //?
  digitalWrite(E1,LOW);  
  analogWrite(E1,0);  //?
  digitalWrite(E2,LOW);      
}  
void advance(char a,char b)          //Move forward
{
  analogWrite(E1,a);      //PWM Speed Control
  digitalWrite(M1,HIGH);    
  analogWrite(E2,b);    
  digitalWrite(M2,HIGH);
}  
void back_off(char a,char b)          //Move backward
{
  analogWrite (E1,a);
  digitalWrite(M1,LOW);  
  analogWrite (E2,b);    
  digitalWrite(M2,LOW);
}
void turn_L(char a,char b)             //Turn Left
{
  analogWrite (E1,a);
  digitalWrite(M1,LOW);    
  analogWrite (E2,b);    
  digitalWrite(M2,HIGH);
}
void turn_R(char a,char b)             //Turn Right
{
  analogWrite (E1,a);
  digitalWrite(M1,HIGH);    
  analogWrite (E2,b);    
  digitalWrite(M2,LOW);
}

void driveMotors(int speed1, int speed2)
{
  if(speed1<0)  
  {
    speed1=-speed1;
    M1=LOW;
  }
  else
  {
    M1=HIGH;
    
  }
  if(speed2<0)  
  {
    speed2=-speed2;
    M2=LOW;
  }
  else
  {
    M2=HIGH;
  }
  analogWrite (E1,speed1);
  digitalWrite(M1,HIGH);    
  analogWrite (E2,speed2);    
  digitalWrite(M2,LOW);  
}

// called after mailboxes from the PC have been sent ... this is where your code will go!
void processMailboxes()
{
  //controlling drive motors
  driveMotors(intValue[0],intValue[1]);
  
  //controlling servo
  int pos = 180/200*(intValue[2]+100);
  myservo.write(pos);
  
  //controlling sound card
// sendCommand(0x0000);
  //delay(200);
  switch(intValue[3])
  {

case 100: // play "0000.ad4"
              sendCommand(0x0000); //delay(3000);
              break;
case 101 : // play "0001.ad4"
              sendCommand(0x0001);//delay(200);
              break;
case 102 : // play "0001.ad4"
              sendCommand(STOP);//delay(200);
              break;  
case 103 : // play "0001.ad4"
              sendCommand(PLAY_PAUSE);//delay(200);
              break;              
    default : break;
    
  }
  
  
  
  
  /*
  char val=0;
  char speed=255;
  switch(val)
  {
  case 'w'://Move Forward
    advance (speed,speed);   //move forward in max speed
    break;
  case 's'://Move Backward
    back_off (speed,speed);   //move back in max speed
    break;
  case 'a'://Turn Left
    turn_L (speed/2,speed/2);
    break;      
  case 'd'://Turn Right
    turn_R (speed/2,speed/2);
    break;
  case 'z':
    //Serial.println("Hello");
    break;
  case 'x':
    stop();
    break;
  }  
  */
  /* Here are samples only
  // Playing sounds
  // play "0000.ad4"
  sendCommand(0x0000);
  delay(1000);

  // play "0001.ad4"
  sendCommand(0x0001);
  delay(1000);

  // stop playing
  sendCommand(STOP);
  delay(1000);
  
  // MCU Communicator
  // variables value will be in one of intValue, textValue or logicValue
  // arrays and address by the mailbox number. Note the variable name is
  // NOT sent by identified by the corresponding mailbox number
  // as seen in the MCU Communicator GUI

  // we have set mailbox 0 to a logic value coming from RR. In this case
  // we attach it to the IMAGE_COUNT variable so we flash for every few
  // frames.
  if (intValue[0])
    digitalWrite(13, HIGH);
  else
    digitalWrite(13, LOW);

  // To send a value back to mailbox #1 specify it as a Get in the MCU Communicator
  // interface and type in a variable name that will hold that value. Indicate its type
  // as Number and then use the following to send a value of IMAGE_COUNT+100 back to the PC
  // into that variable. Don't forget to set the flag!
  intValue[1]=intValue[0]+100;
  mailboxSendFlag[1] = 1;
  
  // To send back a string configure the MCU Communicator to a text type and use
  textValue[2] = (unsigned char *)"this is a test";
  mailboxSendFlag[2] = 1;

  // Now we convert mailbox 3's string into an uppercase version
  // of it and send it back via mailbox 4 into another string
  // variable. Note this is not a very reliable uppercase conversion
  // but just illustrates the point of string manipulation
  static unsigned char replyStr[256];
  int i;  
  for (i=0;textValue[3][i];i++)
  {
    if (textValue[3][i]!=' ')  
      replyStr[i]=textValue[3][i]+('A'-'a');
    else
      replyStr[i]=' ';
  }
  replyStr[i]=0;
  
  textValue[4]=replyStr;
  mailboxSendFlag[4] = 1;
  
  // and set mailbox 5 to the length of this string
  intValue[5] = i;
  mailboxSendFlag[5] = 1;
  
  // invert the logic on mailbox 6
  logicValue[7] = logicValue[6]^1;
  mailboxSendFlag[7] = 1;
  */
}

program.robo
Anonymous 5 years
ron,

Are you saying that you need to hold the key down for 5 seconds BEFORE the sound starts? Or that it starts as soon as you hold the key down but then stops when you release it?

It seems that the code is sending the correct key value but your first Set_Variable module will reset it to zero as soon as you release the key.

Does the sound file start with 5 seconds of silence? Or should it start playing the sound right away?

STeven.
ronn0011  [73 posts] 5 years
Hi Steven
1 yes by pressing 5 second and realise, the sound is playing upon realising the buton. However try pressing < 5 secs, it does bot trigger the sound.

2. The sound does not start with 5 sec of silence. It started immediately after realising the key button. (however the condition now has to hold the button a little long about 5 sec)

"Which it suppose trigger just by pressing abd realise key button instanteniously"

This forum thread has been closed due to inactivity (more than 4 months) or number of replies (more than 50 messages). Please start a New Post and enter a new forum thread with the appropriate title.

 New Post   Forum Index