VRbot Voice Recognition Module

Post here if you want to announce new wiki modules, projects or articles

Moderators: David Barker, Jerry Messina

Post Reply
User avatar
RangerBob
Posts: 152
Joined: Thu May 31, 2007 8:52 am
Location: Beds, UK

VRbot Voice Recognition Module

Post by RangerBob » Tue May 18, 2010 9:46 am

Hello All,

I've just added a page to the modules wiki about controlling the VRbot Voice Recognition Module.

I've had a problem trying to list the code on the page with the wiki returning a "406 - Not Acceptable" error every time I try to add the listing. I think that it seems the Wiki has its Apache mod_security tripped by the listing but I have no idea what is tripping it. Any help?

For the moment, I have the files available as a download on the page and am duplicating the listing here on the forum.

Example Program

Code: Select all

{
*****************************************************************************
*  Name    : VRBotTest.BAS                                                  *
*  Author  : Nathan Herbert                                                 *
*  Notice  : Copyright (c) 2010 								            *
*          : All Rights Reserved                                            *
*  Date    : 17/05/2010                                                     *
*  Version : 1.0                                                            *
*  Notes   :                                                                *
*          :                                                                *
*****************************************************************************
}

// device and clock...                                                                                                                                                                                                                                         
Device = 18F4550
Clock = 48

//Config set for USB Bootloader setup
Config
   PLLDIV = 5,				    // Oscillator Select - Div by 5 (20MHz Input)
   CPUDIV = OSC1_PLL2,          // System Postscaler
   USBDIV = 2,                  // USB Clock - PLL/2
   FOSC = HSPLL_HS,             // High Speed PLL ON
   VREGEN = On,              	// USB Voltage Regulator ON
   WRTB = On                   	// Bootloader Block Write Protect                

// Bootloader Settings - Use $1000 with Microchip HID bootloader or $0800 with PDFSUSB Bootloader
#option org_reset = $0800

#option RX_PRIORITY = iplow
#option USB_SERVICE_PRIORITY = ipHigh

// Need to keep these off for low speed USART - 9600
#option USART_BRGH = false
#option USART_BRGH16 = false

'Include "ClearRegisters.bas"
Include "USBCDC.bas"
Include "USART.bas"
Include "ISRRX.bas"
Include "VRbot.bas"
Include "Convert.bas"

Dim Button1 As PORTB.4
Dim LED0 As PORTD.0
Dim LED1 As PORTD.1

Const 
	CR = 13,
	LF = 10,
	TAB = 9
{
********************************************************************************
* Name    : Startup					                                           *
* Purpose :  							                                       *
********************************************************************************
}                 
Function VRbotSetup() As Boolean
	
	// Set VRbot Options
	If VRbot.Detect() Then
		// Set Language to English
		If VRbot.SetLanguage(0) Then
			result = true
		Else
			result = false
		EndIf
	Else 
		result = false
	EndIf
	
End Function
{
********************************************************************************
* Name    : Startup					                                           *
* Purpose :  							                                       *
********************************************************************************
}                 
Sub Startup()
	
	// Setup VRbot Serial Ports
	USART.SetBaudrate(br9600)
	ISRRX.Initialize()
	
	// Set ports
	Input(Button1)

	Output(LED0)
	Output(LED1)
		
	DelayMS(5000)
	
	// Initialise VRbot
	Repeat
	CDC.Write("> Starting VRbot ...", CR, LF)
	DelayMS(1000)
	Until VRbotSetup() = true
	
	CDC.Write("> VRBot Test Code Ready ...", CR, LF)
	CDC.Write("> Press 'p' to enter Passthrough Mode for setup using VRbotGUI", CR, LF)
	CDC.Write("> or try speaking a command now...", CR, LF)
		
End Sub

{
********************************************************************************
* Name    : PassThrough				                                           *
* Purpose : Passthrough mode for setting up VRBot - Reset or press RB4 button to exit*
********************************************************************************
}                 
Sub PassThrough()

   		CDC.Write("> Passthrough Mode Activated - Press S2 to Exit", CR, LF)
   		CDC.Write("> Disconnect this terminal and start VRbot GUI", CR, LF)

		VRbot.Detect()			// Send break to stop module 

		While Button1 = 1
			While CDC.DataAvailable
				USART.Write(CDC.ReadByte)
			Wend
			
			While ISRRX.DataAvailable
				CDC.Write(ISRRX.ReadByte)
			Wend
		Wend
		
		CDC.Write("> Exit Passthrough Mode", CR, LF)

End Sub

{
********************************************************************************
* Name    : Main					                                           *
* Purpose : Main program loop			                                       *
********************************************************************************
}                 
Sub Main()
	Dim TestChar As Char
	Dim Res As Byte
	Dim Retrigger As Boolean
	Dim WordSet As Byte
	
	Retrigger = true
	Wordset = 0

	High(LED0)
	
	// Main Loop
	While True 
		
		// Check for USBCDC commands
		While CDC.DataAvailable
			TestChar = CDC.ReadByte
			If TestChar = "p" Then
				PassThrough()
			EndIf
			Retrigger = true
			WordSet = 0
		Wend

		// Start SI recognition if necessary
		If Retrigger = true Then
			VRbot.Detect() 	// send break
			VRbot.RecogniseSI(WordSet)	// Set for necessary
			Retrigger = False
		EndIf
		
		// Check for Result & Do Something
		Res = VRbot.CheckResult()
		
		// Valid Return
		If Res < Invalid Then
			
			// Decide what to do			
			Select Wordset
				
				Case 0	// Trigger word
				// Got attention, go onto next wordset
				WordSet = 1	
				CDC.Write("> ")
				High(LED1)

				Case 1	// Action List
					Select Res
						Case wAction
							WordSet = 0					
							CDC.Write("Do Action! ", CR, LF)
						Case wMove
							WordSet = 2
							CDC.Write("Move ")
						Case wTurn
							WordSet = 2	
							CDC.Write("Turn ")
						Case wRun
							WordSet = 0
							CDC.Write("Run!", CR, LF)
						Case wLook
							WordSet = 2
							CDC.Write("Look ", CR, LF)
						Case wAttack
							WordSet = 0
							CDC.Write("Banzai! ", CR, LF)
						Case wStop
							WordSet = 0
							CDC.Write("Stop! ", CR, LF)
						Case wHello	
							WordSet = 0
							CDC.Write("Well Hello there! ", CR, LF)
				     End Select
				
				Case 2	// Direction List
				   	Select Res
                		Case wLeft
							CDC.Write("left ")
							Wordset = 3
                 		Case wRight
							CDC.Write("right ")
							Wordset = 3
                 		Case wUp
							CDC.Write("up ")
							Wordset = 3
                		Case wDown
							CDC.Write("down ")
							Wordset = 3
                		Case wForward
							CDC.Write("forward ")
							Wordset = 3
                		Case wBackward
							CDC.Write("backward ")
							Wordset = 3
               		End Select
				Case 3	// Number List
					
					CDC.Write(DecToStr(Res), CR, LF)
					Wordset = 0
					
			End Select
			
			// Ensure we reset to correct wordset
			retrigger = true
			
		EndIf	
		
		// Detection Timeout has occured - reset to wait for trigger word
		If Res = Invalid Then
			Wordset = 0
			Retrigger = true
			CDC.Write(CR,LF)
		EndIf

		// Show whether in trigger mode or not
		If WordSet = 0 Then 
			Low(LED1)
		Else
			High(LED1)
		EndIf
		
	Wend
End Sub
           
	Startup
	
	Main()
Module

Code: Select all

{
*****************************************************************************
*  Name    : VRbot.BAS                                                      *
*  Author  : Nathan Herbert                                                 *
*  Notice  : Copyright (c) 2010 								            *
*          : All Rights Reserved                                            *
*  Date    : 14/05/2010                                                     *
*  Version : 1.0                                                            *
*  Notes   :                                                                *
*          :                                                                *
*****************************************************************************
}

Module VRbot

Include "USART.bas"
Include "ISRRX.bas"

Public Const 
	CMD_BREAK       = "b", // abort recog or ping
	CMD_SLEEP       = "s", // go to power down
 	CMD_KNOB        = "k", // set si knob <1>
 	CMD_LEVEL       = "v", // set sd level <1>
 	CMD_LANGUAGE    = "l", // set si language <1>
	CMD_TIMEOUT     = "o", // set timeout <1>
	CMD_RECOG_SI    = "i", // do si recog from ws <1>
	CMD_TRAIN_SD    = "t", // train sd command at group <1> pos <2>
	CMD_GROUP_SD    = "g", // insert new command at group <1> pos <2>
	CMD_UNGROUP_SD  = "u", // remove command at group <1> pos <2>
	CMD_RECOG_SD    = "d", // do sd recog at group <1> (0 = trigger mixed si/sd)
	CMD_ERASE_SD    = "e", // reset command at group <1> pos <2>
	CMD_NAME_SD     = "n", // label command at group <1> pos <2> with length <3> name <4-n>
	CMD_COUNT_SD    = "c", // get command count for group <1>
	CMD_DUMP_SD     = "p", // read command data at group <1> pos <2>
	CMD_MASK_SD     = "m", // get active group mask
	CMD_RESETALL    = "r", // reset all commands and groups
	CMD_ID          = "x", // get version id
	CMD_DELAY       = "y", // set transmit delay <1> (log scale)
	CMD_BAUDRATE    = "a", // set baudrate <1> (bit time, 1=>115200)
	                
	STS_MASK        = "k", // mask of active groups <1-8>
	STS_COUNT       = "c", // count of commands <1>
	STS_AWAKEN      = "w", // back from power down mode
	STS_DATA        = "d", // provide command length <1> label <2-11> training <12>
	STS_ERROR       = "e", // signal error code <1-2>
	STS_INVALID     = "v", // invalid command or argument
	STS_TIMEOUT     = "t", // timeout expired
	STS_INTERR      = "i", // back from aborted recognition (see "break")
	STS_SUCCESS     = "o", // no errors status
	STS_RESULT      = "r", // recognised sd command <1> - training similar to sd <1>
	STS_SIMILAR     = "s", // recognised si <1> (in mixed si/sd) - training similar to si <1>
	STS_OUT_OF_MEM  = "m", // no more available commands (see "group")
	STS_ID          = "x", // provide version id <1>
	
	// protocol arguments are in the range 0x40 (-1) to 0x60 (+31) inclusive
	ARG_MIN     	= $40,
	ARG_MAX     	= $60,
	ARG_ZERO    	= $41,

	ARG_ACK     	= $20    // to read more status arguments

// Built in SI wordlists
Public Const	
	// Wordset 0
	wRobot			= 0,
	
	// Wordset 1
	wAction			= 0,
	wMove			= 1,
	wTurn			= 2,
	wRun			= 3,
	wLook			= 4,
	wAttack			= 5,
	wStop			= 6,
	wHello 			= 7,

	// Wordset 2
	wLeft			= 0,
	wRight			= 1,
	wUp				= 2,
	wDown			= 3,
	wForward		= 4,
	wBackward	 	= 5
	
	// Wordset 3
	'Numbers 0 to 10

Public Const
	NoData			= 255,
	Invalid			= 254
	
{
********************************************************************************
* Name    : Detect					                                           *
* Purpose :  							                                       *
********************************************************************************
} 
Public Function Detect() As Boolean
	Dim i As Byte
	Dim pChar As Char
    result = false
	i = 0
	
	Repeat
		// Send data
		USART.Write(CMD_BREAK)
		DelayMS(100)
		// Check for ack		
 		If ISRRX.DataAvailable Then
		 	 pChar = ISRRX.ReadByte()
			 If pChar = STS_SUCCESS Then
 				result = true
			EndIf
		EndIf
		Inc(i)
	Until i = 5 Or result = true 

End Function

{
********************************************************************************
* Name    : SetLanguage				                                           *
* Purpose :  							                                       *
********************************************************************************
} 
Public Function SetLanguage(pLang As Byte) As Boolean
	Dim pChar As Char
	result = false

	USART.Write(CMD_LANGUAGE)
	DelayMS(5)
	USART.Write(ARG_ZERO + pLang)
	DelayMS(100)

	If ISRRX.DataAvailable Then
		pChar = ISRRX.ReadByte
		If pChar = STS_SUCCESS Then
			result = true
		EndIf
	EndIf

End Function

{
********************************************************************************
* Name    : RecogniseSD				                                           *
* Purpose :  							                                       *
********************************************************************************
} 
Public Sub RecogniseSD(pGroup As Byte)
	
	USART.Write(CMD_RECOG_SD)
	DelayMS(5)
	USART.Write(ARG_ZERO + pGroup)

End Sub

{
********************************************************************************
* Name    : RecogniseSI				                                           *
* Purpose :  							                                       *
********************************************************************************
} 
Public Sub RecogniseSI(pGroup As Byte)
	
	USART.Write(CMD_RECOG_SI)
	DelayMS(5)
	USART.Write(ARG_ZERO + pGroup)

End Sub

{
********************************************************************************
* Name    : CheckResult				                                           *
* Purpose : Returns STS_TIMEOUT(255) if no results                             *
********************************************************************************
} 
Public Function CheckResult() As Byte
	Dim RX As Char
	Dim i As Byte
	result = NoData 
	i = 0
	
	If ISRRX.DataAvailable Then
		RX = ISRRX.ReadByte()
		If RX = STS_SIMILAR Or rx = STS_RESULT Then
			DelayMS(5)
			USART.Write(ARG_ACK)
			While Not ISRRX.DataAvailable And i < 20
				DelayMS(1)
				Inc(i)
			Wend
			result = ISRRX.ReadByte() - ARG_ZERO
		Else
			result = Invalid	// Not recognised or timeout
		EndIf
	Else
		result = NoData // No Serial Data
	EndIf

End Function

{
********************************************************************************
* Name    : GetGroupCount			                                           *
* Purpose : Returns 0 if no results		                                       *
********************************************************************************
} 
Public Function GetGroupCount(pGroup As Byte) As Byte
	Dim RX As Char
	Dim i As Byte
    result = 0
	i = 0

	USART.Write(CMD_COUNT_SD)
	DelayMS(5)
	USART.Write(ARG_ZERO + pGroup)
	DelayMS(100)
	
	If ISRRX.DataAvailable Then
		RX = ISRRX.ReadByte()
		If RX = STS_COUNT Then
			DelayMS(5)
			USART.Write(ARG_ACK)
			While Not ISRRX.DataAvailable And i < 20
				DelayMS(1)
				Inc(i)
			Wend
			result = ISRRX.ReadByte() - ARG_ZERO
		EndIf
	EndIf

End Function

Post Reply