Ball and Beam Control System - Page 1
Forum section: electronics / projects

author: nosleep
author_rank: Newbie
timestamp: April 08, 2014, 03:42:18 pm
content: Hello!I am currently developing a ball and beam control system for a competition, and I would like to show you what I've done so far. Also, I will keep this thread updated with the progress until I finish my project.Here is the list of materials:1. Plexiglas beam (1 meter – cut with the aid of a CNC machine into a beam with 2 grooves on the bottom and 4 walls to keep the ball from rolling off; 2 of them go the length of the beam, 2 of them go the width of the beam) – recycled from a broken protection window2. 6 pieces of Iron – all of them plastic-covered in order to be protected from rust and to trump their conductivitya. 1 for the base of the rigb. 2 in order to provide the support for the beamc. 2 to provide support of the motord. 1 as support and stabilizer for the pot3. 3-12V DC motor (shipped with motor support), 6mm (diameter) shaft4. 2 ball bearings5. 2 safety rings, to prevent slipping6. Aluminum platea. Supports the middle of the beam, spins it, as the motor shaft was not long enough; The aluminum plate and the motor shaft are connected via an aluminum cylinder with the use of 2 grub screws7. One rotation logarithmic pota. Used as angular (resistive) sensor for the motorb. Connected to the shaft via an aluminum cylinder-grub screw ensemble8. Rubber carpet, glued to the base, to prevent slipping9. Resistive Wire (with 47 ohm resistor on one end), Copper wire and steel ball, as position sensor for the ball (voltage divider)10. Steel Ball – controlled element11. PmodCLS - Character LCD with serial interface – for the display of the ball’s position, output variables, and other useful information12. PmodHB3 - H-bridge w/ feedback inputs13. chipKit uC32 Prototyping platform - calculates the control signal needed for the system, communicates with the motor and sensorsAlso, I made this video. Please excuse the weird voice, 'tis what mother nature blessed me with :lol: :Have a nice day!

author: Neverther
author_rank: Regular Contributor
timestamp: April 08, 2014, 06:14:13 pm
content: I remember inverted pendulum projectwork at school, 4th degree nonlinear system with analog control...Not as bad, have fun controlling the oscillation.

author: nosleep
author_rank: Newbie
timestamp: April 08, 2014, 07:09:11 pm
content: Oh I think I'll have lots of fun.

author: Dongulus
author_rank: Regular Contributor
timestamp: April 08, 2014, 07:43:43 pm
content: What type of controller do you plan on implementing?

author: nosleep
author_rank: Newbie
timestamp: April 09, 2014, 10:18:54 am
content: Dongulus:Probably PID.As stated in the video, I'm modeling the system first. I'll see better what's the best option at that time. Do you have any suggestions or ideas I could use?

author: Dongulus
author_rank: Regular Contributor
timestamp: April 10, 2014, 08:41:58 pm
content: Naw. A PID controller is pretty good to start with. It's easy to tune using the Nichols-Ziegler method and you can use it without having to dive into a lot of control theory.

author: nosleep
author_rank: Newbie
timestamp: April 12, 2014, 10:28:23 am
content: I was thinking the same. I'd like to keep it as simple as possible, for everyone to understand. It is, after all, intended for lab use.

author: Neverther
author_rank: Regular Contributor
timestamp: April 12, 2014, 09:16:03 pm
content: Modelling that system is pretty easy with couple assumptions (rotation axis offset from ball rail is zero so you dont need to worry about tangential acceleration causing forces along the rail when the angle changes, the angular speed by motor causes still causes acceleration along the axis, get slow enough gearbox so the motor no problem starting -> no need to worry about angular acceleration, or just assume its not relevant).Heck, I'd guess basic tuning could be done with just simple integrator as model (close enough when offset isnt huge, impulse response would throw model away from the system as ball still has velocity, but the system isn't pure double integrator either as there is friction and other stuff).All the system is composed of is the position (offset) from the angle (causes acceleration->velocity->position) and the change of angle (read above), apply some basic geometry with physics and you got a system with almost all the data but friction forces. Friction could be calculated from step response of the real system as you alredy got position information from the resistive wire (and speed and acceleration).And then you take the dive into control theory, probably a simplified model with second order dynamics (ignore air friction as that is porpably not worth compensating), probably not too hard to optimize as you got mechanical limits in motor.Ok that post came bit longer than I tought.If all else fails, go bang-bang, it's not like it is going to oscillate after defining dead zone

author: GiskardReventlov
author_rank: Frequent Contributor
timestamp: April 12, 2014, 10:36:34 pm
content: Good luck in the competition and thanks for sharing. Looks well done. Without seeing the competition I'd bet money on yours.How constrained are you in the design and materials?p.s. no worry about the voice, if someone made fun of it they're probably intimidated.

author: nosleep
author_rank: Newbie
timestamp: April 15, 2014, 06:23:58 am
content: Thank you Neverther, I will surely take into consideration all of your indications. I am a bit nervous about the controller, but given enough patience I am sure I can implement a pretty good one. Also, control theory is one of my favourite fields.GiscardReventlov, thank you, but my system is not the best yet.The chipkit and the pmods are samples from Digilent, for this competition.Most of the mechanical parts are recycled.The constraints I've tried to impose on myself are mostly regarding the cost, as I want to keep it as low as possible.The design I've kept simple, apart from the part where the beam is mounted; the Aluminum plate is mounted to the motor shaft using a system closely resembling the one a double bass drum pedal is using (more specifically, Tama's Iron Cobra).The software I intend to keep as simple as possible.Any Ideas?

author: GiskardReventlov
author_rank: Frequent Contributor
timestamp: April 15, 2014, 03:32:01 pm
content: Quote from: nosleep on April 15, 2014, 06:23:58 amThis is what I'd bet on, the correct attitude/focus.QuoteI have an idea, that's all it is so it's easy to discard or not. A few thoughts on competing. For this kind of judged competition you need to appeal to the judges a little bit. You need to make your project cause them to look close and take note. You're competing for their attention.With that in mind I was thinking to make the ball from some transparent material. The ball is the sensor and will communicate the pertinent data and change color based on speed. I'd use the typical color range for heat. Slow == cool, fast == hotblue -> red.Might be more work than it's worth but it'd stand out. You could also conceal the sensor in the ball and really make them take a longer look at your entry. Not sure but maybe some judge might not figure it out and think it's missing one of the key elements. But it could be an interesting play once it's revealed that it does have all the elements.It's easy for me to suggest more work!

author: FJV
author_rank: Regular Contributor
timestamp: April 15, 2014, 04:34:03 pm
content: Maybe a fuzzy logic controller would be a fun and interesting option.

author: Neverther
author_rank: Regular Contributor
timestamp: April 15, 2014, 10:31:56 pm
content: Nah, fuzzy logic is overkill and hard to tune on this application.I assume the controller only needs to compensate from initial condition (ball placed on the end of rail/ball has velocity/etc) to known rest position.Fuzzy controllers are more useful approach if you got MIMO/MISO systems you need to balance and tune.Might aswell implement discrete extended kalman filter, still needs a model tough.Neural network model would just need teaching and as there is just once case, no worry about needing to tranfer it to another system.Still an overkill.Compare different PID tuning methods against optimal controller?

author: nosleep
author_rank: Newbie
timestamp: May 05, 2014, 07:22:41 am
content: Hi!So I've modeled the system and wrote the following code:------//libraries used:#include <Wire.h>//#include <Lcds.h>//#include <DSPI.h>//#include <Bounce.h>//#include <Pid_v1.h>//pin definitions:const int bpin = A0; //pin used for reading the position of the ballconst int potpin = A1; //pin used for reading the motor's shaft positionconst int enable = 5; //PWM pin for the motorconst int dir_mot= 7; //used or controlling the direction of the motor//global variablesint ball_pos;int ball_pos_val;int shaft_pos;int shaft_pos_val;int error=0;;float dt=0.5; //how often the PID will evaluate ->sampletimefloat proportional;float integrative;float derivative;int setpoint=0;//innitialize errorint lasterror=0; //innitialize last errorfloat bpl;// kp=2.4 -->proportional component of the controller// kd=1.3 --> derivative component of the controller// ki=0.37 -->integrative component of the controllerfloat kp;float ki;float kd;float pid;int pid_out;void setup() //sets pin modes, begins serial communication{pinMode (bpin, INPUT); //set pin as inputpinMode (potpin, INPUT); //set pin as inputpinMode (enable, OUTPUT); //set pin as outputpinMode (dir_mot, OUTPUT); //set pin as output//begin serial Communication @ 9600 baudSerial.begin(9600);}void loop(){read_motor();Serial.print ("shaft=");Serial.print(shaft_pos_val);Serial.print (" ");read_ball();Serial.print ("ball=");Serial.print(ball_pos_val);Serial.print(" ");pid_control();if (error<0){digitalWrite(dir_mot,LOW);analogWrite(enable,pid_out);delay(300);}else{digitalWrite(dir_mot,HIGH);analogWrite(enable,pid_out);delay(300);}Serial.print("pid=");Serial.print(pid_out);Serial.println (" ");if (ball_pos_val==setpoint){proportional=0;integrative=0;derivative=0;}}void read_ball() //function to find the position of the ball{int i=1;float bp1=0;//we now read the position of the ballwhile (i<=10){ball_pos=analogRead(bpin);delay(10);//change to 5bp1=bp1+ball_pos;i++;}ball_pos_val=(int)bp1/10;}void read_motor() //here we read the position of the motor shaft{int i=1;float shaftpos1=0;while (i<=10){shaft_pos= analogRead(potpin);delay(5); //change to 5shaftpos1=shaftpos1+shaft_pos;i++;}shaft_pos_val=(int)shaftpos1/10;}void pid_control(){lasterror=error;error=ball_pos_val-setpoint;//the ball position is the outputproportional=2*error;integrative+=error;derivative=0.2*(error-lasterror)/dt;pid=proportional+(0.01*integrative)+derivative;pid_out=(int)pid/10;pid_out=abs(pid_out);if (pid_out>255){pid_out=255;}if (pid_out<0){pid_out=0;}}LATER EDIT. I found the error. I forgot to disable the PWM pin while changing direction. Rookie mistake.Anyway. What do you think about the code?It seems to compile just fine, yet when uploaded to the chipkit the motor spins in only one direction, regardless of the position of the ball.I've scoured the code for bugs and problems for days and days and can't seem to find the issue. Would you please have a look?Thank you!

author: Neverther
author_rank: Regular Contributor
timestamp: May 05, 2014, 07:28:15 pm
content: I see you're not a veteran on C.Couple notes:Learn to pass arguments to functions so they dont need to pass information trough global variables.Terms for old error and integrator can be global in my opinion as it helps make it less complex, rest are unnecessary and overcomplicate it.Lots if useless stuff in global.The error can be positive or negative, let the PID output positive and negative values and then map that to direction (checking against error might break it as if derivative worked, it could have driven the motor to slow the ball down alredy).You can use #define to set PID terms and other constants.Your integrator has no antiwindup, but works aslong you dont abuse the limits.I dont know physical masses (ball speed) but usually the faster you make the system, the better it usually balances it (current delays on reading values may cause oscillation).Zeroing PID does almost nothing, ony integrative is zeroed, dunno about that. Derivative doesnt do anything as youre writing error to lasterror before calculations.Other tricks:You can constantly update analog data in the backround with interrupts and just sample them when you need (gets rid of readball/motor delays).Use circular buffer if you do averaging to the values and sample interrupts.That on surface read.

author: nosleep
author_rank: Newbie
timestamp: May 07, 2014, 11:19:13 pm
content: Not really a veteran, Neverther, but still eager to perfect myself.Thank you for the advice, it's beed very useful. I have finished the code and am now tweaking it.Also, I switched the plexi beam for a stainless steel, bottomless one. It's just as light and much more resistant. I'm using an IR sensor for the position of the ball now. Once it's done, I could leave a link to a video here.Have a nice day!

author: nosleep
author_rank: Newbie
timestamp: May 13, 2014, 05:53:09 pm
content: guys look.