-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblockPusher.cpp
More file actions
137 lines (115 loc) · 3.2 KB
/
blockPusher.cpp
File metadata and controls
137 lines (115 loc) · 3.2 KB
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
#include "blockPusher.h"
#include <Zumo32U4.h>
Zumo32U4Motors motorsv2;
Zumo32U4ProximitySensors proxSensors;
const uint8_t minimum = 0; //readings must be greater than this to
const uint16_t turnSpeedMax = 300;
const uint16_t turnSpeedMin = 100;
const uint16_t deceleration = 10;
const uint16_t acceleration = 10;
#define LEFT 0
#define RIGHT 1
bool senseDir = RIGHT;
bool turningLeft = false;
bool turningRight = false;
uint16_t turnSpeed = 200;
uint16_t lastTimeObjectSeen = 0;
int i = 0;
// turns right towards block
void blockPusher::turnRight() {
motorsv2.setSpeeds(turnSpeed, -turnSpeed);
turningLeft = false;
turningRight = true;
}
void blockPusher::setup() {
proxSensors.initFrontSensor();
}
//turns left towards block
void blockPusher::turnLeft() {
motorsv2.setSpeeds(-turnSpeed, turnSpeed);
turningLeft = true;
turningRight = false;
}
//stops moving
void blockPusher::stop() {
motorsv2.setSpeeds(0, 0);
turningLeft = false;
turningRight = false;
}
//it goes forward 20 cm
void blockPusher::twin() {
motorsv2.setSpeeds(270, 270);
delay(1000);
motorsv2.setSpeeds(0, 0);
ibne = false;
}
//named this attack because it attacks the block
void blockPusher::attack() {
if (i <= 10) {
motorsv2.setSpeeds(400, 400);
//delay(20);
//i++
}
}
//the code that pushes the block out of the circle
void blockPusher::duwBlock() {
if (ibne) {
twin();
}
// if (himar){
proxSensors.read();
uint8_t leftValue = proxSensors.countsFrontWithLeftLeds();
uint8_t rightValue = proxSensors.countsFrontWithRightLeds();
// Determine if an object is visible or not.
bool objectSeen = leftValue >= minimum || rightValue >= minimum;
// printReadingsToSerial();
if (objectSeen) {
// An object is visible, so we will start decelerating in
// order to help the robot find the object without
// overshooting or oscillating.
turnSpeed = 200;
} else {
// An object is not visible, so we will accelerate in order
// to help find the object sooner.
turnSpeed += acceleration;
}
// Constrain the turn speed so it is between turnSpeedMin and
// turnSpeedMax.
turnSpeed = constrain(turnSpeed, turnSpeedMin, turnSpeedMax);
if (objectSeen) {
// An object seen.
ledYellow(1);
lastTimeObjectSeen = millis();
if (leftValue < rightValue) {
if (rightValue - leftValue > minimum) { // minimum was 1 first
// The right value is greater, so the object is probably
// closer to the robot's right LEDs, which means the robot
// is not facing it directly. Turn to the right to try to
// make it more even.
turnRight();
senseDir = RIGHT;
} else {
stop();
}
} else if (leftValue > rightValue) {
if (leftValue - rightValue > minimum) { // minimum was 1 first
// The left value is greater, so turn to the left.
turnLeft();
senseDir = LEFT;
}
} else {
// The values are equal, so stop the motors.
// stop();
attack();
}
} else {
// No object is seen, so just keep turning in the direction
// that we last sensed the object.
ledYellow(0);
if (senseDir == RIGHT) {
turnRight();
} else {
turnLeft();
}
}
}