User Tools

Site Tools


ball_and_beam

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Last revisionBoth sides next revision
ball_and_beam [2016/11/01 15:49] – created dwallaceball_and_beam [2016/11/09 14:17] dwallace
Line 1: Line 1:
 ====== Ball and Beam ====== ====== Ball and Beam ======
  
-==Motivation== +===== Motivation =====
-The ball and beam system is a classic control theory problem. This simple system is an easily built testbed for control algorithms. Like the inverted pendulum, this system is open-loop unstable and therefore cannot be controlled without sensor feedback. <br> +
-'''Goal:''' Using a beam able to rotate via a servo, the position of a ball rolling on the beam should be maintained at a set point on the beam. This system should be robust to disturbance of the ball and and position the ball in a reasonable amount of time with little positional overshoot. The controller will be tested with static input and by tracking different functions. I will use a LEGO NXT, motor and light sensors.<br>+
  
-==Examples==+The ball and beam system is a classic control theory problem. This simple system is an easily built testbed for control algorithms. Like the inverted pendulum, this system is open-loop unstable and therefore cannot be controlled without sensor feedback. 
 +**Goal:** Using a beam able to rotate via a servo, the position of a ball rolling on the beam should be maintained at a set point on the beam. This system should be robust to disturbance of the ball and and position the ball in a reasonable amount of time with little positional overshoot. The controller will be tested with static input and by tracking different functions. I will use a LEGO NXT, motor and light sensors. 
 + 
 +===== Examples =====
 <html> <html>
 <iframe width="420" height="315" src="http://www.youtube.com/embed/KSlPmbNaPN8" frameborder="0" allowfullscreen></iframe> <iframe width="420" height="315" src="http://www.youtube.com/embed/KSlPmbNaPN8" frameborder="0" allowfullscreen></iframe>
 <iframe width="420" height="315" src="http://www.youtube.com/embed/YYZXVt4dxAE" frameborder="0" allowfullscreen></iframe> <iframe width="420" height="315" src="http://www.youtube.com/embed/YYZXVt4dxAE" frameborder="0" allowfullscreen></iframe>
 </html><br> </html><br>
-''Note:'' If the videos do not load, just refresh the page.<br> 
  
-==Model==+**Note:** If the videos do not load, just refresh the page.<br> 
 + 
 +===== Model ===== 
 After drawing a free body diagram of a ball on a tilted beam, it can be seen that the acceleration due to gravity is proportional to sin(theta) where theta is the angle of the beam with 0 degrees being the horizontal orientation. After drawing a free body diagram of a ball on a tilted beam, it can be seen that the acceleration due to gravity is proportional to sin(theta) where theta is the angle of the beam with 0 degrees being the horizontal orientation.
  
-<pre> m*g*sin(theta) = m*x'' </pre>+  m*g*sin(theta) = m*x''
  
 Based on the small angle theorem, this relation can be approximated by in the form: Based on the small angle theorem, this relation can be approximated by in the form:
  
-<pre> g*theta = x'' </pre>+  g*theta = x''
  
-[[image:BallBeamFBD.jpg|Diagram courtesy of control-systems-principles.co.uk]]+{{dylanw:BallBeamFBD.jpg}}\\  
 +Diagram courtesy of control-systems-principles.co.uk\\  
 + 
 +===== Control =====
  
-==Control== 
 As seen in the approximated equation of motion above, acceleration of the ball on the beam is proportional to the angle of the beam. This angle of the beam (theta), along with the position of the ball (x) and the velocity of the ball (x') can be either measured directly or obtained analytically from the accessible data.  As seen in the approximated equation of motion above, acceleration of the ball on the beam is proportional to the angle of the beam. This angle of the beam (theta), along with the position of the ball (x) and the velocity of the ball (x') can be either measured directly or obtained analytically from the accessible data. 
-<br>+
 Two controllers must be developed to control this system: am angular position controller for the motor controlling beam angle and a controller for the ball position and velocity on the beam. For this project, a stock controller available through the NXC programing language was used to control the motor position. This controller uses Proportional-Integral-Derivative (PID) control to set a motor position with an appropriate rise time and minimal overshoot of the set value. This controller can be accessed in two ways. One way allows the PID gains to be set manually. The second way (used in the following controller code) allows the user to control the maximum motor velocity and acceleration effectively dampening the system.  Two controllers must be developed to control this system: am angular position controller for the motor controlling beam angle and a controller for the ball position and velocity on the beam. For this project, a stock controller available through the NXC programing language was used to control the motor position. This controller uses Proportional-Integral-Derivative (PID) control to set a motor position with an appropriate rise time and minimal overshoot of the set value. This controller can be accessed in two ways. One way allows the PID gains to be set manually. The second way (used in the following controller code) allows the user to control the maximum motor velocity and acceleration effectively dampening the system. 
-<br> + 
-===Motor Position Control===+==== Motor Position Control ===
 For PID control of the NXT motor on Port A: For PID control of the NXT motor on Port A:
-<source lang="c">+ 
 +<code c>
 OnFwdRegPID(byte outputs, OnFwdRegPID(byte outputs,
   char pwr,   char pwr,
Line 38: Line 45:
   byte d     byte d  
   )   )
-</source> +</code>
-<br>+
  
 For Velocity and Acceleration control of the NXT motor on Port A: For Velocity and Acceleration control of the NXT motor on Port A:
-<source lang="c">+ 
 +<code c>
 PosRegEnable(OUT_A); PosRegEnable(OUT_A);
  
Line 51: Line 58:
  
 PosRegSetAngle (OUT_A, angle); PosRegSetAngle (OUT_A, angle);
-</source+</code
-<br>+
 The angular position of the motor is accessed via a built in encoder using the following command: The angular position of the motor is accessed via a built in encoder using the following command:
-<source lang="c"> 
-curAngleInDegrees = MotorRotationCount(OUT_A); 
-</source> 
-<br> 
  
-===Ball Position and Velocity Control===+  curAngleInDegrees = MotorRotationCount(OUT_A); 
 + 
 + 
 +==== Ball Position and Velocity Control ===
 To control the ball on the beam, a PD controller was employed to control the ball position on the beam and its positional derivative, velocity. Control of position allows the ball to be maintained at a set point on the beam while control of the velocity slows the ball as it heads toward the set point.  To control the ball on the beam, a PD controller was employed to control the ball position on the beam and its positional derivative, velocity. Control of position allows the ball to be maintained at a set point on the beam while control of the velocity slows the ball as it heads toward the set point. 
-<br> 
  
 To control these parameters, the ball's position on the beam must be measured on the beam. This was done using two infrared range finders positioned at either end of the beam. The difference between the values obtained from each sensor was used to determine the ball position on the beam. To control these parameters, the ball's position on the beam must be measured on the beam. This was done using two infrared range finders positioned at either end of the beam. The difference between the values obtained from each sensor was used to determine the ball position on the beam.
-<br> + 
-====Sensors==== +=== Sensors === 
-The sensor used was the "'''High Precision Medium Range Infrared distance sensor for NXT (DIST-Nx-Medium-v3)'''" from ''Mindsensors.com''+ 
-<br>http://www.mindsensors.com/index.php?module=pagemaster&PAGE_user_op=view_page&PAGE_id=72 +The sensor used was the **High Precision Medium Range Infrared distance sensor for NXT (DIST-Nx-Medium-v3)** from [[http://www.mindsensors.com/index.php?module=pagemaster&PAGE_user_op=view_page&PAGE_id=72|Mindsensors.com]]
-<br>+
  
 Documentation and examples for using this sensor can be found on their website. Documentation and examples for using this sensor can be found on their website.
  
-<source lang="c">+<code c>
 SetSensorLowspeed(S1); SetSensorLowspeed(S1);
  
Line 78: Line 83:
  
 DR = DISTNxReadValue(S1, DIST_REG_DIST_LSB, 2, DIST_ADDR); //Read Sensor Value DR = DISTNxReadValue(S1, DIST_REG_DIST_LSB, 2, DIST_ADDR); //Read Sensor Value
-</source>+</code>
  
-<br>+=== Calibration ===
  
-====Calibration==== 
 Before the control loop started, the sensors were calibrated to provide a difference of zero (0) when the ball was in the center of the beam. This was done by taking the average of many readings to calculate an offset necessary for each sensor to return a zero at the origin. This offset was then applied to every subsequent reading used for control. Before the control loop started, the sensors were calibrated to provide a difference of zero (0) when the ball was in the center of the beam. This was done by taking the average of many readings to calculate an offset necessary for each sensor to return a zero at the origin. This offset was then applied to every subsequent reading used for control.
  
-<source lang="c">+<code c>
 int calibNum = 30; int calibNum = 30;
 int i; int i;
Line 105: Line 109:
 DL_old =    DISTNxReadValue(S2, DIST_REG_DIST_LSB, 2, DIST_ADDR)-DL_offset; DL_old =    DISTNxReadValue(S2, DIST_REG_DIST_LSB, 2, DIST_ADDR)-DL_offset;
 DR_old =    DISTNxReadValue(S1, DIST_REG_DIST_LSB, 2, DIST_ADDR)-DR_offset; DR_old =    DISTNxReadValue(S1, DIST_REG_DIST_LSB, 2, DIST_ADDR)-DR_offset;
-</source+</code
-<br> + 
-====Data Smoothing====+=== Data Smoothing === 
 A very simple data averaging filter was employed to help smooth the very noisy range sensor data. This filter, before releasing a data point, took multiple readings from each sensors and provided an average reading for each sensor every iteration. Although simple, this filter helped reduce the impact of outlying data points and slowed the control loop down minimally. A very simple data averaging filter was employed to help smooth the very noisy range sensor data. This filter, before releasing a data point, took multiple readings from each sensors and provided an average reading for each sensor every iteration. Although simple, this filter helped reduce the impact of outlying data points and slowed the control loop down minimally.
  
-<source lang="c">+<code c>
 int inLoopAvg = 3; int inLoopAvg = 3;
 int j; int j;
Line 127: Line 132:
 DL = DL/inLoopAvg; DL = DL/inLoopAvg;
 DR = DR/inLoopAvg; DR = DR/inLoopAvg;
-</source+</code
-<br>+ 
 +=== Tuning ===
  
-====Tuning==== 
 The P and D gains were chosen by trial, error and observation. First, P was chosen to react to the ball's existence on either side of the zero position (center of the beam). With just a P controller, the beam would tilt clockwise with the ball on the left and counter-clockwise with the ball on the right. This controller provides the ability to react to a non-central ball location, tilting to move the ball in the direction of the center of the beam. The P and D gains were chosen by trial, error and observation. First, P was chosen to react to the ball's existence on either side of the zero position (center of the beam). With just a P controller, the beam would tilt clockwise with the ball on the left and counter-clockwise with the ball on the right. This controller provides the ability to react to a non-central ball location, tilting to move the ball in the direction of the center of the beam.
-<br> 
  
 The position of the ball is proportional to the calibrated difference between the two range sensors. The position of the ball is proportional to the calibrated difference between the two range sensors.
-<br> 
  
-The D gain was chosen to dampen the velocity of the ball as it rolled past the set point. When tuning the value, it is beneficial to attempt to control the ball manually (tilting the beam by hand). This trial will give insight into what type of motion is necessary to slow the ball down before it passes the set point (zero). The derivative (velocity) controller should be tuned to prevent overshoot while still achieving quick rising and settling times.<br>+The D gain was chosen to dampen the velocity of the ball as it rolled past the set point. When tuning the value, it is beneficial to attempt to control the ball manually (tilting the beam by hand). This trial will give insight into what type of motion is necessary to slow the ball down before it passes the set point (zero). The derivative (velocity) controller should be tuned to prevent overshoot while still achieving quick rising and settling times.
  
 Velocity is calculated as the difference between the ball position from the last position and the current measured position divided by the time elapsed during the iteration.<br> Velocity is calculated as the difference between the ball position from the last position and the current measured position divided by the time elapsed during the iteration.<br>
  
-<pre> +  (Current - Previous) / deltaT 
-(Current - Previous) / deltaT + 
-</pre> +For the ball and beam system, built with a LEGO NXT motor geared down 1:5, the values used were: 
-<br> + 
-For the ball and beam system, built with a LEGO NXT motor geared down 1:5, the values used were:<br> +  KP = 0.15 
-KP = 0.15<br> +  KD = 0.04 
-KD = 0.04<br>+  MotorAngle = KP*Position + KD*Velocity 
 + 
 +===== Video ===== 
 + 
 +{{youtube>zTQ0HEyV-Aw?large}}\\  
 + 
 +**Note:** If the videos do not load, just refresh the page.<br>
  
-<pre> +===== Code =====
-MotorAngle KP*Position + KD*Velocity +
-</pre> +
-<br> +
-==Video== +
-<html><iframe width="853" height="480" src="http://www.youtube.com/embed/zTQ0HEyV-Aw" frameborder="0" allowfullscreen></iframe></html> +
-<br> +
-''Note:'' If the videos do not load, just refresh the page.<br>+
  
-==Code== +<code BallandBeam.nxc>
-<source lang="c">+
 // Program: BallAndBeam.nxc // Program: BallAndBeam.nxc
 // Author:  Alex Alspach ([email protected]) // Author:  Alex Alspach ([email protected])
Line 281: Line 282:
  DISTNxSendCommand(S1, DIST_CMD_DEENERGIZED, DIST_ADDR);  DISTNxSendCommand(S1, DIST_CMD_DEENERGIZED, DIST_ADDR);
 } }
-</source+</code
-<br> + 
-[[File:BallAndBeam.nxc.zip]] +{{dylanw:BallAndBeam.nxc.zip}} 
-<br>+ 
 +===== Future Work =====
  
-==Future Work== 
 The ball and beam system shown in the video is a work in progress and performs poorly at this point. In this first go at the ball and beam system design and control, many lessons were learned that can be used to create a much more stable system and controller. The ball and beam system shown in the video is a work in progress and performs poorly at this point. In this first go at the ball and beam system design and control, many lessons were learned that can be used to create a much more stable system and controller.
-<br> + 
-===Mechanical Design===+==== Mechanical Design ===
 The beam built, along with the frame on which it rotates, is flimsy. This beam is so flimsy that it actually arcs so that either end, when horizontal, is lower than the center. This became noticeable when attempting to tune a gentle proportional controller. Although the beam would tilt, the ball would remain in a low point at either end of the beam, not reacting to the corrective motions. The beam built, along with the frame on which it rotates, is flimsy. This beam is so flimsy that it actually arcs so that either end, when horizontal, is lower than the center. This became noticeable when attempting to tune a gentle proportional controller. Although the beam would tilt, the ball would remain in a low point at either end of the beam, not reacting to the corrective motions.
  
 The frame is also not nearly rigid enough. It allows not only the intended degree of freedom but fast motion and offset weight of the range sensors cause rotation about an upward pointing axis. This was reduced by tightening a zip-tie around the frame but ultimately, these motions still interfered with the dynamics to be controlled and, therefore, the stability of the system. The frame is also not nearly rigid enough. It allows not only the intended degree of freedom but fast motion and offset weight of the range sensors cause rotation about an upward pointing axis. This was reduced by tightening a zip-tie around the frame but ultimately, these motions still interfered with the dynamics to be controlled and, therefore, the stability of the system.
  
-===Backlash===+==== Backlash ===
 Backlash in the NXT motor amounts to almost five degrees of free motion while the motor is meant to be rotationally static. This "slop" was reduced by gearing the system down 5:1 but this was not enough. This error, along with even more rotational error due to beam driving axle torsional deformation, causes hardly precise measurements of the motor angle and poor ability to reach a specified angular position. This slop could be reduced by gearing down further or using more high precision servos. Backlash in the NXT motor amounts to almost five degrees of free motion while the motor is meant to be rotationally static. This "slop" was reduced by gearing the system down 5:1 but this was not enough. This error, along with even more rotational error due to beam driving axle torsional deformation, causes hardly precise measurements of the motor angle and poor ability to reach a specified angular position. This slop could be reduced by gearing down further or using more high precision servos.
  
-===Motor Response===+==== Motor Response ===
 The motor position control needs more attention. Currently, the acceleration is too high, causing a very jerky system. These jerky motion causes quick changes in velocity and therefore quick over-compensations from the velocity controllers. This motion and reactionary motion causes the system to approach instability. The motor position control needs more attention. Currently, the acceleration is too high, causing a very jerky system. These jerky motion causes quick changes in velocity and therefore quick over-compensations from the velocity controllers. This motion and reactionary motion causes the system to approach instability.
  
-===Position Sensing===+==== Position Sensing ===
 The ball position data is extremely noisy, inconsistent and, therefore, unreliable. This is for many reasons. The ball position data is extremely noisy, inconsistent and, therefore, unreliable. This is for many reasons.
 The sensors are not mounted securely enough. Sudden motion causes the mounts to flex and the range data, even if only slightly, to change. Even with a data filter, this system property causes errors from the very beginning. Secondly, the sensors are not focused perfectly on the ball as it moves and the relation between sensor output and ball location is not exactly linear. The sensors are not mounted securely enough. Sudden motion causes the mounts to flex and the range data, even if only slightly, to change. Even with a data filter, this system property causes errors from the very beginning. Secondly, the sensors are not focused perfectly on the ball as it moves and the relation between sensor output and ball location is not exactly linear.
  
-===Filtering===+==== Filtering ===
 The filtering method used (averaging multiple data points each iteration) is very basic and slows the loop more as more data is averaged. A more advanced filtering algorithm may be necessary if sensor data stays this noisy. The filtering method used (averaging multiple data points each iteration) is very basic and slows the loop more as more data is averaged. A more advanced filtering algorithm may be necessary if sensor data stays this noisy.
-<br> 
  
-==Sources==+===== Sources ===== 
 http://www.control-systems-principles.co.uk/whitepapers/ball-and-beam1.pdf, Peter Wellstead http://www.control-systems-principles.co.uk/whitepapers/ball-and-beam1.pdf, Peter Wellstead
ball_and_beam.txt · Last modified: 2016/11/09 14:18 by dwallace