Files
allwpilib/wpilibc/wpilibC++Devices/include/RobotDrive.h
James Kuszmaul 534ea134a4 artf4154: Get rid of raw pointers in C++.
This deals with the majority of the user-facing code
in wpilibC++Devices and a substantial portion of it in
wpilibC++. wpilibC++Sim and wpilibC++IntegrationTests
are untouched except where it is necessary to make them
work with the rest of the libraries.

There is still a lot to do in the following areas:
-The HAL (which we may not want to touch at all).
-The I2C, Serial, and SPI interfaces in wpilibC++Devices,
  which I haven't gotten around to doing yet.
-Most wpilibC++Devices classes have void* pointers
  for interacting with the HAL.
-InterruptableSensorBase passes a void *params for
  the interrupt handler.
-I haven't converted all the const char* to std::strings.
-There are plenty of other cases of raw pointers still
  existing.
-This doesn't fall directly under raw pointer stuff,
  but move syntax and rvalue references could be introduced
  in many places.
-I haven't touched vision code.
-The Resource classes conflict (one is in the hal, the other
  in wpilibC++). Someone should figure out a more
  permanent fix (eg, just renaming them), then doing
  what I did (making a new namespace for one of them,
  essentially the same as renaming it).

A few other things:
-I created a NullDeleter class which is marked as deprecated.
  What this does is it can be passed as the deleter to a
  std::shared_ptr so that when you are converting raw pointers
  to shared_ptrs the shared_ptr doesn't do any deletion if
  someone else owns the raw pointer. This should only be
  used in making old raw pointer UIs.
-I had to alter the build.gradle so that it did not
  emit errors when deprecated functions called deprecated
  functions. Unfortunately, gradle doesn't appear to be
  actually printing out gcc warnigns for some reason.
  The best way I have found to fix this is to patch
  the toolchains (https://bitbucket.org/byteit101/toolchain-builder/pull-request/5/make-gcc-not-throw-warnings-for-nested/diff)
  so that a deprecated function calling a deprecated
  function is fine but a non-deprecated function calling
  a deprecated function will throw a warning (which we
  then elevate with -Werror). I believe that clang
  deals with this properly, although I have not
  tried it myself.

Change-Id: Ib8090c66893576fe73654f4e9d268f9d37be06a2
2015-07-20 13:18:29 -04:00

132 lines
5.6 KiB
C++

/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008. All Rights Reserved.
*/
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
/*----------------------------------------------------------------------------*/
#pragma once
#include "ErrorBase.h"
#include <stdlib.h>
#include <memory>
#include <sstream>
#include "HAL/HAL.hpp"
#include "MotorSafety.h"
#include "MotorSafetyHelper.h"
class SpeedController;
class GenericHID;
/**
* Utility class for handling Robot drive based on a definition of the motor
* configuration.
* The robot drive class handles basic driving for a robot. Currently, 2 and 4
* motor tank and
* mecanum drive trains are supported. In the future other drive types like
* swerve might be
* implemented. Motor channel numbers are passed supplied on creation of the
* class. Those
* are used for either the Drive function (intended for hand created drive code,
* such as
* autonomous) or with the Tank/Arcade functions intended to be used for
* Operator Control
* driving.
*/
class RobotDrive : public MotorSafety, public ErrorBase {
public:
enum MotorType {
kFrontLeftMotor = 0,
kFrontRightMotor = 1,
kRearLeftMotor = 2,
kRearRightMotor = 3
};
RobotDrive(uint32_t leftMotorChannel, uint32_t rightMotorChannel);
RobotDrive(uint32_t frontLeftMotorChannel, uint32_t rearLeftMotorChannel,
uint32_t frontRightMotorChannel, uint32_t rearRightMotorChannel);
[[deprecated("Raw pointers are deprecated; use shared_ptr instead.")]]
RobotDrive(SpeedController *leftMotor, SpeedController *rightMotor);
[[deprecated("References are deprecated; use shared_ptr instead.")]]
RobotDrive(SpeedController &leftMotor, SpeedController &rightMotor);
RobotDrive(::std::shared_ptr<SpeedController> leftMotor,
::std::shared_ptr<SpeedController> rightMotor);
[[deprecated("Raw pointers are deprecated; use shared_ptr instead.")]]
RobotDrive(SpeedController *frontLeftMotor, SpeedController *rearLeftMotor,
SpeedController *frontRightMotor, SpeedController *rearRightMotor);
[[deprecated("References are deprecated; use shared_ptr instead.")]]
RobotDrive(SpeedController &frontLeftMotor, SpeedController &rearLeftMotor,
SpeedController &frontRightMotor, SpeedController &rearRightMotor);
RobotDrive(::std::shared_ptr<SpeedController> frontLeftMotor,
::std::shared_ptr<SpeedController> rearLeftMotor,
::std::shared_ptr<SpeedController> frontRightMotor,
::std::shared_ptr<SpeedController> rearRightMotor);
virtual ~RobotDrive() = default;
void Drive(float outputMagnitude, float curve);
void TankDrive(GenericHID *leftStick, GenericHID *rightStick,
bool squaredInputs = true);
void TankDrive(GenericHID &leftStick, GenericHID &rightStick,
bool squaredInputs = true);
void TankDrive(GenericHID *leftStick, uint32_t leftAxis,
GenericHID *rightStick, uint32_t rightAxis,
bool squaredInputs = true);
void TankDrive(GenericHID &leftStick, uint32_t leftAxis,
GenericHID &rightStick, uint32_t rightAxis,
bool squaredInputs = true);
void TankDrive(float leftValue, float rightValue, bool squaredInputs = true);
void ArcadeDrive(GenericHID *stick, bool squaredInputs = true);
void ArcadeDrive(GenericHID &stick, bool squaredInputs = true);
void ArcadeDrive(GenericHID *moveStick, uint32_t moveChannel,
GenericHID *rotateStick, uint32_t rotateChannel,
bool squaredInputs = true);
void ArcadeDrive(GenericHID &moveStick, uint32_t moveChannel,
GenericHID &rotateStick, uint32_t rotateChannel,
bool squaredInputs = true);
void ArcadeDrive(float moveValue, float rotateValue,
bool squaredInputs = true);
void MecanumDrive_Cartesian(float x, float y, float rotation,
float gyroAngle = 0.0);
void MecanumDrive_Polar(float magnitude, float direction, float rotation);
void HolonomicDrive(float magnitude, float direction, float rotation);
virtual void SetLeftRightMotorOutputs(float leftOutput, float rightOutput);
void SetInvertedMotor(MotorType motor, bool isInverted);
void SetSensitivity(float sensitivity);
void SetMaxOutput(double maxOutput);
void SetCANJaguarSyncGroup(uint8_t syncGroup);
void SetExpiration(float timeout) override;
float GetExpiration() const override;
bool IsAlive() const override;
void StopMotor() override;
bool IsSafetyEnabled() const override;
void SetSafetyEnabled(bool enabled) override;
void GetDescription(std::ostringstream& desc) const override;
protected:
void InitRobotDrive();
float Limit(float num);
void Normalize(double *wheelSpeeds);
void RotateVector(double &x, double &y, double angle);
static const int32_t kMaxNumberOfMotors = 4;
float m_sensitivity = 0.5;
double m_maxOutput = 1.0;
::std::shared_ptr<SpeedController> m_frontLeftMotor = nullptr;
::std::shared_ptr<SpeedController> m_frontRightMotor = nullptr;
::std::shared_ptr<SpeedController> m_rearLeftMotor = nullptr;
::std::shared_ptr<SpeedController> m_rearRightMotor = nullptr;
std::unique_ptr<MotorSafetyHelper> m_safetyHelper;
uint8_t m_syncGroup = 0;
private:
int32_t GetNumMotors() {
int motors = 0;
if (m_frontLeftMotor) motors++;
if (m_frontRightMotor) motors++;
if (m_rearLeftMotor) motors++;
if (m_rearRightMotor) motors++;
return motors;
}
DISALLOW_COPY_AND_ASSIGN(RobotDrive);
};