lego_ball_and_beam
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
lego_ball_and_beam [2016/01/31 20:29] – joaomatos | lego_ball_and_beam [2016/02/04 17:07] (current) – joaomatos | ||
---|---|---|---|
Line 63: | Line 63: | ||
After working on the code idea from week 1 , the final code flowchart is shown below: | After working on the code idea from week 1 , the final code flowchart is shown below: | ||
- | {{:flow_final.png?200|}} | + | {{:: |
** PROBLEMS:** | ** PROBLEMS:** | ||
Line 84: | Line 84: | ||
- | ** PD Control :** Since then i'm trying to tune the gains to make the system works smoothly, | + | ** PD Control :** Since then i'm trying to tune the gains to make the system works smoothly, |
- | {{youtube> | + | {{youtube> |
- | ** NEW ALGORITHM IDEA : ** | ||
- | Use a PID to compute the ball position and instead of using OnFwd and OnRev use angle rotation. | ||
- | ** NXT CODE (NOT WORKING PROPERLY)**: | + | ** PID Control :** I will apply the integral term after i can make the PD works. So the integral term will help on the case where the ball stops near to the setpoint , but not on it. ( the derivative term goes to 0 and the proportional term is very small ). |
- | // Sensor files | ||
- | |||
- | #include " | ||
- | #define DIST_ADDR 0x02 | ||
- | // PID GAINS | + | |
- | + | **ALGORITHM IDEA : ** | |
- | # | + | |
- | # | + | |
+ | ** NXT CODE **: | ||
- | // A function that calculates the distance and the velocity. | ||
- | | ||
- | void calculatedistvelo(int &dist, float & | ||
- | { | ||
- | int i, | ||
- | float prevtick, dt; | ||
- | |||
- | // The distance used is the average from 5 measures. | ||
- | | ||
- | while (i<5) | ||
- | { | ||
- | d_array[i]=(DISTNxReadValue(S1, | ||
- | i=i+1; | ||
- | } | ||
- | i=0; | ||
- | | ||
- | dist=ArrayMean(d_array, | ||
- | dt=CurrentTick()- prevtick; | ||
- | velocity=(dist-prevdist)/ | ||
- | velocity=velocity*1000; | ||
- | prevtick=CurrentTick(); | ||
- | prevdist=dist; | ||
- | } // end calculate distance and velocity | ||
+ | // Files from the Long Range IR sensor | ||
+ | #include " | ||
+ | #define DIST_ADDR 0x02 | ||
- | | + | task main() |
- | { | + | |
- | // Initialize | + | SetSensorLowspeed(S1); |
+ | DISTNxSendCommand(S1, | ||
+ | byte result; | ||
+ | PosRegEnable(OUT_A); | ||
+ | |||
+ | // Defining Variables | ||
+ | float KP=1, | ||
+ | int position, dist, d_setpoint, i=0 , d_array[5]; | ||
+ | float velocity; | ||
+ | int prevdist, | ||
+ | float prevtick, dt; | ||
+ | string positionstr, | ||
+ | |||
+ | // Preparing to enter the loop | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | |||
+ | // Control Loop | ||
+ | while (true) | ||
+ | { | ||
+ | // The distance used is the average from 5 measures. | ||
+ | while (i<5) | ||
+ | { | ||
+ | | ||
+ | | ||
+ | } | ||
+ | | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | // ball position equation applying PD control: | ||
+ | | ||
+ | |||
+ | // Show the raw distance | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | // conditional statements to control the platform | ||
+ | if (position >35 & position <45) | ||
+ | { | ||
+ | | ||
+ | | ||
+ | } | ||
- | SetSensorLowspeed(S1); | + | if (position >45 & position <55) |
- | DISTNxSendCommand(S1, DIST_CMD_ENERGIZED, | + | { |
- | byte result; | + | |
+ | Wait(100); | ||
+ | } | ||
+ | if (position >55 & position <65) | ||
+ | { | ||
+ | | ||
+ | Wait(100); | ||
+ | } | ||
- | | + | if (position >65 & position <75) |
+ | | ||
+ | | ||
+ | | ||
+ | } | ||
- | int power, dist, d_setpoint=480; | + | if (position >75 & position <85) |
- | float velocity; | + | |
- | while(true) | + | |
{ | { | ||
+ | | ||
+ | | ||
+ | } | ||
+ | |||
+ | if (position >85 & position <100) | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | |||
+ | if (position >100 & position <300) | ||
+ | { | ||
+ | | ||
+ | | ||
+ | } | ||
+ | |||
+ | if (position >300) | ||
+ | { | ||
+ | | ||
+ | | ||
+ | } | ||
+ | |||
+ | // Especial conditions , close to the setpoint | ||
+ | if (position <35 & position >-35) | ||
+ | { | ||
+ | | ||
+ | | ||
+ | } | ||
+ | |||
+ | if (position <-35 & position >-45) | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | |||
+ | if (position <-45 & position >-55) | ||
+ | { | ||
+ | | ||
+ | | ||
+ | } | ||
- | // Get the Distance and Velocity of the ball from the function | + | if (position <-55 & position >-65) |
- | + | { | |
- | calculatedistvelo(dist,velocity); | + | PosRegAddAngle(OUT_A,30); |
+ | | ||
+ | } | ||
- | //Power equation using PD control: | + | if (position <-65 & position >-75) |
+ | { | ||
+ | | ||
+ | | ||
+ | } | ||
- | power = KP*(dist-d_setpoint) + KD*(velocity); | + | if (position <-75 & position >-85) |
- | + | { | |
- | // Power < 0 means the ball is going in direction to the sensor | + | PosRegAddAngle(OUT_A, |
- | // OnFwd makes the platform goes down ( to break the ball ) | + | Wait(100); |
- | // Power >0 0 means the ball is going in direction to the motor | + | } |
- | // OnRev makes the platform goes up ( to break the ball ) | + | |
- | if (power > 0) | + | if (position <-85 & position |
{ | { | ||
- | OnRev(OUT_A,(power/15)); | + | PosRegAddAngle(OUT_A,45); |
} | } | ||
- | if (power < 0 ) | + | |
+ | if (position | ||
{ | { | ||
- | OnFwd(OUT_A,abs((power/15))); | + | PosRegAddAngle(OUT_A,50); |
+ | } | ||
+ | |||
+ | | ||
+ | { | ||
+ | | ||
+ | | ||
} | } | ||
- | |||
- | | ||
- | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
- | ** #WEEK 3 SCHEDULE**: | ||
- | *1: I'm planning to spend one more day trying to do the gain tune. | + | ** #WEEK 3 REPORT **: |
- | *2: Finish all the theoretical work: derive the transfer function and simulate it on Simulink | + | *1: I changed |
- | *3: Get back to the algorithm | + | *2: Derivation of the transfer function that represent the ball and beam system. |
- | *4: Make the system | + | *3: Simulation using Simulink and comparison with real system |
- | *5: Include a File Saving function on the code, plot a disturbance rejection situation and compare it to the simulink model. | + | [[pid_vs_lqr_ballandbeam| WEEK 3 WORK ]] |
lego_ball_and_beam.1454300975.txt.gz · Last modified: by joaomatos