Help

116 views
Skip to first unread message

rajni

unread,
Jan 13, 2013, 4:59:41 PM1/13/13
to pb...@googlegroups.com
Hi 

I was wondering how to produce a function that allows my NXT to move forward until it senses a obstacle with a touch sensor, then turn as far as necessary to avoid the obstacle, then carry on forward once clear to do so.
I have been through the tutorials but still cannot manage this task. 

Any help would me much appreciated. 

Thanks 

Rajni  

rajni

unread,
Jan 28, 2013, 1:59:59 PM1/28/13
to pb...@googlegroups.com
My full  program should consist of a main function called wander(). This function when called should perform the following actions:
 
1.When the function is called from the interpreter command input, the program should wait until the orange button is pressed. (this gives you the chance to unplug the USB cable and place your vehicle on the demo area).
2.The vehicle should then turn drive forwards until the front touch sensor is activated
3.The vehicle should then turn until it is free to go forward 
4.repeat from 2
5.If any button is pressed at any time during the above operations, the program should end and control return to the pbLua command line.

 

Step 1 is not an issue for me, the problem is from step 2 onwards. I have coding to make the robot go forward and I have coding for the touch sensor so that it can be read in boolean mode. 

Ive added an IF statement to the function so that if the touch sensor reads 1 (which means that its hit an object) then the robot should turn slightly to the left, I added repeat function so you can go back to step 2.

So essentially you would have a robot that drives forward until it hits an object, once this happens it will slightly turn left and continue in this same method until the robot is fully free to drive forward.

Here is my coding so far. ( I don't know if this is correct or not)

Function wander()
 until( 8 == nxt.ButtonRead() )
end

-- Read touch sensor until the orange button is pressed - note that it's a
-- lot easier to simply read the sensor in boolean mode...

function TouchChange(port, thresh)

  nxt.InputSetType(port,0)

  local oldState,newState

  repeat
    if( thresh < nxt.InputGetStatus( port ) ) then
      newState = 0
    else
      newState = 1
    end

    if newState ~= oldState then
      print( nxt.TimerRead(), newState )
      oldState = newState
    end

 
-- And using the function - press the orange button on the NXT to stop it
TouchChange(1,500)


-- Sync Motors B (I) and C (II) - the speed is s and the difference is t

function MotorSync(s,t)

  nxt.OutputResetTacho(2,1,1,1)
  nxt.OutputResetTacho(3,1,1,1)

  nxt.OutputSetRegulation(2,2,1)
  nxt.OutputSetRegulation(3,2,1)

  nxt.DisableNXT( 1 );
  nxt.OutputSetSpeed(2,0x20,s, 0, t )
  nxt.OutputSetSpeed(3,0x20,s, 0, t )
  nxt.DisableNXT( 0 );

  repeat
  until( 8 == nxt.ButtonRead() )

  nxt.OutputSetSpeed(2)
  nxt.OutputSetSpeed(3)
end

repeat
if newState = 0  then
MotorSync(75,0)
else newState = 1
MotorSync(75,20)

When i use this coding all i get is the robot driving forward, the touch does not respond when it hits and object, so I dont know if my touch function is wrong or the IF statement

Thanks

Rajni

 

 

 

 

Reply all
Reply to author
Forward
0 new messages