C Programming newbie language barrier problem

This is a discussion on C Programming newbie language barrier problem within the C Programming forums, part of the General Programming Boards category; Hello Everyone, I am new to C programming, have been working in electrical engineering for the last 8/9 years but ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    3

    C Programming newbie language barrier problem

    Hello Everyone,

    I am new to C programming, have been working in electrical engineering for the last 8/9 years but have never had to touched C programming.

    I have just been put on a course through work and have been thrown in at the deep end to say the least, or at least thats what it feels like to me.

    Basically im working on a project having had no experience. Im hoping that maybe other more experienced members and programmers might be able to instill some of there knowledge into me. I have purchased a book, watched hours of online tutorials but i have hit a brick wall. The wall being that i know what i want my equipment to do but i dont know how to write it in code to tell it what to do. And assistance or advice would be much appreciated.

    I have attempted using timers, call back functions, mutual exclusions, semaphores and basics like printf and scanf. Individually they seem ok but putting them into practice and working together i get lost in translation.

    I am using the following gear (just to offer a background)

    Windriver Vxworks
    A separate target which is pentium based

    I have to install vxworks on to a floppy and let the target boot from the floppy that will then control my project.

    I hope I have described this correctly at the moment it all seems a little much.

    Anyway I look forward to chatting with you.

    Dixi

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Try and think of your devices as closed up "boxes", which should help put the focus on the software.

    What is it that you're trying to get the C program to do?

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    3
    Basically I have a conveyor belt system which has two sides '0' and '1' on each side of the conveyor, which will be running simultaneously there are 3 sensors. The first two sensors determine the size of the object on the conveyor. This is done by giving and output of 0 for no object a 1 for first sensor activation 2 for second sensor activation or 3 if a large object is on the conveyor therefore triggering both sensors. if a small object is detected then a gate that is 2.8 secs away from the second sensor needs to close for about 1 sec to allow the small object to be push off. If a large object is detected it should pass along the conveyor but the third sensor should be activated which comes after the gate, this is to count the number of large objects. The gate responds to 'setgates (1)' to close the '0' side and 'setgates (3)' to close the both gates at the same time, 'setgates (0)' raises the gate. Today i attempted one side of the conveyor and managed to get the the gate to operate knocking off small objects and letting large objects through using taskdelay after a sensor 2 activation to knock small blocks off and sensor 3 activation to make sure the gate is open for large objects. This is ok but has a limitation in that if there are two small ojects close together on the conveyor then the taskdelay would not be appropriate. This has lead me on to looking at something called watchtimer or watchdog :s Also these block need to be counts so for instance id like a = total blocks counted b = small block counted c = large blocks counted d = large block counted from third sensor as a checksum. Some of the coding has been done and i can call on those task from a separate .h file

    Those are:

    /* Sensor reading functions */
    char readSizeSensors(char conveyor);
    char readCountSensor(char conveyor);
    void resetSizeSensors(char conveyor);
    void resetCountSensor(char conveyor);
    /* Gate & motor control functions */
    void setGates(char state);
    void startMotor(void);
    void stopMotor(void);

    If you could assist me or point me in some direction it would be much apprecitated as it has given me quite a headache. I think i need to do it in separate functions as today i attempted to do it all in one function and the coding got quite long.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Breaking the problem down into separate functions is the ONLY way to fly.

    And it keeps your sanity (probably!).

    STUDY the process, so you REALLY know the state of every sensor, in every type of movement that the system can take, at every moment.

    Because that's what you will need to make your program work efficiently. You might think you know the system already, but you need to know the system at a LOW level, in absolutely EVERY detail.

    The program will be all about the state of these sensors, at any moment in time, and your program will need multiple flags (variables which will have only a few set values and nothing else), so you can have the program work correctly.

    Having a struct (record), with members (fields), just for these sensor status values, seems like a good idea, just now.

    Take some time to work up a pseudo code of how the program should work. Maybe start with a detailed plain English version, but then moving over to a more structured version, with smaller steps, for the pseudo code.

    Finally, get a general "backbone" of the program, putting off the details, and concentrating on the overall flow of the program, and it's functions. Test as you go forward, using the pseudo code, as your rough guide, but you'll outgrow and veer off from the pseudo code, so don't be alarmed when that happens.

    Be SURE to use variable names that, as you read them, you know what they are for. None of this: a,b,c,e,f,g stuff you see sometimes. That will drive you right 'round the bend!

    Make a drawing/diagram of this system, so you can look at it while you're programming, and better visualize the system, at that time. You'll need a test case with some packages that you move through the system (by hand calculation), so you can test if your program is on the right track or not for accuracy.

    You'll need to really test it thoroughly when you're done. These kinds of programs tend to have the "oh yeah! I forgot about when ..." syndrome, because of the complexity.

    Making a diagram in paint, of the system and it's parts, would be a help to anyone you were asking it of. Scale is not important, nor desired. Show the sensors, gates, belt, etc, even if it's just by symbols/boxes/colored boxes, etc.

    Very much a "Divide and Conquer" kind of approach will help you out.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    2,753
    This is ok but has a limitation in that if there are two small ojects close together on the conveyor then the taskdelay would not be appropriate. This has lead me on to looking at something called watchtimer or watchdog
    You do know that the normal use of a watchdog timer is to detect when your program stops running for some reason and resets the hardware. See this link for watchdog timer. I doubt you would want to use this to detect events.

    If the sizes of the large and small objects are different enough you may be able to use the length of time the object is detected to determine the size. Trying to use two sensors to detect length can be difficult unless you can insure that two or more of the small items can not trigger the two sensors at the same time.

    Jim

  6. #6
    LC9
    LC9 is offline
    Registered User
    Join Date
    Oct 2011
    Posts
    1
    Quote Originally Posted by Dixi View Post
    Basically I have a conveyor belt system which has two sides '0' and '1' on each side of the conveyor, which will be running simultaneously there are 3 sensors. The first two sensors determine the size of the object on the conveyor. This is done by giving and output of 0 for no object a 1 for first sensor activation 2 for second sensor activation or 3 if a large object is on the conveyor therefore triggering both sensors. if a small object is detected then a gate that is 2.8 secs away from the second sensor needs to close for about 1 sec to allow the small object to be push off. If a large object is detected it should pass along the conveyor but the third sensor should be activated which comes after the gate, this is to count the number of large objects. The gate responds to 'setgates (1)' to close the '0' side and 'setgates (3)' to close the both gates at the same time, 'setgates (0)' raises the gate. Today i attempted one side of the conveyor and managed to get the the gate to operate knocking off small objects and letting large objects through using taskdelay after a sensor 2 activation to knock small blocks off and sensor 3 activation to make sure the gate is open for large objects. This is ok but has a limitation in that if there are two small ojects close together on the conveyor then the taskdelay would not be appropriate. This has lead me on to looking at something called watchtimer or watchdog :s Also these block need to be counts so for instance id like a = total blocks counted b = small block counted c = large blocks counted d = large block counted from third sensor as a checksum. Some of the coding has been done and i can call on those task from a separate .h file

    Those are:

    /* Sensor reading functions */
    char readSizeSensors(char conveyor);
    char readCountSensor(char conveyor);
    void resetSizeSensors(char conveyor);
    void resetCountSensor(char conveyor);
    /* Gate & motor control functions */
    void setGates(char state);
    void startMotor(void);
    void stopMotor(void);

    If you could assist me or point me in some direction it would be much apprecitated as it has given me quite a headache. I think i need to do it in separate functions as today i attempted to do it all in one function and the coding got quite long.

    I've done same like this before, could you please PM (Private Message) me your full code ?! and I will distinguish it then get back to you with proper solution ?

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I wouldn't do that.

    You need to work through these problems, not off-load them to someone else. Especially not to someone you don't know.

    No offense LC9, (and welcome to the forum!), but it is your very first post here.

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    3
    Ha thanks for the welcome. Sorry for the delay in coming back on here I've spent the last 5 days trying to crack it. Ive decided that i would like to use FSM (finite state machine) to complete the task. Adak i did as you suggested and broke it down into blocks. I believe i have about ten states to consider and i guess it is just putting it into code from english and using various semaphores to carry out any relevant tasks. Does sound about right? As for an image i have drawn something out to show the hardware shall i upload the image onto this thread? As it stand i can get the gate to function now using a semaphore but getting it to work without it having an affect on anything else is tricky, as now it will knock the first small object off but whilst in the semaphore with a task delay in it, any small objects following behind in the period of the task delay gets missed by the gate. Im not entirely sure how i implemented it but using the timer watchdog the gate would do exactly what i wanted in that it was very responsive to open and closing the gate were appropriate. Just not if it will work on both sides of the conveyor, also as i was late at night when i got it working, i didn't save the code and like a nugget can't get it working again which suggests a fluke.

    LC9 thank you for replying what is your experience in c programming? I am still learning myself.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 06-07-2011, 03:30 PM
  2. C programming Language
    By ashiela in forum C Programming
    Replies: 11
    Last Post: 04-19-2010, 05:32 PM
  3. What's the Difference Between a Programming Language and a Scripting Language?
    By Krak in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 07-15-2005, 04:46 PM
  4. which programming language should be used for socket programming?
    By albert_wong_bmw in forum Networking/Device Communication
    Replies: 8
    Last Post: 06-04-2004, 08:12 PM
  5. Your first programming language?
    By Magos in forum A Brief History of Cprogramming.com
    Replies: 31
    Last Post: 12-02-2002, 10:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21