diff --git a/Makefile b/Makefile index e8c812a..e84fc92 100644 --- a/Makefile +++ b/Makefile @@ -27,7 +27,7 @@ EXCLUDE_COLD_LIBRARIES:= IS_LIBRARY:=1 # TODO: CHANGE THIS! LIBNAME:=greenhat -VERSION:=1.0.5 +VERSION:=1.1.0 # EXCLUDE_SRC_FROM_LIB= $(SRCDIR)/unpublishedfile.c # this line excludes opcontrol.c and similar files EXCLUDE_SRC_FROM_LIB+=$(foreach file, $(SRCDIR)/main,$(foreach cext,$(CEXTS),$(file).$(cext)) $(foreach cxxext,$(CXXEXTS),$(file).$(cxxext))) diff --git a/README.md b/README.md index 35343de..9633909 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Greenhat is a library that makes programming the chassis of a vex v5 robot a pie ## Installing greenhat 1. Download the most recent [template](https://github.com/Marsgate/greenhatlib/releases) -2. Run this command from terminal `prosv5 c fetch greenhat@1.0.5.zip` +2. Run this command from terminal `prosv5 c fetch greenhat@1.1.0.zip` 3. `cd` into your pros project directory in your terminal 4. Apply the library to the project `prosv5 c apply greenhat` 5. Put `#include "greenhat/api.h"` in your main.h @@ -21,11 +21,11 @@ All configuration of the library will happen in the file `greenhat/config.h`. Th //negative numbers mean reversed motor #define LEFT_MOTORS 1, 2 #define RIGHT_MOTORS -3, -4 -#define GEARSET green +#define GEARSET 200 ``` This library was designed with the standard tank drives in mind. -If your drive is using torque or turbo motors, you need to change the gearset to `red` or `blue` respectivly. +If your drive is using torque or turbo motors, you need to change the gearset to `100` or `600` respectively. If you have an X-drive, mecanum drive, an H-drive, or any other non-standard type of drive, this library won't work without a bit of a makeover. Your options are to either modify this library, or write your own drive code (sorry). @@ -69,13 +69,17 @@ Near the top of config.h there are two important constants we need to adjust. `DISTANCE_CONSTANT` correlates to the `drive()` function and `DEGREE_CONSTANT` correlates to the `turn()` function. To tune the constants, tell the robot to drive 1 unit in autonomous. -```drive(1)``` +``` +drive(1) +``` Adjust the distance constant until 1 unit equals whatever real life equivalent. When you tell the robot to `drive(2.5);`, the distance constant determines if the robot drives 2.5 meters, 2.5 feet, 2.5 inches, or some other distance. If the robot was overshooting the target distance, then reduce the distance constant and try again. If the robot was undershooting, increase the distance constant. Don’t forget to re-upload the program each time you change the constant. The next thing to tune is the turns. Set the robot to turn 90 degrees in autonomous -```turn(90);``` +``` +turn(90); +``` Go through the same trial and error process, except this time change the degree constant instead of the distance constant. A good way to test if your robot is turning exactly 90 is to run the auton four times and see if the robot does a near-perfect 360. If it's within a couple degrees, it's good enough for most autons. @@ -122,7 +126,7 @@ In autonomous, if you want to move the drive for duration of time rather than a `timeDrive(1000, 50);` The first values is the duration of the movement in milliseconds, and the second values is the top speed. It is optional and defaults to 100 when omitted. -If you want to send different power to each side of the drive, call the function with two seperate speeds. +If you want to send different power to each side of the drive, call the function with two separate speeds. `timeDrive(1000, 50, 80);` This will produce a curved movement. @@ -133,7 +137,7 @@ The previously covered drive movements will be good enough for most applications To start performing a drive movement asynchronously, use the following functions: `driveAsync(3)` `turnAsync(90)` -These functions will not block the program during their execution so you can prefrom other tasks while in motion. +These functions will not block the program during their execution so you can perform other tasks while in motion. Here is an example ``` driveAsync(3); diff --git a/greenhat@1.0.5.zip b/greenhat@1.0.5.zip deleted file mode 100644 index c07944f..0000000 Binary files a/greenhat@1.0.5.zip and /dev/null differ diff --git a/greenhat@1.1.0.zip b/greenhat@1.1.0.zip new file mode 100644 index 0000000..3e185c6 Binary files /dev/null and b/greenhat@1.1.0.zip differ diff --git a/include/greenhat/config.h b/include/greenhat/config.h index c61ae73..7fbe4e3 100644 --- a/include/greenhat/config.h +++ b/include/greenhat/config.h @@ -6,9 +6,7 @@ namespace greenhat{ //negative numbers mean reversed motor #define LEFT_MOTORS 1, 2 #define RIGHT_MOTORS -3, -4 -#define GEARSET green - -#define GYRO_PORT 0 //gyro (set to 0 if not using) +#define GEARSET 200 // rpm of drive motors #define DISTANCE_CONSTANT 273 //ticks per distance unit, the default is a foot #define DEGREE_CONSTANT 2.3 //ticks per degree diff --git a/include/greenhat/drive.h b/include/greenhat/drive.h index 4604432..3c2e947 100644 --- a/include/greenhat/drive.h +++ b/include/greenhat/drive.h @@ -2,6 +2,7 @@ #define _DRIVE_H_ #include "okapi/api.hpp" +#include "greenhat/config.h" namespace greenhat{ @@ -106,9 +107,23 @@ void tank(int left, int right); void arcade(int vertical, int horizontal); /** - * Start the drive tasks and calibrate the Imu if it is being used - */ -void initDrive(); + * initialize the drive + */ +void initDrive( + std::initializer_listleftMotors = {LEFT_MOTORS}, + std::initializer_listrightMotors = {RIGHT_MOTORS}, + int gearset = GEARSET, + int distance_constant = DISTANCE_CONSTANT, + double degree_constant = DEGREE_CONSTANT, + int accel_step = ACCEL_STEP, + int deccel_step = DECCEL_STEP, + int arc_step = ARC_STEP, + double driveKP = DRIVE_KP, + double driveKD = DRIVE_KD, + double turnKP = TURN_KP, + double turnKD = TURN_KD, + double arcKP = ARC_KP +); } diff --git a/src/greenhat/drive.cpp b/src/greenhat/drive.cpp index 8c0d198..cf0f897 100644 --- a/src/greenhat/drive.cpp +++ b/src/greenhat/drive.cpp @@ -6,29 +6,24 @@ using namespace pros; namespace greenhat { // drive motors -okapi::MotorGroup leftMotors = {LEFT_MOTORS}; -okapi::MotorGroup rightMotors = {RIGHT_MOTORS}; -okapi::AbstractMotor::gearset gearset = okapi::AbstractMotor::gearset::GEARSET; - -// gyro (set to 0 if not using) -int gyro_port = GYRO_PORT; -Imu iSens(gyro_port); +std::shared_ptr leftMotors; +std::shared_ptr rightMotors; // distance constants -const int distance_constant = DISTANCE_CONSTANT; // ticks per foot -const double degree_constant = DEGREE_CONSTANT; // ticks per degree +int distance_constant; // ticks per foot +double degree_constant; // ticks per degree // slew control (autonomous only) -const int accel_step = ACCEL_STEP; // smaller number = more slew -const int deccel_step = DECCEL_STEP; // 200 = no slew -const int arc_step = ARC_STEP; // acceleration for arcs +int accel_step; // smaller number = more slew +int deccel_step; // 200 = no slew +int arc_step; // acceleration for arcs // pid constants -const double driveKP = DRIVE_KP; -const double driveKD = DRIVE_KD; -const double turnKP = TURN_KP; -const double turnKD = TURN_KD; -const double arcKP = ARC_KP; +double driveKP; +double driveKD; +double turnKP; +double turnKD; +double arcKP; /**************************************************/ // edit below with caution!!! @@ -41,39 +36,38 @@ static int maxSpeed = 100; // basic control void left_drive(int vel) { vel *= 120; - leftMotors.moveVoltage(vel); + leftMotors->moveVoltage(vel); } void right_drive(int vel) { vel *= 120; - rightMotors.moveVoltage(vel); + rightMotors->moveVoltage(vel); } void left_drive_vel(int vel) { - vel *= (double)gearset / 100; - leftMotors.moveVelocity(vel); + vel *= (double)leftMotors->getGearing() / 100; + leftMotors->moveVelocity(vel); } void right_drive_vel(int vel) { - vel *= (double)gearset / 100; - rightMotors.moveVelocity(vel); + vel *= (double)leftMotors->getGearing() / 100; + rightMotors->moveVelocity(vel); } void setBrakeMode(okapi::AbstractMotor::brakeMode b) { - leftMotors.setBrakeMode(b); - rightMotors.setBrakeMode(b); - leftMotors.moveVelocity(0); - rightMotors.moveVelocity(0); + leftMotors->setBrakeMode(b); + rightMotors->setBrakeMode(b); + left_drive_vel(0); + right_drive_vel(0); } void reset() { - leftMotors.tarePosition(); - rightMotors.tarePosition(); - setBrakeMode(okapi::AbstractMotor::brakeMode::coast); + leftMotors->tarePosition(); + rightMotors->tarePosition(); } int drivePos() { - return (rightMotors.getPosition() + leftMotors.getPosition()) / 2; + return (rightMotors->getPosition() + leftMotors->getPosition()) / 2; } /**************************************************/ @@ -210,8 +204,8 @@ void arc(bool mirror, int arc_length, double rad, int max, int type) { // fix jerk bug between velocity movements if (type != 2) { - leftMotors.moveVelocity(0); - rightMotors.moveVelocity(0); + left_drive_vel(0); + right_drive_vel(0); delay(10); } @@ -244,7 +238,7 @@ void arc(bool mirror, int arc_length, double rad, int max, int type) { if (type == 1) scaled_speed *= (double)time_step / arc_length; else if (type == 2) - scaled_speed *= (1 - (double)time_step / arc_length); + scaled_speed *= (1 - (double)time_step / arc_length); // assign drive motor speeds left_drive_vel(mirror ? speed : scaled_speed); @@ -256,8 +250,8 @@ void arc(bool mirror, int arc_length, double rad, int max, int type) { } if (type != 1) { - leftMotors.moveVelocity(0); - rightMotors.moveVelocity(0); + left_drive_vel(0); + right_drive_vel(0); } } @@ -322,10 +316,8 @@ int driveTask() { // read sensors int sv = - (rightMotors.getPosition() + leftMotors.getPosition() * driveMode) / 2; - if (gyro_port != 0 && driveMode == -1) { - sv = -iSens.get_rotation(); - } + (rightMotors->getPosition() + leftMotors->getPosition() * driveMode) / + 2; // speed int error = sp - sv; @@ -347,16 +339,36 @@ int driveTask() { } } -void initDrive() { +void startTask() { + Task drive_task(driveTask); +} + +void initDrive(std::initializer_list leftMotors, + std::initializer_list rightMotors, int gearset, + int distance_constant, double degree_constant, int accel_step, + int deccel_step, int arc_step, double driveKP, double driveKD, + double turnKP, double turnKD, double arcKP) { + + // assign constants + greenhat::distance_constant = distance_constant; + greenhat::degree_constant = degree_constant; + greenhat::accel_step = accel_step; + greenhat::deccel_step = deccel_step; + greenhat::arc_step = arc_step; + greenhat::driveKP = driveKP; + greenhat::driveKD = driveKD; + greenhat::turnKP = turnKP; + greenhat::turnKD = turnKD; + greenhat::arcKP = arcKP; + + // configure drive motors + greenhat::leftMotors = std::make_shared(leftMotors); + greenhat::rightMotors = std::make_shared(rightMotors); + greenhat::leftMotors->setGearing((okapi::AbstractMotor::gearset)gearset); + greenhat::rightMotors->setGearing((okapi::AbstractMotor::gearset)gearset); + + // start task Task drive_task(driveTask); - if (gyro_port != 0) { - while (iSens.is_calibrating()) - delay(20); - } - leftMotors.setGearing(gearset); - rightMotors.setGearing(gearset); - leftMotors.setEncoderUnits(okapi::AbstractMotor::encoderUnits::degrees); - rightMotors.setEncoderUnits(okapi::AbstractMotor::encoderUnits::degrees); } /**************************************************/ diff --git a/src/main.cpp b/src/main.cpp index 6cae34e..83f954e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,6 +3,7 @@ pros::Controller master(CONTROLLER_MASTER); void initialize() { + initDrive(); } void disabled() {