This repository has been archived by the owner on Nov 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
75 lines (63 loc) · 2.29 KB
/
sketch.js
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
let b = 80*4, c = 35*4;
let A = 130, B = 40, C = 0, O = -80;
let h = 150, min = 10, max = 450;
let socket;
let legs = [];
function setup(){
angleMode(DEGREES)
createCanvas(window.innerWidth, window.innerHeight);
socket = io('http://192.168.0.31:5050');
// legs[0] = new Leg((servo, pos) => socket.emit("servo", [servo,90]), 255, 1, 0, width/2-300);
// legs[1] = new Leg((servo, pos) => socket.emit("servo", [servo,90]), 150, 3, 2, width/2-300);
// legs[2] = new Leg((servo, pos) => socket.emit("servo", [servo,90]), 255, 6, 7, width/2+300);
// legs[3] = new Leg((servo, pos) => socket.emit("servo", [servo,pos]), 150, 4, 5, width/2+300);
legs[0] = new Leg((servo, pos) => socket.emit("servo", [servo,pos]), 255, 1, 0, width/2-300);
legs[1] = new Leg((servo, pos) => socket.emit("servo", [servo,180-pos]), 150, 3, 2, width/2-300);
legs[2] = new Leg((servo, pos) => socket.emit("servo", [servo,180-pos]), 255, 6, 7, width/2+300);
legs[3] = new Leg((servo, pos) => socket.emit("servo", [servo,pos+(servo==5?0:20)]), 150, 4, 5, width/2+300);
}
var steps = [];
let speed = 15;
for(i = 0; i < 4; i++){
steps[i] = {
actual: 0,
mult: 1,
get step(){
if(this.actual < 180) {
this.actual += speed/this.mult;
} else {
this.actual += speed;
}
if(this.actual > 360) this.actual = 0;
return this.actual;
},
set step(s){
this.actual = s;
}
}
}
let gaitTypes = {
'run': [3, 0, 230, 50, 280],
'amble': [1, 0, 180, 40, 220],
'walk': [1, 0, 160, 80, 240],
'pace': [3, 0, 180, 0, 180],
'trot': [1.7, 0, 180, 180, 0],
'canter': [3, 0, 180, 0, 50]
}
let gaitType = 'trot';
let legSpeeds = [6, 6, 6, 6];
for(i=0;i<4;i++)steps[i].mult = gaitTypes[gaitType][0], steps[i].step = gaitTypes[gaitType][i+1];
let he = 60;
function makeEllipseGait(step, id){
var r = 50;
let px = legSpeeds[id]*r*cos(step) + 2.5*r*cos(step+140);
let py = height/2+ he+r + r*sin(step) + 0.5*r*sin(step);
return [px + (id < 2? 100 : -100), py];
}
function draw(){
background(50);
for(i = legs.length-1; i >= 0; i--){
legs[i].draw(...makeEllipseGait(steps[i].step, i));
}
legs.forEach(leg => leg.emitSocket());
}