# Fundamental electrical, mechanical and control principles

## Example questions

These example questions touch on the following topics:

• Basic circuit theory
• Gears, velocity, torque
• Forward and inverse kinematics
• Binary numbers
• Analog-to-digital conversion
Question:

A DC motor drives the main axle of a robot vehicle via a belt drive. The belt sits on two pulleys, one connected to the rotor and the other connected the axle. The rotor pulley has a diameter of 1 cm. The axle pulley has a diameter of 20cm. The vehicle’s wheels which are connected directly to the axle are 30cm in diameter.

• What is the gear ratio of the belt drive system?
• If the motor is running at 300 rpm, what speed will the vehicle be moving at?

The gear ratio of the belt drive is just the ratio of the two pulley diameters, i.e.

$\mbox{gear\_ratio} = \frac{\mbox{rotor\_pulley\_diameter}}{\mbox{axle\_pulley\_diameter}} = 1:20$

• So the axle rotates 20 times slower than the rotor, which is rotating at 300 rpm.
• Therefore, the speed of rotation of the axle is 300/20 = 15 rpm
• The wheels are fixed on the axle, so their speed of rotation is also 15 rpm.
• The distance travelled in 1 minute, $d_{min}$, is therefore equal to 15 times the circumference of the wheel.

$d_{min} = 15 \times \pi \times 0.3 = 14.1372 \mbox{ m}$

The speed of the vehicle in metres per second is therefore:

$\mbox{vehicle\_speed} = \frac{d_{min}}{60} = \frac{14.1372}{60} = 0.2356 \mbox{ ms}^{-1}$

Question:

The main drive axle of a small robot vehicle is driven by a stepper motor via a gear box with a gear ratio of 10:1 (the axle rotates once for every ten revolutions of the stepper motor). Two wheels of radius 7.5cm are connected to the axle. The stepper motor moves in increments of 1.8 degrees at a maximum stepping speed of 500 steps per second.

• What is the smallest distance (greater than zero) that the robot can move?
• What is the maximum speed of the robot?

Begin by finding out what happens when the stepper motor moves one step.

$\mbox{rotor\_step\_angle} = 1.8^{\circ}$

The gear ratio is 1:10. Therefore,

$\mbox{axle\_step\_angle} = \frac{1.8^{\circ}}{10} = 0.18^{\circ}$

So each time the motor steps, the axle rotates 0.18°. The wheel circumference is:

$\mbox{wheel\_circumference} = 2 \times \pi \times 0.075 = 0.4712 \mbox{ m}$

If the wheels moved through one full revolution, the distance moved would be equal to the circumference of the wheels. Therefore the distance moved per degree of wheel rotation is

$d_{degree} = \frac{0.4712}{360} = 0.00131 \mbox{ m}$

The distance moved per step is therefore

$d_{step} = \mbox{axle\_step\_angle} \times d_{degree} = 0.18 \times 0.00131 = 2.3562 \times 10^{-4} \mbox{ m}$

The distance moved per step is the smallest distance greater than zero that the robot can move. The maximum stepping speed is 500 steps per second. Therefore, the robot’s top speed is

$\mbox{max\_speed} = 500 \times 2.3562 \times 10^{-4} = 0.1178 \mbox{ ms}^{-1}$

Question:

The diagram below represents a crane viewed from overhead. The arm of the crane is 10m in length. The angular displacement of the arm of the crane is S. The crane’s lifting hook is suspended from a pulley block that slides back and forth along the boom. The distance of the pulley block from the crane’s mast (i.e. from the start of the arm) is L. The base of the crane is located at the origin and the ground around the crane is mapped out using cartesian (x-y) coordinates.

• Explain the difference between forward and inverse kinematics.
• Write the forward and inverse kinematics equations for the crane, relating x, y, S and L.
• What values of S and L are required to position the end effector at the point x=5m, y=2m.

Forward and inverse kinematic equations

The forward kinematic equations for a robot arm provide expressions for the Cartesian coordinates of the end effector in terms of the joint angles (for revolute joints) and lengths (for prismatic joints). The inverse kinematic equations for a robot arm provide expressions for each of the joint angles and lengths in terms of the desired Cartesian coordinates of the end effector.

Forward kinematic equations of the crane:

• $x = L \cos(S)$
• $y = L \sin(S)$

Inverse kinematic equations of the crane:

• $L = \sqrt{x^2 + y^2}$
• $S = \cos^{-1}\frac{x}{L}$

I have made the assumption here that x and y are positive. When that assumption is not valid, S can be identified unambiguously by checking the signs of x and y.

Now, to position the end effector at x=5, y=2:

$L = \sqrt{5^2 + 2^2} = 5.385\mbox{ m}$

$S = \cos^{-1}\left(\frac{5}{5.385}\right) = 21.8^{\circ}$

Quickly double check the results:

$5.385 \times \sin(21.8) = 2$

$5.385 \times \cos(21.8) = 5$

So the values for S and L are correct.

Question:

The two segments of a SCARA robot have length L1=40cm (shoulder to elbow) and L2=30cm (elbow to wrist) respectively. The shoulder angle is S and the elbow angle is E. The robot arm is oriented so that the end effector always moves in the horizontal x-y plane.

• Sketch the robot, indicating the joint types, and labeling L1, L2, S and E. Show the x and y axes.
• Write the forward and inverse kinematic equations for the SCARA arm.
• Calculate values of S and E that position the end effector at the point x=50cm, y=46cm.

The SCARA arm is shown below.

SCARA forward kinematic equations

• $x = L_1 \cos(S) + L_2 \cos(S+E)$
• $y = L_1 \sin(S) + L_2 \sin(S+E)$

SCARA inverse kinematic equations

First, we obtain an expression for the elbow angle by applying the cos rule in the triangle (0,0)(x’,y’)(x,y).

$E = \cos^{-1} \left( \frac{x^2+y^2-L_1^2-L_2^2}{2L_1L_2} \right)$

The angle $(S+Q) = \tan^{-1}\left( \frac{y}{x}\right)$

The angle $Q = \cos^{-1}\left(\frac{x^2+y^2+L_1^2-L_2^2}{2L_1\sqrt{x^2+y^2}}\right)$

So, the shoulder angle $S=\tan^{-1}\left( \frac{y}{x}\right)-\cos^{-1}\left(\frac{x^2+y^2+L_1^2-L_2^2}{2L_1\sqrt{x^2+y^2}}\right)$

Applying the inverse kinematic equations

We have $x=0.5, y=0.46, L_1=0.4, L_2=0.3$. Now calculate E and S.

$E = \cos^{-1}\left( \frac{0.5^2+0.46^2-0.4^2-0.3^2}{2 \times 0.4 \times 0.3} \right) = 28.156^{\circ}$

$S = \tan^{-1}\left( \frac{0.46}{0.5}\right)-\cos^{-1}\left(\frac{0.5^2+0.46^2+0.4^2-0.3^2}{2\times0.4\sqrt{0.5^2+0.46^2}}\right) = 30.588^{\circ}$

Now just double check the values for S and E using the forward kinematic equations.

• $x = 0.4 \times \cos(28.156) + 0.3 \times \cos(28.156 + 30.588) = 0.5$
• $y = 0.4 \times \sin(28.156) + 0.3 \times \sin(28.156 + 30.588) = 0.46$

Both angles are correct.

Question:

The shoulder and elbow joints of a SCARA arm are driven by two identical servo motors. Each servo’s angle varies between 0° and 180° and is controlled by a PWM control signal generated by a dsPIC microcontroller. The shoulder is controlled by PWM channel 1 and the elbow is controlled by PWM channel 2.

• The dsPIC should send one pulse to each servo every 20ms.
• Each servo’s angle varies linearly with pulse width.
• A pulse width of 1ms produces an angle of 0°.
• A pulse width of 2ms produces an angle of 180°.

Assuming that the dsPIC is running at 30MIPS (i.e. Tcy=33ns) and that the PWM timebase prescaler is set to 64,

• What value should be written to the PTPER register to set the PWM period to 20ms?
• Complete the following C function which sets the shoulder joint to an angle S (in degrees):
void set_shoulder_angle(double S)
{
//
// YOU NEED TO ADD CODE HERE
//
}

Setting the PWM period to 20 ms

The PTMR register is the timebase for PWM signal generation on the dsPIC. The value stored in this register is incremented automatically at regular intervals (every 1, 4, 16 or 64  instruction cycles to be precise, depending on the prescaler setting). In this case, the prescaler is set to 64, so the PTMR register value will increase by 1 every $64 \times T_{cy}$ seconds. Every time the PTMR register value reaches the same value as the one stored in the PTPER register, PTMR is reset to zero and a pulse begins at the PWM output pin. PTMR continues counting upward in steps of one. When PTMR is equal to exactly half the value that’s stored in PDC1, the pulse on PWM output 1 ends. The same is true for PDC2 and PDC3 with regard to PWM outputs 2 and 3.

So basically, we choose the prescaler value (1, 4, 16 or 64) to control how fast the timebase PTMR counts up. We set the value of PTPER to control how often the timebase resets to zero and therefore how often a new pulse commences at each of the PWM outputs. We set the values of PDC1, PDC2 and PDC3 to control the pulse widths from each of the PWM outputs.

We have the following formula for the PWM period, $T_{pwm}$:

$T_{pwm} = T_{cy} \times \mbox{prescale\_factor} \times \mbox{PTPER}$

$\mbox{PTPER} = \frac{T_{pwm}}{T_{cy} \times \mbox{prescale\_factor}}$

$\mbox{PTPER} = \frac{20\times10^{-3}}{33\times10^{-9} \times 64} \approx 9470$

Therefore, setting PTPER equal to 9470 will deliver pulses at 20 ms intervals.

Calculating the PDC value for a given angle

According to the question, pulse widths between 1-2 ms represent the range of angles between 0-180°. A pulse will be delivered every 20 ms and every pulse will be at least 1 ms in length – the question is how much longer than 1 ms will it be. The “extra bit” of the pulse duration could be anywhere between 0 and 1 second, depending on the desired angle.

Here’s the formula for pulse width in terms of target angle S (in degrees):

$T_{pulse} = 1 + \frac{S}{180}$

Here’s the formula for pulse width on PWM channel 1 in terms of the PDC1 register value:

$\mbox{PDC1} =\frac{T_{pulse}}{\mbox{prescale\_factor} \times 0.5 \times T_{cy}}$

Combining the two previous formulae yields the following equation, which will form the basis of the set_shoulder_angle function.

$\mbox{PDC1} = \frac{1+(S/180)}{1.056}$

Ok, let’s put that into a C function:

void set_shoulder_angle(double S)
{
PDC1 = (1.0 + (S / 180.0)) / 1.056;
}

The PTPER register only needs to be set once at the beginning of the program (e.g. in configure_pins()) in order to continuously generate pulses at the right frequency.

Question:

The SCARA arm from the previous question is to be driven through a fixed sequence of four positions repeatedly, moving once every second.

• Two functions (‘set_shoulder_angle’ and ‘set_elbow_angle’)  have been provided that allow the shoulder and elbow angles to be specified in degrees.
• A function called configure_pins has been provided that sets everything up including configuring both PWM outputs to produce a pulse every 20ms.
• The desired sequence of x-y positions has been stored in the arrays x[] and y[].
• The standard function ‘__delay32(n)’, which creates a delay of n*Tcy can be used to generate the one second delay.

Part of the dsPIC’s C program is shown below. Complete the main function.

void configure_pins();
void set_shoulder_angle(double S);
void set_elbow_angle(double S);

double x[] = {0.5, 2.5, 2.0, 1.2};
double y[] = {3.0, 2.1, 2.5, 0.5};

int main()
{
// Set up the PWM outputs etc.
configure_pins();

//
// WRITE THE CODE THAT GOES HERE!
//

return 0;
}

The required C program must implement the inverse kinematic equations to work out joint angles from the specified cartesian coordinates. Here it is:

//
// SCARA Control program - written by Ted Burke - December 2010
//

void configure_pins();
void set_shoulder_angle(double S);
void set_elbow_angle(double S);

double x[] = {0.5, 2.5, 2.0, 1.2};
double y[] = {3.0, 2.1, 2.5, 0.5};

int main()
{
// Set up the PWM outputs etc.
configure_pins();

double S, E; // shoulder and elbow angles
double x, y; // target point coordinates

// Since the following values do not change, they
// do not strictly speaking need to be variables,
// but I think referring to them as L1 and L2 makes
// the inverse kinematic equation below more readable.
double L1 = 0.4;
double L2 = 0.3;

// We will use this variable to keep track of which
// point we're currently on
int n = 0;

while(1)
{
// Select the next target point from the arrays
x = x[n];
y = y[n];

// Use the inverse kinematic equations to find
// the desired shoulder and elbow angles.
// These are the exact same equations for E and S
// that were written out mathematically in an
// earlier question.
E = acos((x*x + y*y - L1*L1 - L2*L2)/(2*L1*L2));
S = atan(y/x) - acos((x*x + y*y + L1*L1 - L2*L2)
/(2*L1*sqrt(x*x + y*y)));

// Set the joint angles
set_shoulder_angle(S);
set_elbow_angle(E);

// Wait for a second. Since the chip is running at
// 30MIPS, a delay of 30,000,000 instruction cycles
// will be exactly 1 second.
__delay32(30000000);

// Move on to the next point. Go back to the
// first point if necessary.
n = n + 1;
if (n > 3) n = 0;
}

return 0;
}
Question:
PTPER is one of the dsPIC’s special function registers – it controls the PWM period. It is a 16-bit register and stores an unsigned integer value. What are the maximum and minimum values it can store?

The maximum binary number that can be stored in a 16-bit register is just the 16-digit binary number where every digit is a “1”, i.e.

1111111111111111 (binary) = $2^{16}-1$ = 65535 (decimal)

So the maximum value that can be stored in PTPER is 65535.  The minimum value is the 16 digit binary number where every digit is a “0”, i.e.

0000000000000000 (binary) = 0 (decimal)

Question:

A 10-bit analog-to-digital converter (ADC) produces unsigned integer results.

• What output value represents the maximum input voltage?
• What output value represents the minimum input voltage?

Similarly to the previous question, the largest 10-bit binary number is

1111111111 (binary) = $2^{10} - 1$ = 1023

and of course the smallest 10-bit binary number is zero.

Therefore,

• 1023 represents the maximum input voltage.
• 0 represents the minimum input voltage.
Question:

Convert the following 8-bit binary numbers to decimal:

1. 10001100
2. 00111100
3. 10101010
4. 00010001

In the familiar decimal number system, we are used to each digit representing a multiple of a different power of ten. For example,

$6481 = 6\times10^3 + 4\times10^2 + 8\times10^1 + 1\times10^0 = 6 \times 1000 + 4 \times 100 + 8 \times 10 + 1 \times 1$

Binary numbers work just the same, except that each digit (“1” or “0”) is a multiple of a power of 2. The first 8 powers of 2 are: 1, 2, 4, 8, 16, 32, 64, 128. So the four binary numbers above are:

• 10001100 (binary) = 128 + 8 + 4 = 140 (decimal)
• 00111100 (binary) = 32 + 16 + 8 + 4 = 60 (decimal)
• 10101010 (binary) = 128 + 32 + 8 + 2 = 170 (decimal)
• 00010001 (binary) = 16 + 1 = 17 (decimal)
Question:

A 10-bit analog-to-digital converter (ADC) has an input voltage range of 0V-5V.

• Find the voltage resolution of the ADC by calculating the quantisation voltage (i.e. the difference between two adjacent voltage quantisation levels)?
• The ADC samples the input voltage and gives a result of 112. Assuming that the ADC always produces unsigned integers, what was the input voltage?

Since the result of each conversion in a 10-bit ADC is a 10-bit binary number, it can only represent 1024 different voltage levels. The input voltage range of the ADC in the question is $V_{max} - V_{min} = 5 - 0 = 5\mbox{ V}$, so the quantisation voltage, $V_Q$ (the difference in voltage between two adjacent quantisation levels) is

$V_Q = \frac{V_{max} - V_{min}}{\mbox{number\_of\_levels - 1}} =\frac{5}{1023} = 4.883 \mbox{ mV}$

The minimum output value, 0, represents the minimum input voltage, $V_{min} = 0\mbox{V}$. The maximum output value, 1023, represents the maximum input voltage, $V_{max} = 5\mbox{V}$. The rest of the quantisation levels are distributed uniformly between the minimum and the maximum. Therefore, if the input voltage $V_{in}$ produces an output value of 112, then we can calculate $V_{in}$ as follows:

$V_{in} = V_{min} + (112 \times V_Q) = 0.5469\mbox{ V}$

Question:

A PT100 sensor is being used to measure the temperature of an industrial process. Its resistance at 0°C is 100Ω. Its resistance increases by 0.385Ω with every rise of 1°C in temperature. The PT100 has been placed in series with a fixed 200Ω resistance to create a potential divider as shown in the diagram below.

The output voltage of the potential divider is connected to analog input AN0 of a dsPIC. The input voltage range of the 10-bit ADC is 0-5V. The supply voltage in the circuit, $V_{DD} = 5V$. An LED warning light is connected to the dsPIC’s digital output RD0. A function “read_analog_channel()” is available to read a sample from any of the dsPIC’s analog inputs – this function returns an 10-bit unsigned int. Add code to the main function shown below so that the LED warning light is swicthed on whenever the temperature is above 200°C.

int read_analog_channel(int channel);

int main()
{
configure_pins();

//
//

return 0;
}

The steps to solve this problem are as follows:

1. Find out the PT100 resistance at 200°.
2. Calculate the potential divider output voltage at 200°.
3. Calculate what numerical value the ADC will convert that voltage to.
4. Implement a while loop that repeatedly samples the potential divider voltage and turns the LED on if and only if the reading exceeds the predetermined threshold.

At 200°, the PT100 resistance R is equal to $100 + (200 \times 0.385) = 177\Omega$

At 200°, the potential divider output voltage $V_{200} = 5 \times \frac{177}{177 + 200} = 2.347V$

The ADC result for an input voltage of 2.347V will be $1023\times\frac{2.347}{5} \approx 480$

Therefore, 480 will be the threshold value for the ADC readings. Here is the C program:

int main()
{
int voltage;

configure_pins();

while(1)
{
// Read the potential divider voltage

// If the voltage is above the threshold
// then switch on the warning LED
if (voltage > 480)
{
_LATD0 = 1; // turn on LED
}
else
{
_LATD0 = 0; // turn off LED
}
}

return 0;
}
Question:

An electric fan has a rotating dial for controlling the motor speed. The dial, which can rotates through a 180° range, is actually a variable resistor. It’s resistance varies from 1kΩ when the angle equals o° up to 10kΩ when the angle equals 18o°. A microcontroller, which controls the motor speed via PWM, needs to sense the changing resistance of the dial.

• Design a simple circuit that allows the resistance of the dial to be sensed at the analog input of the microcontroller.
• Assuming that the microcontroller has a 10-bit analog-to-digital converter (and that max and min ADC input voltages are the supply rails of the dsPIC), what is the average angular resolution of the sampled signal over the full range of movement of the dial?

In this problem, the quantity we are trying to measure is angular displacement (i.e. rotation) of the dial, which is represented by its variable resistance. The analog input of the dsPIC measures voltage rather than resistance, so a simple circuit is required to translate the variable resistance of the dial into a variable voltage. We achieve this by placing the dial’s resistance in series with a fixed resistance to create a potential divider, as shown in the following diagram:

Depending on the value of R that is chosen, a different range of voltages will be produced as the dial rotates through its full range of motion. A bigger variation of voltage is better because it will span more of quantisation levels of the ADC, providing higher resolution in the angle measurement. A reasonable rule of thumb for selecting R is to simply choose a value midway between the maximum and mimimum values of the variable resistance (1kΩ and 10kΩ in this case). A better approach again is to look for a resistor value that is close to the geometric mean of $R_{max}$ and $R_{min}$. Such a resistor would satisfy the following property:

$\frac{R_{max}}{R} \approx \frac{R}{R_{min}}$

I am choosing 3.3kΩ as a reasonable approximation to this. Let’s calculate the maximum and minimum output voltages of the potential divider. When the dial resistance is at its minimum value (1kΩ), the output voltage will be

$V_{min} = \frac{1000}{1000 + 3300} \times V_{DD} = 0.233 V_{DD}$

Similarly, when the dial resistance is at its maximum value (10kΩ):

$V_{max} = \frac{10000}{10000 + 3300} \times V_{DD} = 0.752 V_{DD}$

So, the circuit will only be using (0.752-0.233) = 0.519, i.e. approximately 52% of the input voltage range of the ADC. Rather than using all 1024 quantisation levels, the range of angular displacement of the dial will be represented by $0.519 \times 1024 = 531$ measurable voltage levels. The average angular resolution is

$\mbox{angular\_res} = 180 / 531 = 0.339$ degrees per quantisation level.|

Question:
A sensor which produces an analog output voltage between 0V-5V has an output impedance of 100kΩ. The sensor is connected to the analog input of a microcontroller. According to the data sheet provided by the manufacturer of the microcontroller, the sampling capacitance is 10pF and the series resistance due to the solid state switch at the input is less than 5kΩ. To ensure valid readings from the analog input, it is important that an adequate sampling time be allocated to allow the sampling capacitor to charge (or discharge) to a steady state voltage before commencing each analog-to-digital conversion. Calculate a suitable sampling time.

The total combined series resistance is 105kΩ and the sampling capacitor is 10pF. When the sampling switch closes, the capacitor begins charging or discharging, possibly asyptotically approaching a steady state. A handy rule of thumb is that after five time constants, the system will be as close as it will ever need to be to the steady state.

The time constant of the ADC input circuit is

$\tau = RC = 105000 \times 10 \times 10^{-12} = 1.05$ μs

A sampling time 5 times longer that the time constant. That’s $5.25$ μs.

SOLUTION COMING SOON!