-
Notifications
You must be signed in to change notification settings - Fork 1
/
Lab2A.c
348 lines (348 loc) · 8.14 KB
/
Lab2A.c
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
///*
// * Lab2A.c
// *
// * Created on: Jan 27, 2017
// * Author: Mike Hopkins
// */
//
//
//#include "RBELib/RBELib.h"
////For use of abs()
//#include <stdlib.h>
//#include "Lab2A.h"
//#include "Libraries/motors.h"
//#include "Libraries/ADC.h"
//#include "Libraries/kinematics.h"
//
//int DAC0Val = 0;
//int DAC1Val = 1023;
//int direction =1;
//
//volatile int hzFlag = 0;
//volatile int counter=0;
//volatile int counter2=0;
//
////PID Vaiables
//int integrationSumShoulder = 0;
//double integrationSumElbow = 0;
//
//double KpShoulder = 45;
//double KiShoulder = 0.03;
//double KdShoulder = 0;
//
//double KpElbow = 20;
//double KiElbow = 0.01;
//double KdElbow = 0;
//
//int lastPosShoulder = 0;
//int lastPosElbow = 0;
//int lastPIDOutputShoulder = 0;
//int lastPIDOutputElbow = 0;
//volatile unsigned int timer = 0;
//double desiredValueShoulder=0;
//double desiredValueElbow=0;
//int currentVal;
//int oldValShoulder = -999;
//int oldValElbow = -999;
//int error;
//int diffErr;
//
//int desiredValue = 0;
//
//int DACValue = 1023;
//int ADCValue =0;
//int x = 0;
//int y=10;
//
//double theta1,theta2;
//
//int buttonFunction = FANCY_DRAW_TRIANGLE;
//
//
//void Lab2AInit()
//{
// initADC(2);
// initSPI();
// TimerInit100Hz();
// DDRC = (1<<DDA0);
// DDRC = (0<<DDC4|0<<DDC5|0<<DDC6|0<<DDC7);
// PORTC = 0;
//
// //printf("Desired Position: (%i,%i) Theta1: %f Theta2: %f\n\r",x,y,xyToTheta1(x,y,signTheta2(x,y)*xyToTheta2(x,y)),signTheta2(x,y)*xyToTheta2(x,y));
//// desiredValueShoulder = xyToTheta1(x,y,signTheta2(x,y)*xyToTheta2(x,y));
//// desiredValueElbow = signTheta2(x,y)*xyToTheta2(x,y);
//
// //intiDAC();
//
//}
//
//void Lab2ALoop()
//{
//
//
// if(hzFlag == 1)
// {
// updatePID(desiredValueShoulder,SHOULDER_MOTOR);
// updatePID(desiredValueElbow,ELBOW_MOTOR);
// //printf("Desired Value Elbow, %f, Current Value,%i,Shoulder PID%i,%i\n\r", desiredValueElbow,currentVal,lastPIDOutputElbow,ADCtoMillamps(getADC(0)));
// }
//
//
//
// if(counter>3)
// {
// counter=0;
// driveMotor(SHOULDER_MOTOR,lastPIDOutputShoulder);
// driveMotor(ELBOW_MOTOR,-1*lastPIDOutputElbow);
// //printf("Desired Value Shoulder: %f, Current Value: %i,Shoulder PID: %i,%i\n\r", desiredValueShoulder,currentVal,lastPIDOutputElbow,ADCtoMillamps(getADC(0)));
//
// }
//
// if(buttonFunction == FANCY_DRAW_TRIANGLE)
// {
// if(counter2<500)
// {
// desiredValueShoulder = 2;
// desiredValueElbow = 8;
// }else{
// if(counter2<1000)
// {
// desiredValueShoulder = 31;
// desiredValueElbow = -42;
// }else{
// if(counter2<1500)
// {
// desiredValueShoulder = 58;
// desiredValueElbow = -65;
// }else{
// if(counter2<2000)
// {
// desiredValueShoulder = 44;
// desiredValueElbow = -80;
// }else{
// if(counter2<2500)
// {
// desiredValueShoulder = 46;
// desiredValueElbow = -95;
// }else{
// if(counter2<3000)
// {
// desiredValueShoulder = 28;
// desiredValueElbow = -66;
// }else{
// counter2=0;
// }
// }
// }
// }
// }
// }
// }
//
// if(~PINC & 0b10000) //if PORT C4 is low change value
// {
// switch(buttonFunction) {
//
// case STEP_TEST:
// desiredValueShoulder = 22;
// desiredValueElbow = 70;
// break;
//
// case TRIANGLE_DRAW:
// desiredValueShoulder = 2;
// desiredValueElbow = 8;
// break;
// }
//
//
// }
//
// if(~PINC & 0b100000) //if PORT C5 is low change value
// {
// switch(buttonFunction)
// {
// case STEP_TEST:
// desiredValueShoulder = 63;
// desiredValueElbow = -45;
// break;
//
// case TRIANGLE_DRAW:
// desiredValueShoulder = 58;
// desiredValueElbow = -65;
// break;
// }
// }
//
// if(~PINC & 0b1000000) //if PORT C6 is low change value
// {
// switch(buttonFunction)
// {
// case STEP_TEST:
// desiredValueShoulder = 30;
// desiredValueElbow = -60;
// break;
//
// case TRIANGLE_DRAW:
// desiredValueShoulder = 46;
// desiredValueElbow = -95;
// break;
// }
//
// }
//
// if(~PINC & 0b10000000) //if PORT C7 is low start value
// {
// switch(buttonFunction)
// {
// case STEP_TEST:
// desiredValueShoulder = 50;
// desiredValueElbow = 85;
// break;
//
// case TRIANGLE_DRAW:
// break;
// }
// }
//
//
// theta1 = adcToDegreesArm1(getADC(2));
// theta2 = adcToDegreesArm2(getADC(3));
// double X = getX(theta1, theta2);
// double Y = getY(theta1, theta2);
// printf("T1:,%06.2f, T2:, %06.2f, X:, %06.2f, Y:, %06.2f\n\r",theta1, theta2, X, Y);
// printf("%06.2f,%06.2f,%06.2f,%06.2f\n",theta1, theta2, X, Y);
//
//
//// printf("Desired Position: (%i,%i) Theta1: %f Theta2: %f\n\r",x,y,xyToTheta1(x,y,signTheta2(x,y)*xyToTheta2(x,y)),signTheta2(x,y)*xyToTheta2(x,y));
//// printf("Arm Angle, %f ADCValue: %f\n\r", adcToDegreesArm2(degreesAAA),degreesAAA);
//}
//
//
////int updatePID(int desiredValue, int motor)
////{
//// if(motor == SHOULDER_MOTOR)
//// {
//// if(oldValShoulder == -999){
//// oldValShoulder = desiredValue;
//// }
//// if(oldValShoulder != desiredValue){
//// oldValShoulder = desiredValue;
//// integrationSumShoulder = 0;
//// }
//// currentVal = adcToDegreesArm1(getADC(SHOULDER_MOTOR_ADC_CHANNEL));
//// error = desiredValue - currentVal;
//// integrationSumShoulder += (error/10);
//// diffErr = lastPosShoulder-currentVal;
//// if(error < 5 && error > -5) error = 0;
//// lastPIDOutputShoulder= (int) KpShoulder*error + KiShoulder*integrationSumShoulder + KdShoulder*diffErr;
////
////
//// //printf("Err %i, INT %i, DER %i, OUT %i\n\r",error,integrationSumShoulder,diffErr,(int)lastPIDOutputShoulder );
//// lastPosShoulder = currentVal;
//// return (int) lastPIDOutputShoulder;
//// }
//// if(motor == ELBOW_MOTOR)
//// {
//// if(oldValElbow == -999){
//// oldValElbow = desiredValue;
//// }
//// if(oldValElbow != desiredValue){
//// oldValElbow = desiredValue;
//// integrationSumElbow = 0;
//// }
//// currentVal = adcToDegreesArm2(getADC(ELBOW_MOTOR_ADC_CHANNEL));
//// error = desiredValue - currentVal;
//// integrationSumElbow += (error/10);
//// diffErr = lastPosElbow-currentVal;
//// if(error < 5 && error > -5) error = 0;
//// lastPIDOutputElbow= (int) KpElbow*error + KiElbow*integrationSumElbow + KdElbow*diffErr;
////
////
//// //printf("Err %i, INT %i, DER %i, OUT %i\n\r",error,integrationSumShoulder,diffErr,(int)lastPIDOutputShoulder );
//// lastPosShoulder = currentVal;
//// return (int) lastPIDOutputElbow;
//// }
//// /*
//// if(motor == ELBOW_MOTOR)
//// {
//// currentVal = getADC(ELBOW_MOTOR_ADC_CHANNEL);
//// error = desiredValue - currentVal;
//// integrationSumElbow += error;
//// lastPIDOutputElbow=Kp;// *error+Ki*integrationSumElbow+Kd*(lastPIDOutputShoulder-currentVal);
//// return (int) lastPIDOutputElbow;
//// }
//// */
//// return 0;
////}
//
///*
//void TimerInit100Hz()
//{
// //Timer Setup
// TIMSK0= (0<<OCIE0B)|
// (1<<OCIE0A)| //Enable compare A interrupt
// (0<<TOIE0);
//
// TCCR0A= (0<<COM0A1)|
// (0<<COM0A0)|
// (0<<COM0B1)|
// (0<<COM0B0)|
// (1<<WGM01)|
// (0<<WGM00);
//
// TCCR0B =(0<<FOC0A)|
// (0<<FOC0B)|
// (0<<WGM02)|
// (1<<CS02)|
// (0<<CS01)| //set clock divide to /1024
// (1<<CS00);
// OCR0A = 179; //the compare register not supposed to be binary
// sei(); //enable global interupts
//}*/
//
//
//
///**
// * @brief interrupt to fire 100Hz
// */
///*
//ISR(TIMER0_COMPA_vect)
//{
// hzFlag = 1;
// counter++;
// counter2++;
//}*/
//
//
///**
// *
// * @brief display 2 triangle waves phase shifted 180 degrees Part 5
// *
// */
///*
//void showTriangleWave()
//{
// //This is the code to display 2 triangle waves phase shifted 180 degrees Part 5
// setDAC(0,DAC0Val);
// setDAC(1,DAC1Val);
// _delay_ms(10);
// if(direction==1)
// {
// DAC0Val += 3;
// DAC1Val -= 3;
//
// if(DAC0Val>1024)
// {
// direction =0;
// }
// }else{
// DAC0Val -= 3;
// DAC1Val += 3;
// if(DAC0Val<0)
// {
// direction=1;
// }
// }
//}
//*/
//