# Solenoid 氣動
## 單電磁閥 v.s 雙電磁閥
雙電磁閥適用於所有情況。如果停電時機器人的電磁閥位置與原本不同,此就必須要使用雙電磁閥。(例如:原電磁閥位置:0 停電時電磁閥位置:-1)除了此情況其他也可以用單電磁閥。
:::warning
我們都是用雙電磁閥。
:::
資料來源:https://www.chiefdelphi.com/t/double-vs-single-solenoids-when-to-use-which/343735/4
> **Single Solenoids**
Single solenoids in WPILib are controlled using the Solenoid class. To construct a Solenoid object, simply pass the desired port number (assumes CAN ID 0) or CAN ID and port number to the constructor. To set the value of the solenoid call set(true) to enable or set(false) to disable the solenoid output.
**Double Solenoids**
Double solenoids are controlled by the DoubleSolenoid class in WPILib. These are constructed similarly to the single solenoid but there are now two port numbers to pass to the constructor, a forward channel (first) and a reverse channel (second). The state of the valve can then be set to kOff (neither output activated), kForward (forward channel enabled 啟用正向通道) or kReverse (reverse channel enabled 啟用反向通道). Additionally, the PCM CAN ID can be passed to the DoubleSolenoid if teams have a non-standard PCM CAN ID.
資料來源:https://docs.wpilib.org/en/stable/docs/software/actuators/pneumatics.html
## 導入函式庫
```java=
import edu.wpi.first.wpilibj.DoubleSolenoid;
import edu.wpi.first.wpilibj.DoubleSolenoid.Value;
```
## 宣告空壓機和電磁閥
```java=
Compressor compressor = new Compressor(0);//空壓機 Compressor
DoubleSolenoid suckerS = new DoubleSolenoid(0, 1);//電磁閥 Solenoid
// DoubleSolenoid name = new DoubleSolenoid(往前, 往後)
```
## 布林值
```java=
public boolean collectorContinous = false ;
```
> In FRC®, pressure is created using a pneumatic compressor and stored in pneumatic tanks. The compressor doesn’t necessarily have to be on the robot, but must be powered by the robot’s PCM(s). The “Closed Loop” mode on the Compressor is enabled by default, and it is not recommended that teams change this setting. When closed loop control is enabled the PCM will automatically turn the compressor on when the pressure switch is closed (below the pressure threshold) and turn it off when the pressure switch is open (~120PSI). When closed loop control is disabled the compressor will not be turned on. Using a Compressor, users can query the status of the compressor. The state (currently on or off), pressure switch state, and compressor current can all be queried from the Compressor object.
## 初始化
```java=
@Override
public void robotInit() {
compressor.start();
collectorContinous = false;
}
```
:::success
由於DoubleSolenoid默認為關閉,因此必須先設置它才能切換。
:::
## 控制電磁閥
```java=
@Override
public void teleopPeriodic() {
if(js1.getRawButtonPressed(Btn_A)){
collectorContinous= !collectorContinous;
if(collectorContinous){
suckerS.set(Value.kReverse);
}else{
suckerS.set(Value.kForward);
}
Timer.delay(0.1);
}
}
```
## 其他的寫法
```java=
import edu.wpi.first.wpilibj.DoubleSolenoid;
import edu.wpi.first.wpilibj.DoubleSolenoid.Value;
DoubleSolenoid pneumatic = new DoubleSolenoid(13, 0,1); //氣壓電磁閥
// DoubleSolenoid name = new DoubleSolenoid(port, 往前, 往後)
Compressor comp = new Compressor(); //打氣
boolean SolenoidIsClick = false; //氣壓電磁閥是否打開
@Override
public void teleopInit() {
//氣壓電磁閥初始化
comp.start();
}
@Override
public void teleopPeriodic() {
//氣壓電磁閥控制
if(js1.getRawButton(intakeSolenoid) == true){
pneumatic.set(Value.kReverse);
SolenoidIsClick = false;
}else if (js1.getRawButton(intakeOutSolenoid)){
pneumatic.set(Value.kForward);
SolenoidIsClick = true;
}
```
## Reference
- [Class DoubleSolenoid](https://first.wpi.edu/wpilib/allwpilib/docs/release/java/edu/wpi/first/wpilibj/DoubleSolenoid.html)
- [Class Compressor](https://first.wpi.edu/FRC/roborio/beta/docs/java/edu/wpi/first/wpilibj/Compressor.html)
###### tags: `程式組教程`