This repository has been archived by the owner on Aug 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
barebones_driving_code.cpp
executable file
·89 lines (65 loc) · 3.19 KB
/
barebones_driving_code.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*----------------------------------------------------------------------------*/
/* */
/* Project: Better moter controll */
/* Module: main.cpp */
/* Author: Mitchell */
/* Created: Fri Jan 28 2022 */
/* Description: V5 project */
/* */
/*----------------------------------------------------------------------------*/
// Copyright (C) 2023 HeronErin
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 3 of the
// License, or (at your option) any later version.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// ---- START VEXCODE CONFIGURED DEVICES ----
// Robot Configuration:
// [Name] [Type] [Port(s)]
// Controller1 controller
// Motor1 motor 1
// Motor2 motor 2
// ---- END VEXCODE CONFIGURED DEVICES ----
#include "vex.h"
#include <stdlib.h>
using namespace vex;
const bool INVERT_MOTER_ONE = false;
const bool INVERT_MOTER_TWO = false;
const bool INVERT_TURNING = false;
const bool USE_SAME_STICK_FOR_TURNING = true;
void movementCallback(){
// Get joystick amounts and conver to float for calculations
float speed = (float)Controller1.Axis3.position();
float turn;
if (USE_SAME_STICK_FOR_TURNING)
turn = (float)Controller1.Axis4.position();
else
turn = (float)Controller1.Axis1.position();
// variables for inverting
int moterOneMult = 1; if (INVERT_MOTER_ONE) moterOneMult=-1;
int moterTwoMult = 1; if (INVERT_MOTER_TWO) moterTwoMult=-1;
if (INVERT_TURNING) turn*=-1;
if (speed != 0){
// Set one side to speed while setting other side to percentage of speed var based on turn direction
int ispeed = (int) speed;
if (turn > 0){
Motor1.setVelocity(moterOneMult*ispeed , percent);
Motor2.setVelocity(moterTwoMult*((int)(speed * (1- turn/100))) , percent);
}else{
Motor1.setVelocity(moterOneMult*((int)(speed * (1- fabs(turn)/100))) , percent);
Motor2.setVelocity(moterTwoMult*ispeed , percent);
}
}else{
// use opposing motors to turn without forward momentum
Motor1.setVelocity( moterOneMult*((int)turn), percent);
Motor2.setVelocity(-1* moterTwoMult*((int)turn), percent);
}
}
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
Controller1.Axis1.changed(movementCallback);
Controller1.Axis3.changed(movementCallback);
Controller1.Axis4.changed(movementCallback);
}