-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.js
More file actions
263 lines (233 loc) · 10.9 KB
/
App.js
File metadata and controls
263 lines (233 loc) · 10.9 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
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
import React from 'react';
import { StyleSheet, Text, View, TouchableOpacity, Alert, TextInput } from 'react-native';
import { Feather } from '@expo/vector-icons';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import Waypoint from './Pages/segundatela';
const TOPIC = '/robot/cmd_vel';
const WAY_POINT_TOPIC = '/set_waypoint';
const ODOM = '/odom';
const MAX_LINEAR_VEL = 0.1;
const MAX_ANGULAR_VEL = 0.5;
const Stack = createStackNavigator();
function AppNavigator() {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Home" component={App} />
<Stack.Screen name="Waypoint" component={Waypoint} />
</Stack.Navigator>
</NavigationContainer>
);
}
export default AppNavigator;
class App extends React.Component {
constructor() {
super();
this.state = {
connected: false,
activated: false,
ip_address: "",
linear_vel: 0.1,
angular_vel: 0.1,
};
this.odom = "nao salvou nada";
this.socket = null;
this.emit = this.emit.bind(this);
this.handleConnectButton = this.handleConnectButton.bind(this);
}
emit = (vel, ang) => {
if (this.state.connected) {
this.socket.send(JSON.stringify({
op: 'publish',
topic: TOPIC,
msg: {
linear: { x: vel, y: 0, z: 0 },
angular: { x: 0, y: 0, z: ang }
}
}));
}
};
sub_odom = () => {
if (this.state.connected) {
this.socket.send(JSON.stringify({
op: 'subscribe',
topic: ODOM,
}));
}
};
pub_waypoint = (waypoint) => {
if (this.state.connected) {
this.socket.send(JSON.stringify({
op: 'publish',
topic: WAY_POINT_TOPIC,
msg: {
data: waypoint
}
}));
}
};
handleConnectButton = () => {
try {
if (this.state.connected) {
this.emit(0, 0);
this.socket.close();
}
else {
if (!this.socket || this.socket.readyState == WebSocket.CLOSED) {
this.socket = new WebSocket(`ws://${this.state.ip_address}:8080`);
this.socket.onopen = () => {
if (!this.state.connected) {
this.setState({
connected: true
});
Alert.alert('Connected!');
}
};
this.socket.onclose = () => {
if (this.state.connected) {
this.setState({
connected: false
});
Alert.alert('Disconnected!');
}
};
}
}
}
catch (err) {
console.log(err);
}
};
handleSaveWaypointButton = () => {
// Se inscreve no tópico '/odom'
this.sub_odom();
console.log("OIII");
// Define o manipulador de mensagens para pegar a posição do robô
this.socket.onmessage = (event) => {
let msg = JSON.parse(event.data);
if (msg.topic === ODOM) {
// Salva a posição do robôd
let robotPosition = msg.msg;
this.setState({odom:robotPosition});
console.log("Posição do robô: x = " + robotPosition.pose.pose.position.x + ", y = " + robotPosition.pose.pose.position.y + ", z = " + robotPosition.pose.pose.position.z);
// Faz algo com a posição do robô (por exemplo, salva em um estado ou banco de dados)
// Cancela a inscrição do tópico '/odom' após receber a posição do robô
this.socket.send(JSON.stringify({
op: 'unsubscribe',
topic: ODOM
}));
}
};
};
render() {
return (
<View style={styles.container}>
<View style={{ width: '35%' }} flexDirection={'column'} alignItems={'center'} justifyContent={'center'} gap={10} paddingLeft={'10%'}>
<View flexDirection={'row'} gap={10}>
<TouchableOpacity style={styles.buttonBox} onPress={() => this.emit(this.state.linear_vel, this.state.angular_vel)}><Feather name="arrow-up-left" size={40} color="#fff" /></TouchableOpacity>
<TouchableOpacity style={styles.buttonBox} onPress={() => this.emit(this.state.linear_vel, 0)}><Feather name="arrow-up" size={48} color="#fff" /></TouchableOpacity>
<TouchableOpacity style={styles.buttonBox} onPress={() => this.emit(this.state.linear_vel, -this.state.angular_vel)}><Feather name="arrow-up-right" size={40} color="#fff" /></TouchableOpacity>
</View>
<View flexDirection={'row'} gap={10}>
<TouchableOpacity style={styles.buttonBox} onPress={() => this.emit(0, this.state.angular_vel)}><Feather name="arrow-left" size={48} color="#fff" /></TouchableOpacity>
<TouchableOpacity style={styles.buttonBox} onPress={() => this.emit(0, 0)}><Feather name="circle" size={48} color="#fff" /></TouchableOpacity>
<TouchableOpacity style={styles.buttonBox} onPress={() => this.emit(0, -this.state.angular_vel)}><Feather name="arrow-right" size={48} color="#fff" /></TouchableOpacity>
</View>
<View flexDirection={'row'} gap={10}>
<TouchableOpacity style={styles.buttonBox} onPress={() => this.emit(-this.state.linear_vel, -this.state.angular_vel)}><Feather name="arrow-down-left" size={40} color="#fff" /></TouchableOpacity>
<TouchableOpacity style={styles.buttonBox} onPress={() => this.emit(-this.state.linear_vel, 0)}><Feather name="arrow-down" size={48} color="#fff" /></TouchableOpacity>
<TouchableOpacity style={styles.buttonBox} onPress={() => this.emit(-this.state.linear_vel, this.state.angular_vel)}><Feather name="arrow-down-right" size={40} color="#fff" /></TouchableOpacity>
</View>
</View>
<View style={{ width: '30%', flexDirection: 'column-reverse', paddingLeft: '5%' }}>
<View style={{ height: '70%' }} alignItems={'center'}>
<View style={{ paddingBottom: 20, width: "80%" }}
>
<TextInput
multiline={false}
onChangeText={(ip_address) => this.setState({ ip_address })}
placeholder='IP ADDRESS'
defaultValue='172.20.10.2'
style={{
fontSize: 30,
borderBottomColor: '#000000',
borderBottomWidth: 1
}}
/>
</View>
<TouchableOpacity
onPress={this.state.ip_address !== '' ? this.handleConnectButton : () => Alert.alert('Please insert IP ADDRESS!')}
style={styles.connectButton}
>
<Text style={styles.buttonText}>{this.state.connected ? 'Disconnect' : 'Connect'}</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.waypointButton} onPress={() => this.props.navigation.navigate('Waypoint')}>
<Text style={styles.buttonText}>Waypoint</Text>
</TouchableOpacity>
{/*
<TouchableOpacity style={styles.waypointButton} onPress={() => this.pub_waypoint("LE-1")}>
<Text style={styles.buttonText}>Waypoint</Text>
</TouchableOpacity>
*/}
<TouchableOpacity style={styles.waypointButton} onPress={() => this.handleSaveWaypointButton()}>
<Text style={styles.buttonText}>{this.odom}</Text>
</TouchableOpacity>
</View>
</View>
<View style={{ width: '30%'}}>
<Text style={styles.velText}>Linear Vel: {(this.state.linear_vel).toFixed(1)}</Text>
<View flexDirection={'row'} gap={5} justifyContent={'center'}>
<TouchableOpacity style={styles.buttonBox} onPress={() => this.setState({ linear_vel: (this.state.linear_vel + 0.1) })}><Feather name="arrow-up" size={48} color="#fff" /></TouchableOpacity>
<TouchableOpacity style={styles.buttonBox} onPress={() => this.setState({ linear_vel: (this.state.linear_vel - 0.1) })}><Feather name="arrow-down" size={48} color="#fff" /></TouchableOpacity>
</View>
<Text style={styles.velText}>Angular Vel: {(this.state.angular_vel).toFixed(1)}</Text>
<View flexDirection={'row'} gap={5} justifyContent={'center'}>
<TouchableOpacity style={styles.buttonBox} onPress={() => this.setState({ angular_vel: (this.state.angular_vel + 0.1) })}><Feather name="arrow-up" size={48} color="#fff" /></TouchableOpacity>
<TouchableOpacity style={styles.buttonBox} onPress={() => this.setState({ angular_vel: (this.state.angular_vel - 0.1) })}><Feather name="arrow-down" size={48} color="#fff" /></TouchableOpacity>
</View>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
flexDirection: 'row',
},
buttonBox: {
width: 75,
height: 75,
backgroundColor: '#195AA5',
alignItems: 'center',
justifyContent: 'center',
borderRadius: 10,
},
connectButton: {
borderRadius: 60,
width: 150,
padding: 30,
backgroundColor: '#195AA5',
marginBottom: 10
},
waypointButton: {
borderRadius: 60,
width: 150,
padding: 30,
backgroundColor: '#195AA5',
},
buttonText: {
color: '#fff',
fontSize: 20,
textAlign: 'center',
fontWeight: 'bold'
},
velText: {
fontSize: 20,
textAlign: 'center',
fontWeight: 'bold'
},
});