-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunitree.js
151 lines (142 loc) · 5.45 KB
/
unitree.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
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
const { Action, ActionStates, ActionTypes } = require('./action.js');
class Unitree_Go1 {
constructor(node, io) {
this.state = { vx: -0.2, vy: 0.13, vz: 0}
this.current = { vx: -0.2, vy: 0.13, vz: 0}
this.node = node;
this.io = io;
this.unitreePublisher = node.createPublisher('std_msgs/msg/Float32MultiArray', 'vel_command');
this.actionNow = null;
// this.actionQueue = [];
setInterval(() => {
this.current.vx = this.current.vx + (this.state.vx - this.current.vx) * 1;
this.current.vy = this.current.vy + (this.state.vy - this.current.vy) * 0.5;
this.current.vz = this.current.vz + (this.state.vz - this.current.vz) * 0.5;
this.SendUnitree(this.current);
},100)
//this.AddAction(new Action(ActionTypes.FORWARD, -0.6, ActionStates.WAITING));
//this.AddAction(new Action(ActionTypes.TURN, 45, ActionStates.WAITING));
}
Reset() {
this.state = { vx: -0.2, vy: 0.13, vz: 0}
}
SendUnitree(msg) {
this.unitreePublisher.publish({
data:[msg.vx, msg.vy, msg.vz]
})
}
GetRecognition(recognition) {
//console.log('Unitree: Recognition:', recognition);
this.recognitionRecv = true;
this.itemList = recognition;
}
Forward(value) {
var time = 0;
if(value < 0) {
time = -value/0.28;
this.state.vx = -0.4;
//this.io.emit('Speech', '正在后退');
}
else {
time = value/0.4;
this.state.vx = 0.4;
//this.io.emit('Speech', '正在前进');
}
return new Promise((resolve) => {
setTimeout(() => {
this.Reset();
setTimeout(() => {
resolve();
}, 500)
}, time * 1000)
})
}
Turn(value) {
var time = value / 180 * 3.14 /1 * 1.05;//defualt speed
if(time < 0) {
this.state.vz = -1;
time = -time;
//this.io.emit('Speech', '正在右转');
}
else {
this.state.vz = 1;
//this.io.emit('Speech', '正在左转');
}
this.SendUnitree(this.state);
return new Promise((resolve) => {
setTimeout(() => {
this.Reset();
resolve();
}, time * 1000)
})
}
ActionHandler(action) {
// if(this.actionNow != null && this.actionNow.state == ActionStates.EXCUTING) {
// //console.log('Unitree: Action excuting:', this.actionNow);
// return;
// }
// if(this.actionQueue.length > 0) {
// this.actionNow = this.actionQueue.shift();
// }
// else {return;}
// console.log('Unitree: Action:', this.actionNow);
if(action.type == ActionTypes.UFORWARD) {
action.state = ActionStates.EXCUTING;
this.Forward(action.value).then(() => {
action.state = ActionStates.COMPLETED;
console.log('Unitree: Action completed:', action);
})
}
else if(action.type == ActionTypes.UTURN) {
action.state = ActionStates.EXCUTING;
this.Turn(action.value).then(() => {
action.state = ActionStates.COMPLETED;
console.log('Unitree: Action completed:', action);
})
}
// else if(action.type == ActionTypes.USEARCH) {
// action.state = ActionStates.EXCUTING;
// console.log('Unitree: Searching:', action.value);
// var itemName = action.value;
// this.itemList = [];
// this.recognitionRecv = false;
// this.io.emit('RequestRecognition');
// this.io.emit('Speech', '正在寻找' + itemName);
// var flag=0;
// var interval = setInterval(() => {
// if(this.recognitionRecv) {
// //console.log('Unitree: Search item:',this.itemList);
// this.itemList.forEach(item => {
// if(item['name'] == itemName) {
// flag=1;
// console.log('Unitree: Find an item:', itemName);
// if(item['position'][2] > 0.4) {
// console.log('Unitree: Item too far');
// this.io.emit('Speech', `物品离我太远了,我要走近点`);
// }
// else {
// this.io.emit('Speech', `找到了${itemName}`);
// }
// }
// })
// if(flag==0){
// console.log('Sagi: Item not found');
// this.io.emit('Speech', `没有找到`);
// }
// action.state = ActionStates.COMPLETED;
// clearInterval(interval);
// }
// }, 200)
// setTimeout(() => {
// if(this.recognitionRecv == false) {
// this.io.emit('Speech', '我的眼睛好像出了点问题');
// console.log('Unitree: Search timeout');
// clearInterval(interval);
// }
// }, 4000)
//}
}
}
module.exports = {
Unitree_Go1
}