Question

I am trying to write some embedded firmware for a robot that will move towards a light source at a speed proportional to the light intensity, so that as the robot approaches the light source it slows down at a linear rate.

It has a light sensor on the front, which writes the current light intensity to the variable current_light_intensity, in the range of 1 (low light intensity, far away) to 4095 (high light intensity, close). I am looking for an algorithm that will convert this value to a variable motor_speed, in the range of integers 1 to 8, which are used to control the motor speed (with 1 being the slowest and 8 being the fastest).

Crucially, I want the robot to slow down linearly with distance (i.e., imagine that light intensity is lowest at 8 meters from the light source, I want the speed to decrease by one for every meter). At the moment I have the light source directly proportional to the speed. However, light increases inverse-proportionally to the square of the distance, which means currently my robot moves at full speed towards the light for most of the distance then slows down quite abruptly when close, as the intensity shoots up, which for me is a problem. Any algorithm suggestions would be greatly appreciated. Language is C.

Était-ce utile?

La solution

If current_light_intensity really is proportional to current light intensity, then you can do something like this:

distance = 1.0/sqrt(current_light_intensity); /* range is 1/sqrt(4095) to 1 */
distance = (distance-1.0/sqrt(4095.0))/(1.0 - 1.0/sqrt(4095.0)); /* scaled to [0,1] */
motor_speed = ceil(8.0 * distance);

If, as is often the case, there are a lot of non-linearities in the system (e.g. photosensor saturation), you may have to adjust the speed(intensity) function by hand, which isn't very difficult. Just construct an array of threshold intensities, for use as a lookup table. Then do some experiments with a prototype, and adjust the values in the array until the behavior looks right.

Autres conseils

You want

Velocity = Distance  V = D
Intencity I = C / D^2  where C is some constant

D = Sqrt(C/I)
for D = 8 I = 1 => C=64

so

V=D=Sqrt(64 / I)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top