Matlab to PWMPAL Serial Communication
I am trying to send information from a simulink program with an embedded matlab code to my basic stamp which is working with a PWMPAL to control the direction and speed of a winch motor. I can't seem to get the serial communication to work. I download the PBASIC coding and then I try running my simulink/matlab program and nothing happens. The PWMPAL code was adapted from a motor control example presented by Parallax and it works. Help please.
PWMPAL Code
' {$STAMP BS2}
' {$PBASIC 2.5}
'
'***********************************************************************************************************
'
I/O Definitions
PpPin···· PIN······· 0·········· 'PWMPAL Serial I/O
DirCtrl·· PIN······· 2·········· 'Direction I/O
Rx······· PIN······· 14········· 'Serial I/O Pin
'
Constants
MotorNum· CON······· 1
'SELECTED STAMP BS2
T9600···· CON······· 84·········· 'BaudRate 9600
T19K2···· CON······· 32·········· 'BaudRate 19200
T38K4···· CON······· 6··········· 'BaudRate 38400
PpBaud··· CON······· T9600
OUT······ CON······· 0
IN······· CON······· 1
MinSpeed· CON······· 1·········· 'minimum DC to spin Winch Motor
'SERIAL COM
N9600···· CON······· $4054······ 'Baud mode value for 9600 baud, 8,N,1
'
Variables
speed···· VAR······· Word···· 'speed, 0% to 100%
status··· VAR······· Byte···· 'motor control status
direction VAR······· Word···· 'Motor Direction
onTime··· VAR······· Word···· 'PWM timing
offTime·· VAR······· Word
'
Receive Serial Info
DO
Receive:
· SERIN Rx,N9600, [noparse][[/noparse]speed.BYTE1, speed.BYTE0, direction.BYTE1, direction.BYTE0]
'
Program Code
Main:
··· IF (direction = OUT) THEN
····· HIGH DirCtrl
··· ELSE
····· LOW DirCtrl
··· ENDIF
··· 'IF (speed < 100) THEN
····· 'speed = speed + MinSpeed
····· 'PAUSE 100
··· IF (speed < 5) THEN
····· speed = 0
··· ENDIF
··· IF (speed > 100) THEN
····· speed = 100
··· ENDIF
··· GOSUB Speed_Ctrl
··· DEBUG CR, "Speed = ", DEC speed, CLREOL
··· DEBUG HOME, "Direction: ", direction, CLREOL
LOOP
'
Subroutines
Speed_Ctrl:
· onTime = speed
· offTime = 100 - speed···· 'Set duty cycle
· SEROUT PpPin, PpBaud, [noparse][[/noparse]"!PWMM", (48 + MotorNum), onTime.BYTE0, onTime.BYTE1, offTime.BYTE0, offTime.BYTE1]
· 'status.HIGHNIB = %0001 << (MotorNum - 1)········ 'Set enable bit
· 'status.LOWNIB = %0001 << (MotorNum - 1)········· 'Set control bit
· 'SEROUT PpPin, PpBaud, [noparse][[/noparse]"!PWMSS", status]
· RETURN
· '**********************************************************************************************************
Matlab Code
velocity=test;
if (velocity < 0.0);
vel_dir = 1;
else
vel_dir = 0;
end
MAX_V = 0.2;
vel_perc = abs(velocity)/MAX_V;
s = serial('COM1');
set(s, 'BaudRate', 9600);
fopen(s);
fprintf(s, '%d\n', vel_dir);
fprintf(s, '%f\n', vel_perc);
fclose(s);
·
PWMPAL Code
' {$STAMP BS2}
' {$PBASIC 2.5}
'
'***********************************************************************************************************
'
I/O Definitions
PpPin···· PIN······· 0·········· 'PWMPAL Serial I/O
DirCtrl·· PIN······· 2·········· 'Direction I/O
Rx······· PIN······· 14········· 'Serial I/O Pin
'
Constants
MotorNum· CON······· 1
'SELECTED STAMP BS2
T9600···· CON······· 84·········· 'BaudRate 9600
T19K2···· CON······· 32·········· 'BaudRate 19200
T38K4···· CON······· 6··········· 'BaudRate 38400
PpBaud··· CON······· T9600
OUT······ CON······· 0
IN······· CON······· 1
MinSpeed· CON······· 1·········· 'minimum DC to spin Winch Motor
'SERIAL COM
N9600···· CON······· $4054······ 'Baud mode value for 9600 baud, 8,N,1
'
Variables
speed···· VAR······· Word···· 'speed, 0% to 100%
status··· VAR······· Byte···· 'motor control status
direction VAR······· Word···· 'Motor Direction
onTime··· VAR······· Word···· 'PWM timing
offTime·· VAR······· Word
'
Receive Serial Info
DO
Receive:
· SERIN Rx,N9600, [noparse][[/noparse]speed.BYTE1, speed.BYTE0, direction.BYTE1, direction.BYTE0]
'
Program Code
Main:
··· IF (direction = OUT) THEN
····· HIGH DirCtrl
··· ELSE
····· LOW DirCtrl
··· ENDIF
··· 'IF (speed < 100) THEN
····· 'speed = speed + MinSpeed
····· 'PAUSE 100
··· IF (speed < 5) THEN
····· speed = 0
··· ENDIF
··· IF (speed > 100) THEN
····· speed = 100
··· ENDIF
··· GOSUB Speed_Ctrl
··· DEBUG CR, "Speed = ", DEC speed, CLREOL
··· DEBUG HOME, "Direction: ", direction, CLREOL
LOOP
'
Subroutines
Speed_Ctrl:
· onTime = speed
· offTime = 100 - speed···· 'Set duty cycle
· SEROUT PpPin, PpBaud, [noparse][[/noparse]"!PWMM", (48 + MotorNum), onTime.BYTE0, onTime.BYTE1, offTime.BYTE0, offTime.BYTE1]
· 'status.HIGHNIB = %0001 << (MotorNum - 1)········ 'Set enable bit
· 'status.LOWNIB = %0001 << (MotorNum - 1)········· 'Set control bit
· 'SEROUT PpPin, PpBaud, [noparse][[/noparse]"!PWMSS", status]
· RETURN
· '**********************************************************************************************************
Matlab Code
velocity=test;
if (velocity < 0.0);
vel_dir = 1;
else
vel_dir = 0;
end
MAX_V = 0.2;
vel_perc = abs(velocity)/MAX_V;
s = serial('COM1');
set(s, 'BaudRate', 9600);
fopen(s);
fprintf(s, '%d\n', vel_dir);
fprintf(s, '%f\n', vel_perc);
fclose(s);
·
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen