📄 servodamper.java
字号:
package net.sf.dz.device.actuator.impl;import java.io.IOException;import java.util.HashSet;import java.util.Iterator;import java.util.Set;import org.freehold.jukebox.logger.LogChannel;import org.freehold.servomaster.device.model.Servo;import org.freehold.servomaster.device.model.ServoController;import org.freehold.servomaster.device.model.transform.LinearTransformer;import org.freehold.servomaster.device.model.transform.Reverser;import org.freehold.servomaster.device.model.transition.CrawlTransitionController;import net.sf.dz.util.ObjectFactory;/** * Damper controlled by a RC Servo. * * The configuration elements read are the servo controller class and the * servo name. * */public class ServoDamper extends AbstractDamper { public static final LogChannel CH_ST = new LogChannel("ServoDamper"); /** * Set of controllers we know about. */ private static Set controllerSet = new HashSet(); private Servo servo; public ServoDamper() { } protected void configure() throws Throwable { super.configure(); String controllerClass = getConfiguration().getString(getConfigurationRoot() + ".controller.class"); String controllerPort = getConfiguration().getString(getConfigurationRoot() + ".controller.port"); String servoID = getConfiguration().getString(getConfigurationRoot() + ".controller.id"); boolean reverse = getConfiguration().getBoolean(getConfigurationRoot() + ".reverse.enabled", false); boolean crawl = getConfiguration().getBoolean(getConfigurationRoot() + ".crawl.enabled", false); complain(LOG_DEBUG, CH_ST, getConfigurationRoot() + ".controller.class: " + controllerClass); try { synchronized ( controllerSet ) { for ( Iterator i = controllerSet.iterator(); i.hasNext(); ) { ServoController c = (ServoController)i.next(); if ( c.getClass().getName().equals(controllerClass) ) { // Let's find out if this controller works on the same port if ( c.getPort().equals(controllerPort) ) { // Cool, we have the controller of the same class on the // same port complain(LOG_DEBUG, CH_ST, "Found existing " + controllerClass + " on " + controllerPort); servo = c.getServo(servoID); return; } } } // If we're here, it means that no suitable controller was found, // thus we'll have to instantiate it. complain(LOG_DEBUG, CH_ST, "Instantiating " + controllerClass + " on " + controllerPort); ServoController c = (ServoController)ObjectFactory.instantiate(controllerClass, ServoController.class); c.init(controllerPort); // VT: FIXME: FT639 HACK: // Default setting for FT639 is 90 degree range. This is not // enough for at least the servo damper described at // http://diy-zoning.sourceforge.net/docs/teasers/damper.html // - 180 degrees is more like it. if ( c instanceof org.freehold.servomaster.device.impl.ft.FT639ServoController ) { ((org.freehold.servomaster.device.impl.ft.FT639ServoController)c).setRange(true); } // VT: FIXME: For other controllers, there will be other // hacks. For example, Phidget has a nice feature allowing // to set the servo operating range in a very gentle and // precise way, and it would be a really good idea to make // use of it. Just a little bit later. try { c.setSilentMode(true); } catch ( UnsupportedOperationException ex ) { complain(LOG_NOTICE, CH_ST, "Controller doesn't support the silent mode"); } controllerSet.add(c); servo = c.getServo(servoID); } } finally { if ( servo != null ) { if ( false ) { // VT: FIXME: This slows down the startup by 15 seconds // and possibly annoys my wife if she's sleeping while // I'm debugging the app, so the hell with it so far. // May need to enable it in a final setup. servo.setPosition(0); Thread.sleep(1000); servo.setPosition(1); Thread.sleep(1000); } // This is a safe position - if by chance there's a logic or // wiring problem, we're not going to end up blowing A/C // into all closed throttles. servo.setPosition(0.5); if ( crawl ) { servo.attach(new CrawlTransitionController()); } servo = new LinearTransformer(servo); if ( reverse ) { servo = new Reverser(servo); } } } } public void moveDamper(double throttle) { try { if ( servo.getPosition() != throttle ) { complain(LOG_DEBUG, CH_ST, servo.getName() + ": " + throttle); } servo.setPosition(throttle); } catch ( Throwable t ) { complain(LOG_ERR, CH_ST, "Can't move throttle:", t); } } public Set getControllerSet() { return controllerSet; } public String getServoName() { return servo.getName(); } /** * Get the servo. * * FIXME: This is a hack to allow a space-saving display for the servo * controller status. */ public Servo getServo() { return servo; } public double getThrottle() throws IOException { return servo.getPosition(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -