Is there any way to make an arduino interact with cpp code? For context we are using a swerve drive base this year for the first time and we want to have a second driver with a set of buttons for some scoring. I have spent the last day trying to get the arduino to send signals to the c++ code in order to send commands to the robot. I am currently trying to use the Arduino joystick library to get the arduino to act as a controller but its not a library made for frc. GitHub - MHeironimus/ArduinoJoystickLibrary: An Arduino library that adds one or more joysticks to the list of HID devices an Arduino Leonardo or Arduino Micro can support.
The device will need to show up in the DS as a joystick in order for robot code to see it. In the DS joysticks view, does the device appear, and do any of the button indicators / axes indicators show anything when you change the inputs to the arduino? What does your arduino code look like?
1 Like
gixxy
January 14, 2023, 1:44pm
3
Like said above, you just need the Arduino to emulate a Joystick HID Device. Which you can absolutely do with that library listed as long as you are using a compatible Arduino based on the ATmega32u4 chip, ie. Leonardo, Micro, Pro Micro as instructed in the library’s Readme.
This is the code for the custom Gamepad that 3468 made for Infinite Recharge. Using the same Library you listed on an Arduino Pro Micro:
// 3468 MAGNATech's Custom Gamepad for the 2020 FRC Game Infinite Recharge
// For use with the Arduino Pro Micro (and other ATmega32u4 Microcontrollers)
// This sketch makes use of the following Libraries:
// https://github.com/MHeironimus/ArduinoJoystickLibrary/tree/version-2.0
// Written by: Gustave Michel III <gustave@michel.com>
#include <Joystick.h>
// Pin Definitions
#define LAUNCHER_THROTTLE_PIN A3
#define LAUNCHER_MANUAL_PIN 15 // No Pullup
#define LAUNCER_AUTO_PIN 14 // Pullup, Invert
#define COLOR_WHEEL_RAISE_PIN 10 // No Pullup
#define LIFT_HOOK_RAISE_PIN 16 // No Pullup
#define COLOR_WHEEL_FORWARD_PIN 3 // Pullup, Invert
#define COLOR_WHEEL_REVERSE_PIN 2 // Pullup, Invert
#define LIFT_FORWARD_PIN 5 // Pullup, Invert
#define LIFT_REVERSE_PIN 4 // Pullup, Invert
#define CONVEYOR_FORWARD_PIN 6 // Pullup, Invert
#define CONVEYOR_REVERSE_PIN 7 // Pullup, Invert
This file has been truncated. show original
And while its Java, the process should be basically the same in C++, this is the code utilizing the custom gamepad, the same as any other generic Joystick:
* Use this method to define your button->command mappings. Buttons can be
* created by instantiating a {@link GenericHID} or one of its subclasses
* ({@link edu.wpi.first.wpilibj.Joystick} or {@link XboxController}), and then
* passing it to a {@link edu.wpi.first.wpilibj2.command.button.JoystickButton}.
*/
private void configureButtonBindings() {
// Driver Controller Buttons
JoystickButton intakeButton = new JoystickButton(driverController, DriverControllerConstants.intakeButton);
JoystickButton launchButton = new JoystickButton(driverController, DriverControllerConstants.launchButton);
// Override Controller Buttons
JoystickButton IntakeBallIntakeOverrideButton = new JoystickButton(overrideController,
OverrideControllerConstants.intakeBallIntakeButton);
JoystickButton exhaustBallIntakeOverrideButton = new JoystickButton(overrideController,
OverrideControllerConstants.exhaustBallIntakeButton);
JoystickButton advanceConveyorOverrideButton = new JoystickButton(overrideController,
OverrideControllerConstants.advanceConveyorButton);
JoystickButton retreatConveyorOverrideButton = new JoystickButton(overrideController,
OverrideControllerConstants.retreatConveyorButton);
JoystickButton setLauncherVelocityOverrideButton = new JoystickButton(overrideController,
OverrideControllerConstants.launcherVelocityButton);
and this is what the custom gamepad looked like
2 Likes