Continue to Site

# Autonomous cruise control

Status
Not open for further replies.

#### ans1991

##### Newbie level 4
hello, am doing a graduation project based on measuring the distance of a prototype from objects and controlling its speed ( DC motor).
i have an idea of how to measure the distance using ultrasonic sensor and 16 pic, but am having a problem in how to control the dc motor speed. Any ideas?!

Do you mean you are having a problem designing the electronics to drive the motor, or the function that would control the motor? If I understand what you're trying to do, for something like 'cruise control' you could use your typical PID algorithm using distance measured by the ultrasonic sensor as the error value. You'd simply have the PID sum used to vary the duty cycle of PWM to mosfet gates in an H-bridge, or 3 phase bridge for a BLDC motor etc.

yeah this is what I am looking for to control it using PID but can it done by PIC without the using of a computer (for example matlab)

Yes- the PID code is easy to implement on something like a PIC- its a very simple algorithm.

Effectively you will be writing something like:

output = kp*(distance error) + (ki*(distance error + previous distance error)) + kd*(distance error/sampling time)

You will need to scale and filter the output sum according to your specific application, and tune the kp, ki, kd constants. Matlab will find suitable constants if you can derive a transfer function for the system.

ans1991

### ans1991

Points: 2
thank you for the information. Although i didn't get the one thing, when deriving the transfer function constant should i add the controller before or after the derivation?

- - - Updated - - -

and i think the equation should be:

output = kp*(distance error) + (ki*(integral +distance error*dt)) + kd*((error - previous distance error)/dt)

where integral = integral + error*dt
am I right?

You're correct- I wasn't thinking enough when I wrote that. the D term should be kd*the difference between 2 successive errors over the sampling period.

as for
(ki*(integral +distance error*dt))

If you mean integral as in (sum of previous errors * sampling time) rather than the whole integral 'I' term, then you're right.

so, output = kp*(distance error) + ki*(distance error + (sum previous distance errors))*sampling time + kd*((distance error - previous error)/sampling time)

>when deriving the transfer function constant should i add the controller before or after the derivation?

The transfer function for the motor will be a 2nd order function. It will "transfer" the voltage input to a velocity output. You don't need to change the PID function to simulate the motor velocity, but you do need to add a function which takes the PID sum as an input, and produces a voltage output (this is based on your hardware)- this is where you need to define how a number calculated on the MCU ends up driving the motor. If I remember correctly I think the MATLAB website actually has good examples for motor transfer functions.

ans1991

### ans1991

Points: 2
yeah that is what i mean when say integral.
I will have a look in the examples.

thank you very much h@ro for the good information.

Status
Not open for further replies.