0

What type of machine you ask? A machine that measures wedge and roundness of lenses.

I've already written software for the machine and it is in production, but it is brittle and prone to lock ups when they dont do things in the right order. I'm trying to come up with the best way to architect it so that its stable and maintainable.

Here's the quick twenty second run down. There are two modes, setup and run. In setup mode the operator can manually move any of the 6 different motors using one of the 6 different momentary toggle switches on the control panel. They get everything homed in, then they turn it to run mode, load a lense, and press go. The machine will automatically bring in the three indicators, find the edge of the lense, and then rotate the spindle and measure all the way around the wedge.

I ended with a very poor non-design of having a class that raised an event that says when a switch has changed state, which switch it was, and what its new state is. Then I do a lot of ifs and stuff to determine what the machine can do. As you can imagine, this is very very bad.

Does anyone have any good ideas on how to structure this? I have some of my own, but I want to hear some from those that may be a little more experienced with this type of application development.

Max Schmeling
  • 12,363
  • 14
  • 66
  • 109
  • Any sort of example code / pseudo code will probably get you more detailed answers. – tgray Jun 15 '09 at 20:03
  • I'm looking for overall design of machine automation programs... Like should I encapsulate the switch panel and have a property for each switch/button and then hook commands up to the events of individual buttons, and put the logic in the commands about whether or not the command can execute and so on? – Max Schmeling Jun 15 '09 at 20:05
  • 1
    Should you "put the logic in the commands about whether or not the command can execute"? You are modeling a "command", like "go make me some toast". Is a "command" in real life something that encapsulates knowledge of the conditions under which that command can be performed? No. So don't model your program that way. You have three things -- the state of physical _mechanisms_, the _commands_ represented by user inputs via those mechanisms, and _policies_ that describe what legal commands are, and what their consequences are, given certain states. – Eric Lippert Jun 16 '09 at 01:09
  • 1
    In short: do not mix policy code with mechanism code. That makes it very difficult to change the mechanism code without accidentally changing the policy code, and vice versa. – Eric Lippert Jun 16 '09 at 01:10

2 Answers2

2

What you need is build a finite-state machine, that's the best way to model a problem like the one you have:

http://en.wikipedia.org/wiki/Finite_state_machine

tekBlues
  • 5,745
  • 1
  • 29
  • 32
0

I wouldn't even worry about figuring out what the system can do until the operator starts the machine.

Event listeners are fine, but just have them update a memory model of the switches with the current information. Make sure you catch all the events, IN ORDER....if there's a way to query the current state of the switches, that's even better - ignore all switching until the operator starts the machine, then query.

Either way, determine the operational mode at start time, based on the memory model of the switches. Make a copy of the memory model for operation to avoid any chance of it changing during operation.

sangretu
  • 1,208
  • 9
  • 12