question about getting Joystick data and writing it into the file

Typically: "How do I... ", "How can I... " questions
Post Reply
lwang87
Posts: 41
Joined: 24 May 2021, 16:47

question about getting Joystick data and writing it into the file

Post by lwang87 »

Hello!
I've achieved to control my model(a car) by a joystick, but I failed to correctly transfer it's data to a file.
In particular, I firstly defined a function to define my car's moving state:

Code: Select all

function current_moving(m)
	local moving=nil
	if(m==0) then
		moving='wait'
	elseif(m==1) then
		moving='forward'
    	elseif(m==2) then
        	moving='back'
    	elseif(m==3) then
        	moving='turn_L'
	elseif(m=4) then
		moving='turn_R'
	end
	return moving
	end
and next is my controlling code:

Code: Select all

if(axes[2]>0 and -250<rotAxes[3] and rotAxes[3]<250) then    --back
                axes[2]=-axes[2]/10000  
                sim.setJointTargetVelocity(leftJointDynamic,(linearVelocityLeft*axes[2]*v)/(s*wheelRadius))
                sim.setJointTargetVelocity(rightJointDynamic,(linearVelocityRight*axes[2]*v)/(s*wheelRadius))
                m=2  
                elseif(axes[2]<0 and -250<rotAxes[3] and rotAxes[3]<250) then    -- forward
                axes[2]=-axes[2]/10000
                sim.setJointTargetVelocity(leftJointDynamic,(linearVelocityLeft*axes[2]*v)/(s*wheelRadius))
                sim.setJointTargetVelocity(rightJointDynamic,(linearVelocityRight*axes[2]*v)/(s*wheelRadius))
                m=1                
                elseif(0<rotAxes[3]) then   --turn left
                rotAxes[3]=rotAxes[3]/50000
                sim.setJointTargetVelocity(leftJointDynamic,(linearVelocityLeft*rotAxes[3]*v)/(s*wheelRadius))
                sim.setJointTargetVelocity(rightJointDynamic,-(linearVelocityRight*rotAxes[3]*v)/(s*wheelRadius))
                m=3
                elseif(rotAxes[3]<0) then   --turn right
                rotAxes[3]=rotAxes[3]/50000
                sim.setJointTargetVelocity(leftJointDynamic,(linearVelocityLeft*rotAxes[3]*v)/(s*wheelRadius))
                sim.setJointTargetVelocity(rightJointDynamic,-(linearVelocityRight*rotAxes[3]*v)/(s*wheelRadius))            
                m=4
                elseif(axes[2]==0)then
                m=0
            end
and it has written the corrent_moving(m) to the file(.csv). My car will move very slowly(with the initial
nominalLinearVelocity) when I release the joystick(I think it means the axes[2]==0), but it can't record 'wait' to the file, it still wrote 'forward'. and when m=3, it didn't wrote 'turn left' but 'turn right'. I want to know what's wrong with my code. I've tried a lot of way, and I can't solve the problems, please help me! btw... my joystick type is PXN-2113 which can control with only one hand.

Cheers

fferri
Posts: 1217
Joined: 09 Sep 2013, 19:28

Re: question about getting Joystick data and writing it into the file

Post by fferri »

Hi,

your code is not complete (e.g. function current_moving is not called from anywhere).

Post Reply