Parallx & matalab
My project consist in connect the Ping sensor, and·measure the distance of a ball, then i· activate a motor according to the distance that the·ping sensor is giving me. However, I achived to communicate the·motor with matlab, but i cannot achived the communication of the Ping sensor with matlab. ·Does anyone can help me? with matlab and BS2.
I already check one papper of a guy who actually·did, but·i am confuse·
I already check one papper of a guy who actually·did, but·i am confuse·
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
here is my call back puss botton function...
a=handles.edit4
setpoint=a;
ser_obj=serial('COM4','baudrate',9600);
ser_obj.terminator ='CR';
ser_obj.ReadAsyncMode = 'manual';
fopen(ser_obj);
fprintf(ser_obj,'%d\n',[noparse][[/noparse]setpoint],'async'); % send duration of pulse width used to control the servomotor
pause(0.5);
i=0
while i<=50
distance=fscanf(ser_obj,'%d\n');
y=distance
set(handles.text6,'string',distance) %
i=i+1
end
fclose(ser_obj);
Here is my basic stamp code for my project..
' {$STAMP BS2}
' {$PBASIC 2.5}
'
[noparse][[/noparse] Declarations ]
FAN·················· PIN 2············· ' Fan Driver I/O
PINGSENSOR··········· PIN 10
'
[noparse][[/noparse]Constants]
kp··················· CON··· 200······················ 'proportional constant
ki··················· CON··· 1······················· 'Integral constant
kd··················· CON··· 0····················· 'derivative constant
Current·············· CON··· 0···················· ' Array index - current error
Accumulator·········· CON···· 1···················· ' Array index - accumulated error
Previous············· CON···· 2···················· ' Array index - previous error
Delta················ CON···· 3···················· ' Array index - change in error
signbit··· VAR Bit········ 'hold the sign of a calculation
time······ VAR Word······· ' Var that holds the time of the sensor
distance·· VAR Word······· ' var that holds the distance of the ball
temp······ VAR Word
duty······ VAR Word······· '
setpoint·· VAR Word······· 'Var that holds the set point
error····· VAR Word(4)···· 'Calculates the 4 type of errors
p········· VAR Word······· 'Proportional term
i········· VAR Word······· 'Integral term
d········· VAR Word······· 'Derivative term
'
[noparse][[/noparse]Intialization ]
E1:
····· SERIN 16,84, [noparse][[/noparse]DEC setpoint]
····· IF setpoint > 138 OR setpoint=0 THEN E1···· 'setpoint cannot be more than the limit or 0, debuggin error
····· error(Previous)=0
'
[noparse][[/noparse] Main Routine ]
DO
······ GOSUB sensor
······ GOSUB Proportional
······ GOSUB Integral
······ GOSUB Derivative
······ error(Previous)=error(Current)
······ IF distance>=130 THEN
······ duty=190
······ ENDIF
······ GOSUB motor
LOOP
END
'
[noparse][[/noparse] Subroutine ]
motor:
····· PWM FAN,duty,255
····· RETURN
sensor:
····· PULSOUT PINGSENSOR,5
····· PULSIN· PINGSENSOR, 1, time
····· distance= time ** 2251········ 'transform distance in cm by the constant 2251 -> due to
····· distance= 139 - distance····· 'for the ping pong ball in rest hight=0,
····· SEROUT 16,84,[noparse][[/noparse]DEC distance,CR]
····· PAUSE 100
····· RETURN
Proportional:
····· 'Calculate error
······ error(Current)=setpoint - distance
······ temp=error(Current)
······ signbit=temp.BIT15
······ 'Calculate proportional term
······ p=kp*error(Current)
······ p=ABS p/255
······ 'Calculate output
······ IF signbit=1 THEN
········ duty=200-p MIN 170
······ ELSE
········ duty=200+p
········ IF duty>=255 THEN
········ duty=200+p MAX 200
········ ENDIF
······ ENDIF
······ RETURN
Integral:
······· 'Calculate integral of the error
······· error(Accumulator)=error(Accumulator)+ error(Current)
······· temp=error(Accumulator)
······· signbit=temp.BIT15
······· 'Calculate integral term
······· i=ki*error(Accumulator)
······· i=ABS i /255
······· 'Calculate output
······· IF signbit=1 THEN
·········· duty=duty-i· MIN 170
······· ELSE
·········· duty=duty+i· MAX· 200
······· ENDIF
end2:··· RETURN
Derivative:
······· 'Calculate derivative of the error
······· error(Delta)=error(Current)- error(Previous)
······· temp=error(delta)
······· signbit= temp.BIT15
······· 'Calculate derivative term
······· d=kd*error(Delta)
······· d=ABS d / 255
······· 'Calculate OUTPUT
······· temp=duty
······· IF signbit=1 THEN
·········· duty=duty-d
······· ELSE
······· duty=duty+d
········· IF duty>=255 THEN
········· duty=duty-d
········· ENDIF
······· ENDIF
end3:······ RETURN
Thanks Stephen for your time..
Atte.
Milton I Quinteros