少女祈祷中...

VEX学习笔记

烧录方式

手柄与主控配对

代码烧录在手柄上

代码逻辑

vex.h包含vex函数库

常用函数

调用函数:把两个冒号变成一个点

1.motor函数

在int main前面实体化对象

1
brain b1;//实体化最大的类

img

1
2
3
public vex::motor::motor(int32_t index)

motor motor(1);

(1)指的是接在了端口1上

1
public vex::motor::motor(int32_t index, bool reverse)

(端口,电机正转反转)

1
motor motor(1,true);
1
public void vex::motor::spin([directionType] dir, double velocity, [(velocityUnits) units)

(2)参数dir

fwd:电机正转、

rev:电机反转

pct:百分制

rpm:转速

dps:每秒钟转的角度

1
motor1.spin(fwd,10,rpm)//每分钟十转正转

2.延时函数:

1
this_thread::sleep_for(100);//执行100次

3.遥控器

摇杆前后对应3,左右对应4

1
Controller c1;
1
2
3
4
5
6
7
8
9
10
11
public bool vex::controller::button::pressing(void) const

if(c1.button.pressing)

{

motor1.spin(fwd,20,rpm)

}

//对应摇杆的轴
1
2
3
4
5
public int32_t vex::controller::axis::position([percentUnits) units=percentUnits::pct)

ly=c1.Axis3.position(pct);

v=ly/vm;

在开头定义变量

1
2
3
double ly=0; double vm=100; double v=0;

this_thread::sleep_for(100);

调用

c1.Axis

考核作业

1.二轮差速

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
/*----------------------------------------------------------------------------*/
/* */
/* Module: main.cpp */
/* Author: 24682 */
/* Created: 2024/5/14 00:20:21 */
/* Description: V5 project */
/* */
/*----------------------------------------------------------------------------*/

#include "vex.h"
using namespace vex;

// 鍒涘缓椹揪瀵硅薄
motor LF(PORT1锛宖alse);
motor LB(PORT2,false);
motor RF(PORT3, true);
motor RB(PORT4, true);


controller Controller1 ;

const double 2d = 2.5;
const double vm = 150.0;
const double wm = 200.0;
double ly1=0;
double ly2=0;

int main() {
vexcodeInit();

while (1) {

ly1 = Controller1.Axis3.position(pct);
v=ly/vm;
ly2= Controller1.Axis4.position(pct);


double leftSpeed = (ly1/100)*vm - ((ly2/100)*wm * d);
double rightSpeed = (ly1/100)*vm + ((ly2/100)*wm * d);


LF.spin(fwd, leftSpeed, rpm);
LB.spin(fwd, leftSpeed, rpm);
RF.spin(fwd, rightSpeed, rpm);
RB.spin(fwd, rightSpeed, rpm);


This_thread::sleep_for(10);
}

return 0;
}

2.麦轮

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
/*----------------------------------------------------------------------------*/
/* */
/* Module: main.cpp */
/* Author: 24682 */
/* Created: 2024/5/14 00:16:57 */
/* Description: V5 project */
/* */
/*----------------------------------------------------------------------------*/
#include "vex.h"

using namespace vex;

// A global instance of vex::brain used for printing to the V5 brain screen
vex::brain Brain;

// define your global instances of motors and other devices here


#include "vex.h"

using namespace vex;


motor LF(PORT1,false);
motor RF(PORT2, true);
motor LB(PORT3,false
motor RB(PORT4, true);

controller Controller2;


const double vm = 100.0;

int main() {
vexcodeInit();

while (1) {


double ly1 = Controller2.Axis1.position(pct);
double ly2= Controller2.Axis2.position(pct);
double ly3= Controller2.Axis4.position(pct);


double LFSpeed = ly1 + ly2 + ly3;
double RFSpeed = ly2 - ly1 - ly3;
double LBSpeed = ly2- ly1 + ly3;
double RBSpeed = ly1 + ly2 - ly3;


LF.spin(fwd, LFSpeed, pct);
RF.spin(fwd, RFSpeed, pct);
RL.spin(fwd, LBSpeed, pct);
RB.spin(fwd, RBSpeed, pct);


This_thread::sleep_for(10);

}

return 0;
}

竞赛代码

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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*----------------------------------------------------------------------------*/
/* */
/* Module: main.cpp */
/* Author: zhaoy */
/* Created: 2024/5/18 18:08:30 */
/* Description: V5 project */
/* */
/*----------------------------------------------------------------------------*/

#include "vex.h"

using namespace vex;

// A global instance of competition
competition Competition;

// 定义电机
motor leftFrontMotor(19, gearSetting::ratio18_1,false);
motor rightFrontMotor(10, gearSetting::ratio18_1, true);
motor leftRearMotor(9, gearSetting::ratio18_1, false);
motor rightRearMotor(0, gearSetting::ratio18_1, true);
motor moter5(1, gearSetting::ratio18_1, true);

// 定义遥控器
controller Controller;

// define your global instances of motors and other devices here

/*---------------------------------------------------------------------------*/
/* Pre-Autonomous Functions */
/* */
/* You may want to perform some actions before the competition starts. */
/* Do them in the following function. You must return from this function */
/* or the autonomous and usercontrol tasks will not be started. This */
/* function is only called once after the V5 has been powered on and */
/* not every time that the robot is nisabled. */
/*---------------------------------------------------------------------------*/

void pre_auton(void) {

// All activities that occur before the competition starts
// Example: clearing encoders, setting servo positions, ...
}

/*---------------------------------------------------------------------------*/
/* */
/* Autonomous Task */
/* */
/* This task is used to control your robot during the autonomous phase of */
/* a VEX Competition. */
/* */
/* You must modify the code to add your own robot specific commands here. */
/*---------------------------------------------------------------------------*/

void autonomous(void) {
leftFrontMotor.spin(directionType::fwd, 100, velocityUnits::pct);
rightFrontMotor.spin(directionType::rev, 100, velocityUnits::pct);
leftRearMotor.spin(directionType::fwd, 100, velocityUnits::pct);
rightRearMotor.spin(directionType::rev, 100, velocityUnits::pct);
wait(400, msec);

moter5.spin(directionType::fwd, 100 , velocityUnits::pct);

// 停止
leftFrontMotor.stop();
rightFrontMotor.stop();
leftRearMotor.stop();
rightRearMotor.stop();
wait(100, msec);

// 前进
leftFrontMotor.spin(directionType::fwd, 100, velocityUnits::pct);
rightFrontMotor.spin(directionType::fwd, 100, velocityUnits::pct);
leftRearMotor.spin(directionType::fwd, 100, velocityUnits::pct);
rightRearMotor.spin(directionType::fwd, 100, velocityUnits::pct);
wait(1000, msec);

leftFrontMotor.spin(directionType::fwd, 100, velocityUnits::pct);
rightFrontMotor.spin(directionType::fwd, 100, velocityUnits::pct);
leftRearMotor.spin(directionType::fwd, 100, velocityUnits::pct);
rightRearMotor.spin(directionType::fwd, 100, velocityUnits::pct);
wait(1200, msec);


leftFrontMotor.spin(directionType::rev, 100, velocityUnits::pct);
rightFrontMotor.spin(directionType::fwd, 100, velocityUnits::pct);
leftRearMotor.spin(directionType::rev, 100, velocityUnits::pct);
rightRearMotor.spin(directionType::fwd, 100, velocityUnits::pct);
wait(430, msec);


leftFrontMotor.spin(directionType::fwd, 100, velocityUnits::pct);
rightFrontMotor.spin(directionType::fwd, 100, velocityUnits::pct);
leftRearMotor.spin(directionType::fwd, 100, velocityUnits::pct);
rightRearMotor.spin(directionType::fwd, 100, velocityUnits::pct);
wait(1500, msec);

leftFrontMotor.stop();
rightFrontMotor.stop();
leftRearMotor.stop();
rightRearMotor.stop();
wait(100, msec);

leftFrontMotor.spin(directionType::fwd, 100, velocityUnits::pct);
rightFrontMotor.spin(directionType::rev, 100, velocityUnits::pct);
leftRearMotor.spin(directionType::fwd, 100, velocityUnits::pct);
rightRearMotor.spin(directionType::rev, 100, velocityUnits::pct);
wait(400, msec);

leftFrontMotor.spin(directionType::fwd, 100, velocityUnits::pct);
rightFrontMotor.spin(directionType::fwd, 100, velocityUnits::pct);
leftRearMotor.spin(directionType::fwd, 100, velocityUnits::pct);
rightRearMotor.spin(directionType::fwd, 100, velocityUnits::pct);
wait(300, msec);


leftFrontMotor.stop();
rightFrontMotor.stop();
leftRearMotor.stop();
rightRearMotor.stop();
moter5.spin(directionType::rev, 100 , velocityUnits::pct);

leftFrontMotor.spin(directionType::rev, 100, velocityUnits::pct);
rightFrontMotor.spin(directionType::rev, 100, velocityUnits::pct);
leftRearMotor.spin(directionType::rev, 100, velocityUnits::pct);
rightRearMotor.spin(directionType::rev, 100, velocityUnits::pct);
wait(800, msec);

leftFrontMotor.stop();
rightFrontMotor.stop();
leftRearMotor.stop();
rightRearMotor.stop();

leftFrontMotor.spin(directionType::fwd, 100, velocityUnits::pct);
rightFrontMotor.spin(directionType::fwd, 100, velocityUnits::pct);
leftRearMotor.spin(directionType::fwd, 100, velocityUnits::pct);
rightRearMotor.spin(directionType::fwd, 100, velocityUnits::pct);
wait(1000, msec);

leftFrontMotor.stop();
rightFrontMotor.stop();
leftRearMotor.stop();
rightRearMotor.stop();






// ..........................................................................
// Insert autonomous user code here.
// ..........................................................................
}

/*---------------------------------------------------------------------------*/
/* */
/* User Control Task */
/* */
/* This task is used to control your robot during the user control phase of */
/* a VEX Competition. */
/* */
/* You must modify the code to add your own robot specific commands here. */
/*---------------------------------------------------------------------------*/

void usercontrol(void) {
// User control code here, inside the loop
while (1) {


int forward = Controller.Axis3.position();
int sideways = Controller.Axis4.position();
int ewai = Controller.Axis2.position();


int leftFrontSpeed = forward - sideways ;
int rightFrontSpeed = forward + sideways ;
int leftRearSpeed = forward - sideways ;
int rightRearSpeed = forward + sideways ;




leftFrontMotor.spin(directionType::fwd, leftFrontSpeed, velocityUnits::pct);
rightFrontMotor.spin(directionType::fwd, rightFrontSpeed, velocityUnits::pct);
leftRearMotor.spin(directionType::fwd, leftRearSpeed, velocityUnits::pct);
rightRearMotor.spin(directionType::fwd, rightRearSpeed, velocityUnits::pct);
moter5.spin(directionType::fwd, ewai , velocityUnits::pct);

// This is the main execution loop for the user control program.
// Each time through the loop your program should update motor + servo
// values based on feedback from the joysticks.

// ........................................................................
// Insert user code here. This is where you use the joystick values to
// update your motors, etc.
// ........................................................................

wait(20, msec); // Sleep the task for a short amount of time to
// prevent wasted resources.
}
}

//
// Main will set up the competition functions and callbacks.
//
int main() {
// Set up callbacks for autonomous and driver control periods.
Competition.autonomous(autonomous);
Competition.drivercontrol(usercontrol);

// Run the pre-autonomous function.
pre_auton();

// Prevent main from exiting with an infinite loop.
while (true) {
wait(100, msec);
}
}