diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..acaf745 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/.vscode +/bin +/build +compile_commands.json diff --git a/Ri3D 2024.v5code b/Ri3D 2024.v5code new file mode 100644 index 0000000..3663ce2 --- /dev/null +++ b/Ri3D 2024.v5code @@ -0,0 +1 @@ +{"title":"Ri3D 2024","description":"Empty V5 C++ Project","icon":"USER921x.bmp","version":"23.09.1216","sdk":"20220726_10_00_00","language":"cpp","competition":false,"files":[{"name":"include/robot-config.h","type":"File","specialType":"device_config"},{"name":"include/vex.h","type":"File","specialType":""},{"name":"include/joystick.cpp","type":"File","specialType":""},{"name":"makefile","type":"File","specialType":""},{"name":"src/main.cpp","type":"File","specialType":""},{"name":"src/robot-config.cpp","type":"File","specialType":"device_config"},{"name":"vex/mkenv.mk","type":"File","specialType":""},{"name":"vex/mkrules.mk","type":"File","specialType":""},{"name":"include","type":"Directory"},{"name":"src","type":"Directory"},{"name":"vex","type":"Directory"}],"device":{"slot":1,"uid":"276-4810","options":{}},"isVexFileImport":false,"robotconfig":[]} \ No newline at end of file diff --git a/include/joystick.cpp b/include/joystick.cpp new file mode 100644 index 0000000..f3fe729 --- /dev/null +++ b/include/joystick.cpp @@ -0,0 +1,96 @@ +#include "vex.h" +#include + + +/* + this class was made by Cole Huffine class of 2024 + lets hope rivera gives this to future classes bc + i think it would be useful for everyone +*/ + +/* + In order to use this class, put it in your "include" folder + and then put '#inlcude "joystick.cpp"' at the top of your main file + + Usage: + If you need to get the value of a joystick fixed to account for the physical limitations + getLeftX(); + getLeftY(); + getRightX(); + getRightY(); + + If you need to get the REAL value of a joystick, as inputted into the brain + getTrueLeftX(); + getTrueLeftY(); + getTrueRightX(); + getTrueRightY(); +*/ + +/* + If you want to use a second controller, or name your controller something else other than Controller1, + then go to all of the getTrue methods, and change the name to what you have as your config +*/ + + +//this returns the sign of the value you pass in as a parameter +//i originally made this file in java, and c++ doesnt have this +//built in so i made it :) +int signum(double val) { + return (val >= 0) ? 1 : -1; +} + + +//use these if you need to get the true value of any joystick +double getTrueLeftX() { + return Controller1.Axis4.position(pct); +} + +double getTrueLeftY() { + return Controller1.Axis3.position(pct); +} + +double getTrueRightX() { + return Controller1.Axis1.position(pct); +} + +double getTrueRightY() { + return Controller1.Axis2.position(pct); +} + +/* + * This method is to fix the flawed input of a vex controller. The input is a box, + * both X and Y axis can go from -1 to 1. However, since the controller build physically + * forces the joystick into a circle, its impossible to reach the the corner of the box. + * This method fixes that issue. +*/ +double joystickFix(bool isLeft, bool isX) { + double root2 = sqrt(2); + double x = isLeft ? getTrueLeftX() : getTrueRightX(); + double y = isLeft ? getTrueLeftY() : getTrueRightY(); + double magnitude = sqrt(x*x + y*y); + double values[2] = { + signum(x) * fmin(fabs(x*root2), magnitude), + signum(y) * fmin(fabs(y*root2), magnitude) + }; + return isX? values[0] : values[1]; +} + + +// these return the fixed value of all of the joysticks +double getLeftX() { + return joystickFix(true, true); +} + +double getLeftY() { + return joystickFix(true, false); +} + +double getRightX() { + return joystickFix(false, true); +} + +double getRightY() { + return joystickFix(false, false); +} + + diff --git a/include/robot-config.h b/include/robot-config.h new file mode 100644 index 0000000..c34fd17 --- /dev/null +++ b/include/robot-config.h @@ -0,0 +1,12 @@ +using namespace vex; + +extern brain Brain; + +// VEXcode devices + +/** + * Used to initialize code/tasks/devices added using tools in VEXcode Pro. + * + * This should be called at the start of your int main function. + */ +void vexcodeInit( void ); \ No newline at end of file diff --git a/include/vex.h b/include/vex.h new file mode 100644 index 0000000..ad2084c --- /dev/null +++ b/include/vex.h @@ -0,0 +1,26 @@ +/*----------------------------------------------------------------------------*/ +/* */ +/* Module: vex.h */ +/* Author: Vex Robotics */ +/* Created: 1 Feb 2019 */ +/* Description: Default header for V5 projects */ +/* */ +/*----------------------------------------------------------------------------*/ +// +#include +#include +#include +#include + +#include "v5.h" +#include "v5_vcs.h" + +#include "robot-config.h" + +#define waitUntil(condition) \ + do { \ + wait(5, msec); \ + } while (!(condition)) + +#define repeat(iterations) \ + for (int iterator = 0; iterator < iterations; iterator++) \ No newline at end of file diff --git a/makefile b/makefile new file mode 100644 index 0000000..acc3bca --- /dev/null +++ b/makefile @@ -0,0 +1,30 @@ +# VEXcode makefile 2019_03_26_01 + +# show compiler output +VERBOSE = 0 + +# include toolchain options +include vex/mkenv.mk + +# location of the project source cpp and c files +SRC_C = $(wildcard src/*.cpp) +SRC_C += $(wildcard src/*.c) +SRC_C += $(wildcard src/*/*.cpp) +SRC_C += $(wildcard src/*/*.c) + +OBJ = $(addprefix $(BUILD)/, $(addsuffix .o, $(basename $(SRC_C))) ) + +# location of include files that c and cpp files depend on +SRC_H = $(wildcard include/*.h) + +# additional dependancies +SRC_A = makefile + +# project header file locations +INC_F = include + +# build targets +all: $(BUILD)/$(PROJECT).bin + +# include build rules +include vex/mkrules.mk diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..ff4dc4c --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,138 @@ +/*----------------------------------------------------------------------------*/ +/* */ +/* Module: main.cpp */ +/* Author: C:\Users\User */ +/* Created: Wed Mar 27 2024 */ +/* Description: V5 project */ +/* */ +/*----------------------------------------------------------------------------*/ + +// ---- START VEXCODE CONFIGURED DEVICES ---- +// Robot Configuration: +// [Name] [Type] [Port(s)] +// ---- END VEXCODE CONFIGURED DEVICES ---- + +#include "vex.h" +#include +#include "joystick.cpp" + +using namespace vex; + +competition Competition; + +motor LeftDriveMotor = motor(1, ratio18_1, false); +motor RightDriveMotor = motor(2, ratio18_1, false); +motor FrontDriveMotor = motor(3, ratio18_1, false); +motor BackDriveMotor = motor(4, ratio18_1, false); +inertial Inertial = inertial(5); +motor arm = motor(7, ratio36_1, false); +motor deploy = motor(8, ratio18_1, false); +motor climbMotorA = motor(9, ratio18_1, false); +motor climbMotorB = motor(10, ratio18_1, true); +motor_group climb = motor_group(climbMotorA, climbMotorB); +controller Controller1 = controller(primary); + + +class Drives { + public: + void static robotOirented() { + double yInput = getLeftY(); + double xInput = getLeftX(); + double turning = getTrueRightX(); + double leftMotor = yInput + turning; + double rightMotor = yInput - turning; + double frontMotor = xInput + turning; + double backMotor = xInput - turning; + + LeftDriveMotor.spin(fwd, leftMotor, velocityUnits::pct); + RightDriveMotor.spin(fwd, rightMotor, velocityUnits::pct); + FrontDriveMotor.spin(fwd, frontMotor, velocityUnits::pct); + BackDriveMotor.spin(fwd, backMotor, velocityUnits::pct); + } + + void static autonDrive(double xIn, double yIn, double turnIn) { + double yInput = yIn; + double xInput = xIn; + double turning = turnIn; + double leftMotor = yInput + turning; + double rightMotor = yInput - turning; + double frontMotor = xInput + turning; + double backMotor = xInput - turning; + + LeftDriveMotor.spin(fwd, leftMotor, velocityUnits::pct); + RightDriveMotor.spin(fwd, rightMotor, velocityUnits::pct); + FrontDriveMotor.spin(fwd, frontMotor, velocityUnits::pct); + BackDriveMotor.spin(fwd, backMotor, velocityUnits::pct); + } + + void static fieldOriented() { + double headingRadians = Inertial.heading() * 3.141592/180; + double yInput = getLeftY(); + double xInput = getLeftX(); + double sineHeading = sin(headingRadians); + double cosHeading = cos(headingRadians); + double rotatedYInput = xInput * sineHeading + yInput * cosHeading; + double rotatedXInput = xInput * cosHeading - yInput * sineHeading; + double turning = getTrueRightX(); + double leftMotor = rotatedYInput + turning; + double rightMotor = -rotatedYInput + turning; + double FrontMotor = rotatedXInput + turning; + double backMotor = -rotatedXInput + turning; + + LeftDriveMotor.spin(fwd, leftMotor, velocityUnits::pct); + RightDriveMotor.spin(fwd, rightMotor, velocityUnits::pct); + FrontDriveMotor.spin(fwd, FrontMotor, velocityUnits::pct); + BackDriveMotor.spin(fwd, backMotor, velocityUnits::pct); + } +}; + +void pre_auton(void) { + Inertial.setHeading(0.0, degrees); + Inertial.setRotation(0.0, degrees); + Inertial.startCalibration(); + while(Inertial.isCalibrating()) { + task::sleep(10); + } + Inertial.setHeading(0.0, degrees); + Inertial.setRotation(0.0, degrees); + + // Initializing Robot Configuration. DO NOT REMOVE! + vexcodeInit(); +} + +void usercontrol(void) { + while(true) { + Drives::fieldOriented(); + + //zeroes inertial sensor + if(Controller1.ButtonY.pressing()) { + Inertial.setHeading(0.0, degrees); + Inertial.setRotation(0.0, degrees); + Inertial.startCalibration(); + while (Inertial.isCalibrating()) { + task::sleep(10); + } + Inertial.setHeading(0.0, degrees); + Inertial.setRotation(0.0, degrees); + } + + } +} + +void auton(void) { + Drives::autonDrive(0.0, 0.5, 0); + task::sleep(4000); + Drives::autonDrive(0.0, 0.0, 0); +} + +int main() { + pre_auton(); + + Competition.autonomous(auton); + Competition.drivercontrol(usercontrol); + + while(true) { + task::sleep(10); + } + +} diff --git a/src/robot-config.cpp b/src/robot-config.cpp new file mode 100644 index 0000000..d2402ee --- /dev/null +++ b/src/robot-config.cpp @@ -0,0 +1,23 @@ +#include "vex.h" + +using namespace vex; +using signature = vision::signature; +using code = vision::code; + +// A global instance of brain used for printing to the V5 Brain screen +brain Brain; + +// VEXcode device constructors + +// VEXcode generated functions + + + +/** + * Used to initialize code/tasks/devices added using tools in VEXcode Pro. + * + * This should be called at the start of your int main function. + */ +void vexcodeInit( void ) { + // nothing to initialize +} \ No newline at end of file diff --git a/vex/mkenv.mk b/vex/mkenv.mk new file mode 100644 index 0000000..f420da0 --- /dev/null +++ b/vex/mkenv.mk @@ -0,0 +1,101 @@ +# VEXcode mkenv.mk 2019_06_06_01 + +# macros to help with windows paths that include spaces +sp := +sp += +qs = $(subst ?,$(sp),$1) +sq = $(subst $(sp),?,$1) + +# default platform and build location +PLATFORM = vexv5 +BUILD = build + +# version for clang headers +ifneq ("$(origin HEADERS)", "command line") +HEADERS = 8.0.0 +endif + +# Project name passed from app +ifeq ("$(origin P)", "command line") +PROJECT = $(P) +else +PROJECT = $(notdir $(call sq,$(abspath ${CURDIR}))) +endif + +# Toolchain path passed from app +ifeq ("$(origin T)", "command line") +TOOLCHAIN = $(T) +endif +ifndef TOOLCHAIN +TOOLCHAIN = ${HOME}/sdk +endif + +# Verbose flag passed from app +ifeq ("$(origin V)", "command line") +BUILD_VERBOSE=$(V) +endif + +# allow verbose to be set by makefile if not set by app +ifndef BUILD_VERBOSE +ifndef VERBOSE +BUILD_VERBOSE = 0 +else +BUILD_VERBOSE = $(VERBOSE) +endif +endif + +# use verbose flag +ifeq ($(BUILD_VERBOSE),0) +Q = @ +else +Q = +endif + +# compile and link tools +CC = clang +CXX = clang +OBJCOPY = arm-none-eabi-objcopy +SIZE = arm-none-eabi-size +LINK = arm-none-eabi-ld +ARCH = arm-none-eabi-ar +ECHO = @echo +DEFINES = -DVexV5 + +# platform specific macros +ifeq ($(OS),Windows_NT) +$(info windows build for platform $(PLATFORM)) +SHELL = cmd.exe +MKDIR = md "$(@D)" 2> nul || : +RMDIR = rmdir /S /Q +CLEAN = $(RMDIR) $(BUILD) 2> nul || : +else +$(info unix build for platform $(PLATFORM)) +MKDIR = mkdir -p "$(@D)" 2> /dev/null || : +RMDIR = rm -rf +CLEAN = $(RMDIR) $(BUILD) 2> /dev/null || : +endif + +# toolchain include and lib locations +TOOL_INC = -I"$(TOOLCHAIN)/$(PLATFORM)/clang/$(HEADERS)/include" -I"$(TOOLCHAIN)/$(PLATFORM)/gcc/include" -I"$(TOOLCHAIN)/$(PLATFORM)/gcc/include/c++/4.9.3" -I"$(TOOLCHAIN)/$(PLATFORM)/gcc/include/c++/4.9.3/arm-none-eabi/armv7-ar/thumb" +TOOL_LIB = -L"$(TOOLCHAIN)/$(PLATFORM)/gcc/libs" + +# compiler flags +CFLAGS_CL = -target thumbv7-none-eabi -fshort-enums -Wno-unknown-attributes -U__INT32_TYPE__ -U__UINT32_TYPE__ -D__INT32_TYPE__=long -D__UINT32_TYPE__='unsigned long' +CFLAGS_V7 = -march=armv7-a -mfpu=neon -mfloat-abi=softfp +CFLAGS = ${CFLAGS_CL} ${CFLAGS_V7} -Os -Wall -Werror=return-type -ansi -std=gnu99 $(DEFINES) +CXX_FLAGS = ${CFLAGS_CL} ${CFLAGS_V7} -Os -Wall -Werror=return-type -fno-rtti -fno-threadsafe-statics -fno-exceptions -std=gnu++11 -ffunction-sections -fdata-sections $(DEFINES) + +# linker flags +LNK_FLAGS = -nostdlib -T "$(TOOLCHAIN)/$(PLATFORM)/lscript.ld" -R "$(TOOLCHAIN)/$(PLATFORM)/stdlib_0.lib" -Map="$(BUILD)/$(PROJECT).map" --gc-section -L"$(TOOLCHAIN)/$(PLATFORM)" ${TOOL_LIB} + +# future statuc library +PROJECTLIB = lib$(PROJECT) +ARCH_FLAGS = rcs + +# libraries +LIBS = --start-group -lv5rt -lstdc++ -lc -lm -lgcc --end-group + +# include file paths +INC += $(addprefix -I, ${INC_F}) +INC += -I"$(TOOLCHAIN)/$(PLATFORM)/include" +INC += ${TOOL_INC} diff --git a/vex/mkrules.mk b/vex/mkrules.mk new file mode 100644 index 0000000..a45bbdc --- /dev/null +++ b/vex/mkrules.mk @@ -0,0 +1,32 @@ +# VEXcode mkrules.mk 2019_03_26_01 + +# compile C files +$(BUILD)/%.o: %.c $(SRC_H) + $(Q)$(MKDIR) + $(ECHO) "CC $<" + $(Q)$(CC) $(CFLAGS) $(INC) -c -o $@ $< + +# compile C++ files +$(BUILD)/%.o: %.cpp $(SRC_H) $(SRC_A) + $(Q)$(MKDIR) + $(ECHO) "CXX $<" + $(Q)$(CXX) $(CXX_FLAGS) $(INC) -c -o $@ $< + +# create executable +$(BUILD)/$(PROJECT).elf: $(OBJ) + $(ECHO) "LINK $@" + $(Q)$(LINK) $(LNK_FLAGS) -o $@ $^ $(LIBS) + $(Q)$(SIZE) $@ + +# create binary +$(BUILD)/$(PROJECT).bin: $(BUILD)/$(PROJECT).elf + $(Q)$(OBJCOPY) -O binary $(BUILD)/$(PROJECT).elf $(BUILD)/$(PROJECT).bin + +# create archive +$(BUILD)/$(PROJECTLIB).a: $(OBJ) + $(Q)$(ARCH) $(ARCH_FLAGS) $@ $^ + +# clean project +clean: + $(info clean project) + $(Q)$(CLEAN)