Programming the PLC
A PLC project must be programmed in order to use TwinCAT Speech. For a quick start, this is explained below on the basis of this sample.
- 1. Create a new PLC project.
- 2. Add the Tc3_Speech library.
- 3. Insert the following code from Sample01 into MainASR.
Declaration part:
PROGRAM MainASR
VAR
// ASR Trigger Variables
bListenTrigger : BOOL := FALSE;
bListenStopTrigger : BOOL := FALSE;
// TTS Command Configuration
nConfigIdTTS : UINT := 200;
// ASR Command Configuration
nConfigIdASR : UINT := 100;
// Language Id for TTS Output
nLanguageId : UINT := 1033;
// fbTTS is required for providing TTS responses upon successful recognitions in this sample.
fbTTS : FB_TextToSpeech := (nConfigurationId := nConfigIdTTS);
fbASR : FB_SpeechRecognition:= (nConfigurationId := nConfigIdASR);
fbRetrieveUtterance : FB_RetrieveUtterance;
// Update Code before Release
// TTS Variables (required for TTS responses upon recognition)
bSpeak : BOOL := FALSE;
{attribute 'TcEncoding':= 'UTF-8'}
sText2Speech : STRING(4096) := '<speak> TcSpeech beta demo project is greeting you </speak>';
// ASR Variables
bListen : BOOL := FALSE;
nLastRecoId : ULINT := 0;
sLastRecognition : STRING(4096) := '';
fConfidenceThreshold : REAL := 0.7; // TcSpeech will inform PLC if recognition confidence is larger than this threshold
bInit : BOOL := FALSE;
Process part:
IF NOT binit THEN
fbTTS.SetAmsNetAddr(GVL_SpeechDemo.sAmsNetId);
fbASR.SetAmsNetAddr(GVL_SpeechDemo.sAmsNetId);
binit := TRUE;
END_IF
// ASR - Automatic Speech Recognition
// Trigger Start/Stop Listening
IF bListenTrigger THEN
bListenTrigger:=FALSE;
bListen := TRUE;
END_IF
IF bSpeak THEN // for answering a recognized ASR command ...
fbTTS(sUtterance := sText2Speech,bSpeak := TRUE, nConfigurationId:= nConfigIdTTS, nLanguageId:=nLanguageId);
IF NOT fbTTS.bBusy THEN
fbTTS(sUtterance := sText2Speech,bSpeak := FALSE, nConfigurationId:= nConfigIdTTS);
bSpeak := FALSE;
END_IF
END_IF
IF bListen THEN
IF bListenStopTrigger THEN
fbASR(bListen := FALSE,nConfigurationId:= nConfigIdASR);
ELSE
fbASR(bListen := TRUE,nConfigurationId:= nConfigIdASR);
END_IF
IF NOT fbASR.bBusy THEN
fbASR(bListen:= FALSE,nConfigurationId:= nConfigIdASR);
blisten := FALSE;
bListenStopTrigger := FALSE;
END_IF
END_IF
// Check if new Recognition is available
IF nLastRecoId <> fbASR.nRecognitionId THEN
IF NOT bSpeak THEN
IF fbAsr.fRecognitionConfidence > fConfidenceThreshold THEN // if recognition better than treshhold, answer via TTS. Next cycle will process via FB_SwitchLanguage
// Check if Recognition Confidence is high enough
nLastRecoId := fbASR.nRecognitionId;
fbRetrieveUtterance(nLanguageId := nLanguageId, sRecognitionTag := fbASR.sRecognitionTag,fRecognitionConfidence := fbAsr.fRecognitionConfidence, sText2Speech => sText2Speech);
bSpeak := TRUE;
END_IF
END_IF
END_IF
END_VAR
- 4. Set "bListen" to TRUE to make a speech input.
The TwinCAT configuration must have been activated beforehand.
The identification number for the ASR configuration to be used is present in the code as ConfigIdASR.