Skip to content
This repository has been archived by the owner on Jul 11, 2024. It is now read-only.

Commit

Permalink
Modified the drive initializer pass config info as params
Browse files Browse the repository at this point in the history
  • Loading branch information
Marsgate committed Mar 30, 2020
1 parent d41a534 commit f79d9fb
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 63 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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).

Expand Down Expand Up @@ -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.


Expand Down Expand Up @@ -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.

Expand All @@ -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);
Expand Down
Binary file removed [email protected]
Binary file not shown.
Binary file added [email protected]
Binary file not shown.
4 changes: 1 addition & 3 deletions include/greenhat/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 18 additions & 3 deletions include/greenhat/drive.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define _DRIVE_H_

#include "okapi/api.hpp"
#include "greenhat/config.h"

namespace greenhat{

Expand Down Expand Up @@ -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_list<okapi::Motor>leftMotors = {LEFT_MOTORS},
std::initializer_list<okapi::Motor>rightMotors = {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
);

}

Expand Down
110 changes: 61 additions & 49 deletions src/greenhat/drive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<okapi::MotorGroup> leftMotors;
std::shared_ptr<okapi::MotorGroup> 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!!!
Expand All @@ -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;
}

/**************************************************/
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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;
Expand All @@ -347,16 +339,36 @@ int driveTask() {
}
}

void initDrive() {
void startTask() {
Task drive_task(driveTask);
}

void initDrive(std::initializer_list<okapi::Motor> leftMotors,
std::initializer_list<okapi::Motor> 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<okapi::MotorGroup>(leftMotors);
greenhat::rightMotors = std::make_shared<okapi::MotorGroup>(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);
}

/**************************************************/
Expand Down
1 change: 1 addition & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
pros::Controller master(CONTROLLER_MASTER);

void initialize() {
initDrive();
}

void disabled() {
Expand Down

0 comments on commit f79d9fb

Please sign in to comment.