Robot beep (SAY) example code

From MobileRobots Research and Academic Customer Support

Jump to: navigation, search

Client command 15 ("SAY") can be used to make the robot's built in piezo buzzer (speaker) beep a short tune.

This command takes as an argument an array or buffer of unsigned 8-bit bytes. This array is organized as pairs of bytes; the first byte in each pair is a duration value (with special units) and the second is a MIDI note: Middle C (C4)=60 decimal, C#=6l, D=62, D#=63, E=64, F=65, F#=66, G=67, G#=68, A=69, etc. up to 127 and down to 1. (Values greater than 127 are interpreted differently: 127 is subtracted, and it is multiplied by 32 to compute the tone's frequency in hz.) To convert from milliseconds to the special duration value to be used in the duration byte, divide milliseconds by 20.

This function shows one way you can do it with ARIA. The function accepts as an argument a std::vector of <double, unsigned char> std::pair objects to store the duration/note pairs, and builds the byte array to send to the robot from that.

/** Send a SAY command to make the robot beep a short tune.
 *  Each pair in the @a tune std::vector argument consists
 *  of a duration (miliseconds) and the note's tone. If the tone
 *  value is 0, no sound is played for that duration. Otherwise
 *  it specifies a MIDI note:  Middle C (C4)=64,  C#=65,  D=66,  D#=67,
 *  E=68,  F=69,  F#=70,  G=71,  G#=72,  A=73, etc. up to 127 and down to 1.
 *  (Values greater than 127 are interpreted differently: 127 is subtracted,
 *  and it is multiplied by 32 to compute the tone's frequency
 *  in hz.)
 *  There is a maximum of 20 notes.
 */
void beepTones(ArRobot *robot, const std::vector< std::pair<double, unsigned char> >& tune)
{
  char buf[40];
  size_t p = 0;
  for(std::vector< std::pair<double, unsigned char> >::const_iterator i = tune.begin(); i != tune.end() && p < 40;  ++i)
  {
    if((*i).first > 0)
    {
      buf[p++] = (char)((*i).first / 20.0);
      buf[p++] = (char)(*i).second;
    }
  }
  if(p > 0)
    robot->comStrN(ArCommands::SAY, buf, p);
}
Personal tools