-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
335 lines (287 loc) · 8.03 KB
/
main.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
//CREATED BY DAVID XU AND ARYAN GHOSH FOR THE NIOS II ROBOT//
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
//Defining I/O addresses for NIOSII GPIO
#define ADDR_JP1PORT ((volatile char *) 0xFF200060)
#define ADDR_JP2PORT ((volatile char *) 0xFF200070)
//Onboard Hardware initialization
#define LED ((volatile long *) 0xFF200000)
#define PUSHBUTTONS ((volatile long *) 0xFF200050)
#define SWITCHES ((volatile long *) 0xFF200040)
// PS/2 port address
#define PS2_ptr ((volatile int *) 0xFF200100)
#define PUSHBUTTONS ((volatile long *) 0xFF200050)
#define NUM_JOINTS 3
//Motor set to microstepping
//1.8 degrees /5 due to 5:1 planetary gear ratio and /4 due to quarter step a4988 driver setting
double STEP_SIZE = 1.8/20;
// forward kinematics
typedef struct {
double x;
double y;
double z;
} Vector3D;
typedef struct {
double theta;
double length;
double upperlimit, lowerlimit;
} Joint;
typedef struct {
Joint joints[NUM_JOINTS];
} RoboticArm;
void write_pixel(int x, int y, short colour) {
volatile short *vga_addr=(volatile short*)(0x08000000 + (y<<10) + (x<<1));
*vga_addr=colour;
}
/* use write_pixel to set entire screen to black (does not clear the character buffer) */
void clear_screen() {
int x, y;
for (x = 0; x < 320; x++) {
for (y = 0; y < 240; y++) {
write_pixel(x,y,0);
}
}
}
void background() {
int x, y;
for (x = 0; x < 320; x++) {
for (y = 0; y < 240; y++) {
if(y < 179){
write_pixel(x,y,image[y][x]);
}else{
write_pixel(x,y,0);
}
}
}
}
/* write a single character to the character buffer at x,y
* x in [0,79], y in [0,59]
*/
void write_char(int x, int y, char c) {
// VGA character buffer
volatile char * character_buffer = (char *) (0x09000000 + (y<<7) + x);
*character_buffer = c;
}
bool isValidPosition(double theta1, double theta2, double theta3) {
// Define the rotational limits for each motor
const double motor1_min = -180.0;
const double motor1_max = 180.0; // 360 degrees in radians
const double motor2_min = 85;
const double motor2_max = 95; // 200 degrees in radians
const double motor3_min = -135.0;
const double motor3_max = +135.0; // 270 degrees in radians
// Check if the position is within the limits for each motor
if (theta1 < motor1_min || theta1 > motor1_max)
return false;
if (theta2 < motor2_min || theta2 > motor2_max)
return false;
if (theta3 < motor3_min || theta3 > motor3_max)
return false;
// If all checks pass, the position is valid
return true;
}
Vector3D cylindricalToCartesian(double theta, double radius, double height) {
Vector3D cartesian;
cartesian.x = radius * cos(theta);
cartesian.y = radius * sin(theta);
cartesian.z = height;
return cartesian;
}
void motor(int sw, RoboticArm *arm){
long count = 2500;
int i = 0;
int led = inttohex(sw);
int index = (int)(sw/2);
//long PBreleases;
long Swval = *SWITCHES;
while(Swval != 0){
Swval = *SWITCHES;
if(i == 0){
if(sw % 2 == 1){
led = led ^ inttohex(sw-1); //xor for toggling led
if((led & inttohex(sw-1)) != 0){
arm->joints[index].theta += STEP_SIZE;
if(arm->joints[index].theta >= arm->joints[index].upperlimit){ break;}
}
} else {
led = led ^ inttohex(sw); //xor for toggling led
if(led != 0){
arm->joints[index].theta -= STEP_SIZE;
if(arm->joints[index].theta <= arm->joints[index].lowerlimit){ break;}
}
}
i = count;
}else{
i--;
}
*ADDR_JP1PORT = led;
*LED = led;
}
*LED = 0;
*ADDR_JP1PORT = 0;
return;
}
void moveMotor(Joint* vector, double targetAngle, int num) {
// Determine the direction of rotation
bool clockwise = (vector->theta > targetAngle);
long count = 5000;
int i = 0;
int led = inttohex(num*2);
// Move the motor until it reaches the target angle
do {
if(i == 0){
if (clockwise){
led = led ^ inttohex(num*2+1); //xor for toggling led
i = count;
if((led & inttohex(num-1)) != 0){
vector->theta += STEP_SIZE;
}
}else{
led = led ^ inttohex(num*2); //xor for toggling led
i = count;
if(led != 0){
vector->theta -= STEP_SIZE;
}
}
}else{
i--;
}
*ADDR_JP1PORT = led;
*LED = led;
}while((int)vector->theta != (int)targetAngle && vector->theta > vector->lowerlimit && vector->theta < vector->upperlimit && *SWITCHES != 0);
*LED = 0;
*ADDR_JP1PORT = 0;
}
void moveArmToCoordinates(RoboticArm *arm, double theta, double radius, double height) {
Vector3D targetPosition = cylindricalToCartesian(theta, radius, height);
double L1 = arm->joints[0].length;
double L2 = arm->joints[1].length;
double L3 = arm->joints[2].length;
double x = targetPosition.x;
double y = targetPosition.y;
double z = targetPosition.z;
// Calculate theta1
//arm->joints[0].theta = atan2(y, x);
moveMotor(&arm->joints[0], atan2(y, x), 0);
// Calculate theta2
double D = (x * x + y * y + (z - L1) * (z - L1) - L2 * L2 - L3 * L3) / (2 * L2 * L3);
//arm->joints[2].theta = atan2(-sqrt(1 - D * D), D);
moveMotor(&arm->joints[2], atan2(-sqrt(1 - D * D), D), 2);
double c1 = cos(arm->joints[0].theta);
double s1 = sin(arm->joints[0].theta);
double c3 = cos(arm->joints[2].theta);
double s3 = sin(arm->joints[2].theta);
double A = L2 + L3 * c3;
double B = L3 * s3;
double C = z - L1;
double theta2_1 = atan2(C, sqrt(x * x + y * y)) - atan2(B, A);
double theta2_2 = atan2(C, -sqrt(x * x + y * y)) - atan2(B, A);
// Choose the solution closest to the previous angle to avoid joint flips
if (fabs(theta2_1 - arm->joints[1].theta) < fabs(theta2_2 - arm->joints[1].theta))
moveMotor(&arm->joints[1], theta2_1, 1);
else
moveMotor(&arm->joints[1], theta2_2, 1);
}
int main () {
//init interface directions
*(ADDR_JP2PORT+4) = 0xffffffff; //set every JP2 bit direction to output
*(ADDR_JP1PORT+4) = 0xffffffff; //set every JP1 bit dir to output
clear_screen();
background();
int x = 0;
int y = 50;
char* hw;
unsigned char byte1 = 0;
unsigned char byte2 = 0;
unsigned char byte3 = 0;
long PBval;
int checkswitch;
int sw;
int PS2_data, RVALID;
bool turned = false;
RoboticArm arm;
// Initialize arm parameters
arm.joints[0].length = 0;
arm.joints[0].theta = 0;
arm.joints[0].upperlimit = 180;
arm.joints[0].lowerlimit = -180;
arm.joints[1].length = 225;
arm.joints[1].theta = 90;
arm.joints[1].upperlimit = 95;
arm.joints[1].lowerlimit = 85;
arm.joints[2].length = 150;
arm.joints[2].theta = 0;
arm.joints[2].upperlimit = 135;
arm.joints[2].lowerlimit = -135;
while(1){
int checkswitch = *(SWITCHES);
if(checkswitch == 0b1000000){
moveArmToCoordinates(&arm, 0.0, 0.0, 300.0);
moveArmToCoordinates(&arm, 0.0, 0.0, 375.0);
}
if (checkswitch != 0){
if(!turned){
sw = hextoint(checkswitch);
motor(sw, &arm);
turned = true;
}else{
sw = 0;
}
}else{
turned = false;
}
}
return 0;
}
int motornumber(int sw){
if(sw == 0 || sw ==1){
return 1;
}
if (sw == 2 || sw ==3){
return 2;
}
if (sw == 4 || sw ==5){
return 3;
}
}
int inttohex(int sw){
if(sw == 0){
return 0b1;
}
if(sw == 1){
return 0b10;
}
if(sw == 2){
return 0b100;
}
if(sw == 3){
return 0b1000;
}
if(sw == 4){
return 0b10000;
}
if(sw == 5){
return 0b100000;
}
}
int hextoint(int checkswitch){
if(checkswitch == 0b1){
return 0;
}
if(checkswitch == 0b10){
return 1;
}
if(checkswitch == 0b100){
return 2;
}
if (checkswitch == 0b1000){
return 3;
}
if (checkswitch == 0b10000){
return 4;
}
if (checkswitch == 0b100000){
return 5;
}
}