Thread: Need suggestions for control/variable storage structures

  1. #1
    Registered User Sergeant82d's Avatar
    Join Date
    Mar 2011
    Location
    Oklahoma
    Posts
    2

    Need suggestions for control/variable storage structures

    I am working on a embedded project using a Microchip PIC, a Radio Controlled robot lawnmower. I live on a farm and have a very large yard, you see - which I hope to eventually make autonomous. I want to be sure I make the program extensible. I have the basic functionality down (finally!), and am trying to expand my programming ability by incorporating additional features.

    I am including a variety of error messages & display LEDs, for one thing, and am trying to figure out a logical hierarchy for controlling them. I will be using one or more Maxim MAX7221 SPI LED Controllers, and I plan to use one segment line from the '7221 per sub-system. I have the hardware working, no problems there, but I want to organize the control logic so it is uniform and expandable.

    I am thinking about an array of structs, each made up of three other struct arrays, as follows:

    Code:
    #define NUMBER_OF_MOTORS      4 
    #define NUMBER_OF_ENGINES     3 
    #define NUMBER_OF_MCUS        7 
    
    struct MOTOR[ NUMBER_OF_MOTORS ] { 
       temperature : 16 
       speed : 16 
       voltage : 8 
       current : 8 
       is_running : 1 
       is_over_voltage : 1 
       is_over_current : 1 
       is_over_temperature : 1 
       is_over_speed : 1 
       has_CAN_comms : 1 
       } 
    
    struct ENGINE[ NUMBER_OF_ENGINES ] { 
       temperature : 16 
       speed : 16 
    //   voltage : 8 
    //   current : 8 
       is_running : 1 
    //   over_voltage : 1 
    //   over_current : 1 
       is_over_temperature : 1 
       is_over_speed : 1 
       has_CAN_comms : 1 
       } 
    
    struct MCU[ NUMBER_OF_MCUS ] { 
       temperature : 16 
       voltage : 8 
       current : 8 
       has_power : 1 
       is_over_voltage : 1 
       is_over_current : 1 
       is_over_temperature : 1 
       has_CAN_comms : 1 
       } 
    
    struct LOCATION[ item_location ] {   // e.g.   front_left.motor.temperature = 235 
       struct MOTOR[]                    // e.g.   middle_engine.mcu.has_power = TRUE 
       struct ENGINE[]                   // e.g.   master_mcu.has_CAN_comms = TRUE 
       struct MCU[]                      // e.g.   right_rear.motor.speed = 1250                  // Captured pulses 
       } 
    
    //========================================== 
    
    typedef enum { 
       rear_left, 
       front_left, 
       front_right, 
       rear_right, 
       left_engine, 
       middle_engine, 
       right_engine, 
       master_mcu, 
       engine_mcu, 
       contact_sensor_mcu 
       }; item_location
    While this looks like it will work, it also looks like it will take up a lot of RAM. I have (a relatively large amount!) 3.8KB, but am already using half of it. Again, this is not a show stopper, as I am looking for something that works, but I also would like something that makes sense.

    The actual members of the structs are not important as I am certain they will change as the execution is refined, but does this look like a reasonable solution?

    Thanks for your time,
    Brad
    Last edited by Sergeant82d; 05-06-2011 at 06:08 AM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Sergeant82d View Post
    I am working on a embedded project using a Microchip PIC, a Radio Controlled robot lawnmower.
    I want one too!!!! I hope you also have a large fence, btw

    Having never programmed an LED controller, I dunno how these fields are used. Why do you need 16-bit numbers for temp and speed? Isn't that a bit much?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    This could be a lot of work if you have to keep downloading to hardware each time.
    Abstraction (computer science) - Wikipedia, the free encyclopedia

    Think about how you might prototype this on your PC.
    For example,
    Code:
    #ifdef PC
    void turnMotor ( int motorID, int direction, int numSteps ) {
      printf("Turning motor %d in direction %d for %d steps\n", motorID, direction, numSteps );
    }
    #else
    void turnMotor ( int motorID, int direction, int numSteps ) {
      // here you write some bits to some I/O ports on the device to cause
      // a motor to turn.
    }
    #endif
    Sensors are a little trickier; simple tests could be
    Code:
    #ifdef PC
    // version 2 could read from a file of actual data recorded from
    // your real device
    int readTemperature ( int sensorID ) {
      static int samples[] = { 10, 11, 12, 3, 99, 12, 12, 13 };
      static int count = 0;
      int result = samples[count];
      count = ( count + 1 ) % 8;
      return result;
    }
    #else
    int readTemperature ( int sensorID ) {
      // do something with a sensor, and return value
    }
    #endif
    By creating a lot of little functions like these to describe the environment on the hardware, you should be able to get a LOT of testing done quite quickly on your PC. When you're happy with some progress, then recompile it for the target.

    > I want to be sure I make the program extensible.
    If you're that short on resources, go for something simple that works.
    Extensibility comes with price tags (more CPU, more memory, more time to develop).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User Sergeant82d's Avatar
    Join Date
    Mar 2011
    Location
    Oklahoma
    Posts
    2
    @ MK27 - I expect temps - at least on the engne - to be greater than 255; and the speed will be counting pulses from a gear-tooth sensor, which could be as high as 400 RPM x 64 pulses per revolution at full speed. Not while mowing, mind you... just while I'm joy-riding on it! To see my inspiration for this, see here: http://www.rediculouslygoodlooking.com/

    @Salem - I do sincerely appreciate the advice regarding prototyping on the PC, but loading the firmware is not a problem... figuring out what to put in, and how to organize it is. I have been researching error handling, but have not located anything useful on writing a coherant overview of data.

    Any further advice gratefully accepted!


    // edited to add: I am using a number of functions such as you mention for sensors to collect data/command hardware; I really need help in storing and presenting the data in a comprehensive format. I apologize if I was not clear.
    Last edited by Sergeant82d; 05-07-2011 at 05:03 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-09-2009, 11:06 AM
  2. Suggestions to practice with data structures
    By smoking81 in forum C Programming
    Replies: 2
    Last Post: 03-31-2008, 01:57 PM
  3. COntrol structures and functions questions
    By angelicscars in forum C Programming
    Replies: 1
    Last Post: 11-21-2005, 11:50 AM
  4. control structures
    By wind_lark in forum C Programming
    Replies: 16
    Last Post: 11-21-2005, 01:10 AM
  5. float/double variable storage and precision
    By cjschw in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2003, 06:23 PM