001// Copyright (c) FIRST and other WPILib contributors. 002// Open Source Software; you can modify and/or share it under the terms of 003// the WPILib BSD license file in the root directory of this project. 004 005package edu.wpi.first.wpilibj2.command.button; 006 007import edu.wpi.first.wpilibj.Joystick; 008import edu.wpi.first.wpilibj.event.EventLoop; 009import edu.wpi.first.wpilibj2.command.CommandScheduler; 010 011/** 012 * A version of {@link Joystick} with {@link Trigger} factories for command-based. 013 * 014 * @see Joystick 015 */ 016public class CommandJoystick extends CommandGenericHID { 017 private final Joystick m_hid; 018 019 /** 020 * Construct an instance of a controller. 021 * 022 * @param port The port index on the Driver Station that the controller is plugged into. 023 */ 024 public CommandJoystick(int port) { 025 super(port); 026 m_hid = new Joystick(port); 027 } 028 029 /** 030 * Get the underlying GenericHID object. 031 * 032 * @return the wrapped GenericHID object 033 */ 034 @Override 035 public Joystick getHID() { 036 return m_hid; 037 } 038 039 /** 040 * Constructs an event instance around the trigger button's digital signal. 041 * 042 * @return an event instance representing the trigger button's digital signal attached to the 043 * {@link CommandScheduler#getDefaultButtonLoop() default scheduler button loop}. 044 * @see #trigger(EventLoop) 045 */ 046 public Trigger trigger() { 047 return trigger(CommandScheduler.getInstance().getDefaultButtonLoop()); 048 } 049 050 /** 051 * Constructs an event instance around the trigger button's digital signal. 052 * 053 * @param loop the event loop instance to attach the event to. 054 * @return an event instance representing the trigger button's digital signal attached to the 055 * given loop. 056 */ 057 public Trigger trigger(EventLoop loop) { 058 return button(Joystick.ButtonType.kTrigger.value, loop); 059 } 060 061 /** 062 * Constructs an event instance around the top button's digital signal. 063 * 064 * @return an event instance representing the top button's digital signal attached to the {@link 065 * CommandScheduler#getDefaultButtonLoop() default scheduler button loop}. 066 * @see #top(EventLoop) 067 */ 068 public Trigger top() { 069 return top(CommandScheduler.getInstance().getDefaultButtonLoop()); 070 } 071 072 /** 073 * Constructs an event instance around the top button's digital signal. 074 * 075 * @param loop the event loop instance to attach the event to. 076 * @return an event instance representing the top button's digital signal attached to the given 077 * loop. 078 */ 079 public Trigger top(EventLoop loop) { 080 return button(Joystick.ButtonType.kTop.value, loop); 081 } 082 083 /** 084 * Set the channel associated with the X axis. 085 * 086 * @param channel The channel to set the axis to. 087 */ 088 public void setXChannel(int channel) { 089 m_hid.setXChannel(channel); 090 } 091 092 /** 093 * Set the channel associated with the Y axis. 094 * 095 * @param channel The channel to set the axis to. 096 */ 097 public void setYChannel(int channel) { 098 m_hid.setYChannel(channel); 099 } 100 101 /** 102 * Set the channel associated with the Z axis. 103 * 104 * @param channel The channel to set the axis to. 105 */ 106 public void setZChannel(int channel) { 107 m_hid.setZChannel(channel); 108 } 109 110 /** 111 * Set the channel associated with the throttle axis. 112 * 113 * @param channel The channel to set the axis to. 114 */ 115 public void setThrottleChannel(int channel) { 116 m_hid.setThrottleChannel(channel); 117 } 118 119 /** 120 * Set the channel associated with the twist axis. 121 * 122 * @param channel The channel to set the axis to. 123 */ 124 public void setTwistChannel(int channel) { 125 m_hid.setTwistChannel(channel); 126 } 127 128 /** 129 * Get the channel currently associated with the X axis. 130 * 131 * @return The channel for the axis. 132 */ 133 public int getXChannel() { 134 return m_hid.getXChannel(); 135 } 136 137 /** 138 * Get the channel currently associated with the Y axis. 139 * 140 * @return The channel for the axis. 141 */ 142 public int getYChannel() { 143 return m_hid.getYChannel(); 144 } 145 146 /** 147 * Get the channel currently associated with the Z axis. 148 * 149 * @return The channel for the axis. 150 */ 151 public int getZChannel() { 152 return m_hid.getZChannel(); 153 } 154 155 /** 156 * Get the channel currently associated with the twist axis. 157 * 158 * @return The channel for the axis. 159 */ 160 public int getTwistChannel() { 161 return m_hid.getTwistChannel(); 162 } 163 164 /** 165 * Get the channel currently associated with the throttle axis. 166 * 167 * @return The channel for the axis. 168 */ 169 public int getThrottleChannel() { 170 return m_hid.getThrottleChannel(); 171 } 172 173 /** 174 * Get the x position of the HID. 175 * 176 * <p>This depends on the mapping of the joystick connected to the current port. On most 177 * joysticks, positive is to the right. 178 * 179 * @return the x position 180 */ 181 public double getX() { 182 return m_hid.getX(); 183 } 184 185 /** 186 * Get the y position of the HID. 187 * 188 * <p>This depends on the mapping of the joystick connected to the current port. On most 189 * joysticks, positive is to the back. 190 * 191 * @return the y position 192 */ 193 public double getY() { 194 return m_hid.getY(); 195 } 196 197 /** 198 * Get the z position of the HID. 199 * 200 * @return the z position 201 */ 202 public double getZ() { 203 return m_hid.getZ(); 204 } 205 206 /** 207 * Get the twist value of the current joystick. This depends on the mapping of the joystick 208 * connected to the current port. 209 * 210 * @return The Twist value of the joystick. 211 */ 212 public double getTwist() { 213 return m_hid.getTwist(); 214 } 215 216 /** 217 * Get the throttle value of the current joystick. This depends on the mapping of the joystick 218 * connected to the current port. 219 * 220 * @return The Throttle value of the joystick. 221 */ 222 public double getThrottle() { 223 return m_hid.getThrottle(); 224 } 225 226 /** 227 * Get the magnitude of the vector formed by the joystick's current position relative to its 228 * origin. 229 * 230 * @return The magnitude of the direction vector 231 */ 232 public double getMagnitude() { 233 return m_hid.getMagnitude(); 234 } 235 236 /** 237 * Get the direction of the vector formed by the joystick and its origin in radians. 0 is forward 238 * and clockwise is positive. (Straight right is π/2.) 239 * 240 * @return The direction of the vector in radians 241 */ 242 public double getDirectionRadians() { 243 // https://docs.wpilib.org/en/stable/docs/software/basic-programming/coordinate-system.html#joystick-and-controller-coordinate-system 244 // A positive rotation around the X axis moves the joystick right, and a 245 // positive rotation around the Y axis moves the joystick backward. When 246 // treating them as translations, 0 radians is measured from the right 247 // direction, and angle increases clockwise. 248 // 249 // It's rotated 90 degrees CCW (y is negated and the arguments are reversed) 250 // so that 0 radians is forward. 251 return m_hid.getDirectionRadians(); 252 } 253 254 /** 255 * Get the direction of the vector formed by the joystick and its origin in degrees. 0 is forward 256 * and clockwise is positive. (Straight right is 90.) 257 * 258 * @return The direction of the vector in degrees 259 */ 260 public double getDirectionDegrees() { 261 return m_hid.getDirectionDegrees(); 262 } 263}