CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Ball and beam c code help?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
john42



Joined: 11 May 2010
Posts: 6

View user's profile Send private message

Ball and beam c code help?
PostPosted: Tue May 11, 2010 6:09 am     Reply with quote

hello guys anybody can help me to complete this code?

I used ultrasonic sensor and step motor. The aim is the used PD controller in order to balance ball.
Code:

void main(){
   //define your variables here;
   int16 time;
   long a;
   
   //set_tris for servo pin here;
   
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);

printf("Program Starts!!!\n\r");

   while(TRUE){
   delay_ms(1);
   //////////*setting the US sensor*////////
   set_tris_b(0b00000000);
   output_high(US);
   delay_us(5);
   output_low(US);
   set_tris_b(0b01000000);
   /////////////////////////////////////////
   
   ////////////*calculating the duration////
   delay_us(500);
   while (!input(US)){ };
   set_timer1(0);
   while (input(US)){ };
   time = get_timer1();
   delay_us(5);
   ///////////*calculating the distance in cm's/////
   a=(long)((float)time/10*0.034);
   printf("%lu\n\r",a);
   /////////////////////////////////////////////////
   
    if ("condition1"){ //set your condition according to position of the ball
    //do nothing
    }
    else if ("condition2"){ //set your condition according to position of the ball
    //CW-OR-CCW
    calculate your servo duty with proportional and derivative gains according to distance
    }
    else if ("condition3"){ //set your condition according to position of the ball
    //CCW-OR-CW
    calculate your servo duty with proportional and derivative gains according to distance
    }
   
   printf("-----duty=%lu\n\r",duty);
   
   for(i=0;i<10;i++)
      {
        output_high(SERVO);
        delay_us(duty);
         
        output_d(0);
        delay_ms(20);
      }
     
   //delay_ms(100);
   }
}


Last edited by john42 on Tue May 11, 2010 7:25 am; edited 1 time in total
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Tue May 11, 2010 6:59 am     Reply with quote

Is this copied directly from your homework question.

It looks like you have not even attempted it yourself yet.

At least try to get it to a compilable state. I get the impression you do not know where to start.

What do YOU think the "conditions" refer to ?
john42



Joined: 11 May 2010
Posts: 6

View user's profile Send private message

PostPosted: Tue May 11, 2010 7:06 am     Reply with quote

I got this code from my homework and I have to complete this code in order to balance to ball (ball balancer). I do not know how to complete and i have just 2 days:((
collink



Joined: 08 Jan 2010
Posts: 137
Location: Michigan

View user's profile Send private message Visit poster's website

PostPosted: Tue May 11, 2010 7:28 am     Reply with quote

What you have to do is figure out what your input is, what it means, and what that should do to your output.

I assume that this is a ball on a beam and it can roll back and forth on the beam and it's held in by rails or something? And there is a sensor which bounces an ultrasonic wave off of the ball.

So, your input is what? Seems you calculate the distance to the ball. (a in the program)

What does the distance to the ball tell you? How do you take that distance and use it to balance the ball?

How do you control the servo motor?

If you answer those questions you'll be on your way to figuring out how to solve this challenge.
john42



Joined: 11 May 2010
Posts: 6

View user's profile Send private message

PostPosted: Tue May 11, 2010 7:48 am     Reply with quote

ok i will try to explain;

- the hardware part includes pathway (ball goes forward and back), pathway rod (which is connected to the servo motor) and there is a sensor on the pathway which measures the ball distance hence it balance the ball. I did a electrical circuit which include PIC 16F877A there is no problem about this and i connected to servo motor on the pic PIN_B7 and i connected the ultrasonic sensor on the pic PIN_B6 I think that sensor will measure the ball distance and then servo motor balance the system. So we need duty cycle and PD controller code? in this code everything is explained by comments but i do not know how to write:(
collink



Joined: 08 Jan 2010
Posts: 137
Location: Michigan

View user's profile Send private message Visit poster's website

PostPosted: Tue May 11, 2010 8:08 am     Reply with quote

john42 wrote:
ok i will try to explain;

- the hardware part includes pathway (ball goes forward and back), pathway rod (which is connected to the servo motor) and there is a sensor on the pathway which measures the ball distance hence it balance the ball. I did a electrical circuit which include PIC 16F877A there is no problem about this and i connected to servo motor on the pic PIN_B7 and i connected the ultrasonic sensor on the pic PIN_B6 I think that sensor will measure the ball distance and then servo motor balance the system. So we need duty cycle and PD controller code? in this code everything is explained by comments but i do not know how to write:(


You see, the thing is, it depends on how the hardware is hooked up. Nobody here can tell you how to use hardware we haven't seen and you haven't shown. This is not to mention that you'll learn a lot more if you try to figure it out yourself. I can't speak for everyone here but personally I find it a bit odd that you were given a project to do but you've got absolutely no idea how to even start it. That's not good!

What sort of servo motor is it and how does it expect to be controlled? This is a big question which you need to find the answer to. Look up the part number online and see if you can find a spec sheet.

Your goal is this:

Find the distance that the ball is from the sensor. There will be three basic situations:

1. The ball is too far away
2. The ball is just about in the right spot
3. The Ball is too close

Given those three situations you need to either make the motor push or pull a given amount to correct the ball position. Obviously the further it is from the ideal position the more moving you've got to do. As it gets close to the right position you'll likely want to slow down and make more careful adjustments to the servo so that you do not over shoot.

And... if you can't figure it how from here you will certainly need to be spending some quality time with your teacher trying to figure out how you can better understand how to do this project.
john42



Joined: 11 May 2010
Posts: 6

View user's profile Send private message

PostPosted: Tue May 11, 2010 8:11 am     Reply with quote

ok thanks for your attention and helps:)
frankwilliam



Joined: 12 May 2010
Posts: 2

View user's profile Send private message

PostPosted: Wed May 12, 2010 7:07 am     Reply with quote

Hi all i am new to this forum.... Smile

I am working on the similar sort of project that the Arduino control of ball and beam

http://control-systems-principles.co.uk/whitepapers/ball-and-beam1.pdf

you can find in the above pdf the device which i am using now i want to know what kind of sensor i can use it to hook up to the beam in order to sense the ball on the beam ?
Because i am having two copper wires hooked up on the beam in between which the ball will roll on the beam.

Then how can i code the for sense of ball position?
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Wed May 12, 2010 10:35 am     Reply with quote

If I were building this I would use two fairly thin steel piano wires (NOT COPPER) for the ball to roll on. I would put DC current through one of the wires so that I got a voltage drop from one end of the wire to the other. Then the ball would pick up the voltage from this driven wire and conduct it to the other wire, which would be connected to an A/D converter.

Unless the wire is really thin, or you drive it with a lot of current, or you use a metal for the wire with a higher resistance than steel, you will probably need an amplifier to drive the A/D.

There are lots of other ways to do it, but this is the easiest I can think of.
_________________
The search for better is endless. Instead simply find very good and get the job done.
frankwilliam



Joined: 12 May 2010
Posts: 2

View user's profile Send private message

PostPosted: Thu May 13, 2010 2:50 pm     Reply with quote

Hi Thanks for ur reply...but here i am not able to change those wires they are already hooked up. Please let me know what kind of sensor can be used at this point?

Ultrasonic or any other and also how can i attach it to the beam?

depending on the distance of ball from the sensor it can change the beam angle....

Can u please help me in this code?

Any replies greatly appreciated....
dyeatman



Joined: 06 Sep 2003
Posts: 1923
Location: Norman, OK

View user's profile Send private message

PostPosted: Thu May 13, 2010 3:56 pm     Reply with quote

frankwilliam,
Just curious, what version of CCS compiler and MCU are you using for this project?
_________________
Google and Forum Search are some of your best tools!!!!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group