Motor control with light sensor

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
kieran-pierce
Posts: 10
Joined: 02 Nov 2012, 22:14

Motor control with light sensor

Post by kieran-pierce »

Hi again,

I would like to know your thoughts on the best way to control a motor with a a light sensor. What I am trying to do is run a motor at full power and when an event occurs a light sensor will read a hight value, at this point I want to stop the motor dead. I have tried a few techniques, one being to run the motor and monitor the sensor in a loop then switch it off, the issue with this is I always overrun as the Off command does not stop the motor straight away. I have tried a smiler approach using 2 tasks one watching the sensor and telling the other when it's time to stop. I have also tried moving the motor a little then checking the state of the sensor but this is very slow and jerky. I have tried applying a reverse when see the point to stop the movement but this is hard to calculate and often overruns then back up I really need to stop the movement dead.

so any ideas?
hassenplug
Posts: 346
Joined: 27 Sep 2010, 03:05
Contact:

Re: Motor control with light sensor

Post by hassenplug »

Not sure what language you're using, but here's what I've done (thanks to John) in NXC

Code: Select all

inline void HoldAtPosition(byte motor)
{
	SetOutput(motor,
		Power, 0,
		OutputMode, OUT_MODE_MOTORON+OUT_MODE_BRAKE+OUT_MODE_REGULATED,
		RegMode, OUT_REGMODE_SPEED,
		RunState, OUT_RUNSTATE_RUNNING,
		UpdateFlags, UF_UPDATE_SPEED+UF_UPDATE_MODE+UF_UPDATE_PID_VALUES);

}
And, this line to call the function:
HoldAtPosition(OUT_B);

Is that what you want?

Steve
---> Link to lots of MINDSTORMS stuff under my picture --->
kieran-pierce
Posts: 10
Joined: 02 Nov 2012, 22:14

Re: Motor control with light sensor

Post by kieran-pierce »

Thanks Steve, I had a block of code very smiler to that, some times the rotation continues beyond where I would like it to stop and you can see that this is trying to pull it back which it does do. I think maybe the issue is the way i am detecting the event i guess its too slow, so I have this now,

Code: Select all

lightLast = 0;
     SetSensorLight(IN_1);
     lightNow = SENSOR_1;

      OnFwd(OUT_A,100);
      
      until(SENSOR_1 > 55);

   SetOutput(OUT_A,
      Power, 0,
      OutputMode, OUT_MODE_MOTORON+OUT_MODE_BRAKE+OUT_MODE_REGULATED,
      RegMode, OUT_REGMODE_SPEED,
      RunState, OUT_RUNSTATE_RUNNING,
      UpdateFlags, UF_UPDATE_SPEED+UF_UPDATE_MODE+UF_UPDATE_PID_VALUES);
      
      }
I wonder if I should check the status of the light sensor is some other way?
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 0 guests