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