Repository navigation
Expand file tree
/
Copy pathrobot.cpp
More file actions
executable file
·212 lines (146 loc) · 4.67 KB
/
Copy pathrobot.cpp
File metadata and controls
executable file
·212 lines (146 loc) · 4.67 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
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
/*
Robot.c
*/
#include "robot.h"
#include <stdint.h>
extern "C"{
#include "motorcontrol.h"
#include "pwm.h"
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "stdlib.h"
}
#include "HardwareSerial.h"
robotState Robot;
Encoder encoder;
void RobotInit(void){
initMotor1();
initMotor2();
initEncoders();
initSpeedControl();
}
void initEncoders()
{
//************* Intialize encoders *********************
DDRD &= ~_BV(PIN2) & ~_BV(PIN3)& ~_BV(PIN4)& ~_BV(PIN5);
cli();
/*
* Using PCINT18,19,20,6
*/
PCICR |= _BV(PCIE2);
PCMSK2 = _BV(PCINT18) | _BV(PCINT19) | _BV(PCINT20) | _BV(PCINT21);
sei();
}
ISR(PCINT2_vect){
Robot.rightWheel.encoder.update(bit_is_set(PIND, PIN5), bit_is_set(PIND, PIN4));
Robot.leftWheel.encoder.update( bit_is_set(PIND, PIN2), bit_is_set(PIND, PIN3));
}
void initSpeedControl()
{
// this function initializes the timer for the feed back
// loop controlling the speed
cli();
//Feed back is going to be set for a prescaller of 1024 on a 8 bit timer, feed back loop is
// 32.8 miliseoncs
TCCR2B = 0b00000111;
TCNT2 = 0;
TIMSK2 = _BV(TOIE2); // Set interrupt on compare match
Robot.rightWheel.vel=0;
Robot.rightWheel.velD = 0;
Robot.rightWheel.encoder.clearCount();
Robot.rightWheel.countD = 0;
Robot.rightWheel.errorp =0;
Robot.rightWheel.kiGain =0;
Robot.leftWheel.vel = 0;
Robot.leftWheel.velD = 0;
Robot.leftWheel.encoder.clearCount();
Robot.leftWheel.countD = 0;
Robot.leftWheel.errorp=0;
Robot.leftWheel.kiGain =0;
Robot.feedbackState = 0;
sei();
}
ISR(TIMER2_OVF_vect) // feed back loop interrupt
{
//TCNT2 = 0; // Feedbackloop modifies every 32.8 miliseconds
Robot.stuff++;
if (Robot.feedbackState == 1){
Robot.rightWheel.vel = Robot.rightWheel.encoder.count() - Robot.rightWheel.previousCount;
Robot.rightWheel.previousCount = Robot.rightWheel.encoder.count();
Robot.leftWheel.vel = Robot.leftWheel.encoder.count() - Robot.leftWheel.previousCount;
Robot.leftWheel.previousCount = Robot.leftWheel.encoder.count();
Robot.rightWheel.error = Robot.rightWheel.velD - Robot.rightWheel.vel;
Robot.leftWheel.error = Robot.leftWheel.velD - Robot.leftWheel.vel;
//12V
Robot.rightWheel.kpGain = Robot.rightWheel.error*4 ;
Robot.leftWheel.kpGain = Robot.leftWheel.error *4;
//12V
Robot.rightWheel.kDGain = (Robot.rightWheel.errorp -Robot.rightWheel.error);
Robot.leftWheel.kDGain = (Robot.leftWheel.errorp -Robot.leftWheel.error);
Robot.rightWheel.kiGain = 0;
Robot.leftWheel.kiGain = 0;
Robot.rightWheel.errorp = Robot.rightWheel.error;
Robot.leftWheel.errorp = Robot.leftWheel.error;
Robot.rightWheel.pwmPeriod += Robot.rightWheel.kpGain - Robot.rightWheel.kDGain + Robot.rightWheel.kiGain;
Robot.leftWheel.pwmPeriod += Robot.leftWheel.kpGain - Robot.leftWheel.kDGain + Robot.leftWheel.kiGain;
Robot.leftWheel.pwmPeriod = limitSpeedMax(Robot.leftWheel.pwmPeriod);
Robot.rightWheel.pwmPeriod = limitSpeedMax(Robot.rightWheel.pwmPeriod);
if((Robot.leftWheel.velD == 0) && (abs(Robot.leftWheel.pwmPeriod) < 30))
{
motor1SetSpeed(2);
}
else
{
motor1SetSpeed(-Robot.leftWheel.pwmPeriod);
}
if((Robot.rightWheel.velD == 0) && (abs(Robot.rightWheel.pwmPeriod) < 30))
{
motor2SetSpeed(2);
}
else motor2SetSpeed(Robot.rightWheel.pwmPeriod);
Robot.feedbackState =0;
} // end if of feedback state 1
else{
Robot.feedbackState = 1;
}
}
void stop()
{
Robot.leftWheel.velD = 0;
Robot.leftWheel.encoder.clearCount();
Robot.rightWheel.velD=0;
Robot.rightWheel.encoder.clearCount();
Robot.leftWheel.countD=0;
Robot.rightWheel.countD=0;
}
void setMovement(int movementCounts)
{
Robot.leftWheel.countD = Robot.leftWheel.encoder.count() + movementCounts;
Robot.rightWheel.countD = Robot.rightWheel.encoder.count() + movementCounts;
}
void setLeftMotorSpeed(int cmPerSec)
{
cmPerSec = cmPerSec;
if (cmPerSec > 100) cmPerSec = 100;
if (cmPerSec< -100) cmPerSec = -100;;
/* if ((Robot.leftWheel.blacklashFlag == 1) && (cmPerSec <0)){ //new speed is backwards
// i was going forwards
Robot.leftWheel.blacklashFlag = 0;
Robot.leftWheel.encoder.adjustCount(motorBacklashEncoderCounts);
}
else if ((Robot.leftWheel.blacklashFlag == 0) && (cmPerSec >0)){ // new speed is forwards, was going back
Robot.leftWheel.blacklashFlag = 1; // iam now set to be going forward
Robot.leftWheel.encoder.adjustCount(-motorBacklashEncoderCounts);
}
*/
Robot.leftWheel.velCM = cmPerSec;
Robot.leftWheel.velD = ((cmPerSec) *velEncoderPerCm);
}
void setRightMotorSpeed(int cmPerSec)
{
if (cmPerSec > 100) cmPerSec = 100;
if (cmPerSec< -100) cmPerSec = -100;
Robot.rightWheel.velCM = cmPerSec;
Robot.rightWheel.velD = cmPerSec *velEncoderPerCm;
}