# 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
- Reading sensor

**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.

- 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, , is therefore equal to 15 times the circumference of the wheel.

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

**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.

The gear ratio is 1:10. Therefore,

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

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

The distance moved per step is therefore

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

**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:

Inverse kinematic equations of the crane:

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:

Quickly double check the results:

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**

**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).

The angle

The angle

So, the shoulder angle

**Applying the inverse kinematic equations**

We have . Now calculate E and S.

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

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 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, :

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):

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

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

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:**

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) = = 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) = = 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:

- 10001100
- 00111100
- 10101010
- 00010001

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

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 , so the quantisation voltage, (the difference in voltage between two adjacent quantisation levels) is

The minimum output value, 0, represents the minimum input voltage, . The maximum output value, 1023, represents the maximum input voltage, . The rest of the quantisation levels are distributed uniformly between the minimum and the maximum. Therefore, if the input voltage produces an output value of 112, then we can calculate as follows:

**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, . 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 the analog input and digital output configure_pins(); // // ADD YOUR CODE HERE! // return 0; }

The steps to solve this problem are as follows:

- Find out the PT100 resistance at 200°.
- Calculate the potential divider output voltage at 200°.
- Calculate what numerical value the ADC will convert that voltage to.
- 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

At 200°, the potential divider output voltage

The ADC result for an input voltage of 2.347V will be

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

int main() { int voltage; // Configure the analog input and digital output configure_pins(); while(1) { // Read the potential divider voltage voltage = read_analog_channel(0); // 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 and . Such a resistor would satisfy the following property:

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

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

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 measurable voltage levels. The average angular resolution is

degrees per quantisation level.|

**Question:**

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

μs

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

SOLUTION COMING SOON!

Pingback: Scara Arm forward kinematic equation simulator--wattnotions