BakeoutController-Basic  0.1
A Controller for the Omicron vacuum chamber
TDKLambdaPowerSupply.java
1 package devices;
2 
5 import org.slf4j.Logger;
6 import org.slf4j.LoggerFactory;
7 
8 import java.io.IOException;
9 import java.util.concurrent.locks.Lock;
10 import java.util.concurrent.locks.ReentrantLock;
11 
15 public class TDKLambdaPowerSupply extends AbstractRS232Device
16  implements PowerSupply {
17 
22  private static final Lock portCommunicatorLock = new ReentrantLock();
23 
27  private static final Logger log = LoggerFactory.getLogger(
29  );
30 
34  private final int deviceAddress;
35 
50  PortCommunicator portCommunicator,
51  int deviceAddress
52  ) throws IOException {
53  super(portCommunicator);
54  this.deviceAddress = deviceAddress;
55  this.startDevice();
56  }
57 
61  @Override public int getDeviceAddress(){
62  return this.deviceAddress;
63  }
64 
69  @Override public Double getVoltage() throws IOException {
70  return this.writeWithDoubleResponse(GET_VOLTAGE_COMMAND);
71  }
72 
77  @Override public Double getMeasuredVoltage() throws IOException {
78  return this.writeWithDoubleResponse(GET_MEASURED_VOLTAGE_COMMAND);
79  }
80 
86  @Override public void setVoltage(double newVoltage) throws IOException {
87  String commandToWrite = String.format(
89  );
90  this.writeWithOKResponse(commandToWrite);
91  }
92 
97  @Override public void reset() throws IOException {
98  this.writeWithOKResponse(RESET_COMMAND);
99  }
100 
107  @Override public Double getCurrent() throws IOException {
108  return this.writeWithDoubleResponse(GET_CURRENT_COMMAND);
109  }
110 
115  @Override public void setCurrent(double newCurrent) throws IOException {
116  String commandToWrite = String.format(
117  PowerSupply.SET_CURRENT_COMMAND, newCurrent
118  );
119 
120  this.writeWithOKResponse(commandToWrite);
121  }
122 
128  @Override public void outputOff() throws IOException {
129  String commandToWrite = String.format(
131  );
132 
133  this.writeWithOKResponse(commandToWrite);
134  }
135 
141  @Override public void outputOn() throws IOException {
142  String command = String.format(
144  );
145 
146  this.writeWithOKResponse(command);
147  }
148 
156  private void startDevice() throws IOException {
157  String commandToWrite = String.format(GET_ADDRESS_COMMAND,
158  deviceAddress);
159 
160  this.writeWithOKResponse(commandToWrite);
161  }
162 
173  private Double writeWithDoubleResponse(String commandToWrite) throws
174  IOException {
175  portCommunicatorLock.lock();
176 
177  this.write(commandToWrite);
178  log.debug("Writing command {} to power supply.", commandToWrite);
179  String response = this.read();
180  log.debug("Received response {} from power supply", response);
181 
182  portCommunicatorLock.unlock();
183 
184  return Double.parseDouble(response);
185  }
186 
192  private void writeWithOKResponse(String commandToWrite) throws
193  IOException {
194  portCommunicatorLock.lock();
195 
196  this.write(commandToWrite);
197  log.debug(
198  String.format(
199  "Writing command %s to power supply",
200  commandToWrite
201  )
202  );
203  String response = this.read();
204  log.debug(
205  String.format(
206  "Received response %s from power supply",
207  response
208  )
209  );
210 
211  if (!response.equals(PowerSupply.OK_RESPONSE)){
212  throw new ResponseNotOKException("Did not receive response of " +
213  String.format("%s", PowerSupply.OK_RESPONSE));
214  }
215 
216  portCommunicatorLock.unlock();
217  }
218 }
void setVoltage(double newVoltage)
void setCurrent(double newCurrent)
TDKLambdaPowerSupply(PortCommunicator portCommunicator, int deviceAddress)
Git Repo