This repository has been archived by the owner on Jul 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSimulator.py
106 lines (84 loc) · 3.49 KB
/
Simulator.py
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
import math
import random
import time
from robocluster import Device
from libraries.GPS.GPSPosition import GPSPosition
from libraries.differential_drive import diff_drive_fk
import config
class Rover:
def __init__(self):
self.position = GPSPosition(51.470889, -112.733622)
self.prev_pos = self.position
self.heading = 0 # degress, north = 0
self.wheelspeed = [0, 0]
self.prev_wheelspeed = self.wheelspeed
self.wheel_radius = 0.26 # meters
self.wheel_axis = 1
self.velocity = [0,0]
self.acceleration = [0,0]
def update(self, dt):
delta_wheelspeed = [self.wheelspeed[0]-self.prev_wheelspeed[0],
self.wheelspeed[1]-self.prev_wheelspeed[1]]
next_pose = diff_drive_fk(0, 0,
self.wheel_axis,
-math.radians(self.heading)+math.pi/2,
self.wheelspeed[0],
self.wheelspeed[1],
dt)
bearing = -math.degrees(math.atan2(next_pose[1], next_pose[0]) - math.pi/2)
distance = math.sqrt(next_pose[0]**2 + next_pose[1]**2)
n_gps = self.position.gpsPosition(bearing, distance)
n_velocity = [next_pose[0]*dt, next_pose[1]*dt]
self.acceleration = [n_velocity[0]-self.velocity[0],
n_velocity[1]-self.velocity[1]]
self.position = n_gps
self.heading = -math.degrees(next_pose[2] - math.pi/2)%360
self.prev_wheelspeed = self.wheelspeed
self.velocity = n_velocity
def simulate_piksi(gpsPosition):
stddev_lat = 1.663596084712623e-05
stddev_lon = 2.1743680968892167e-05
return [random.gauss(gpsPosition.lat, stddev_lat),
random.gauss(gpsPosition.lon, stddev_lon)]
def simulate_bno(accel):
stddev_x = 0.025842126805189967
stddev_y = 0.03368775942186025
stddev_z = 0.038244224565556637
return [random.gauss(accel[0], stddev_x),
random.gauss(accel[1], stddev_y)]
simDevice = Device('simDevice', 'rover', network=config.network)
log = config.getLogger()
# Rover parameters
simDevice.storage.rover = Rover()
simDevice.storage.rover.wheelspeed = [0, 0]
simDevice.storage.rover.heading = 15.4
DELTA_T = 0.1
@simDevice.every(DELTA_T)
async def update():
simDevice.storage.rover.update(DELTA_T)
# simDevice.storage.rover.wheelspeed = [x+0.1 for x in simDevice.storage.rover.wheelspeed]
log.debug('position: {}'.format(simDevice.storage.rover.position))
log.debug('wheel speed: {}'.format(simDevice.storage.rover.wheelspeed))
log.debug('heading: {}'.format(simDevice.storage.rover.heading))
log.debug('accleration: {}'.format(simDevice.storage.rover.acceleration))
@simDevice.every(DELTA_T)
async def publish_state():
position = simDevice.storage.rover.position
pos_list = [position.lat, position.lon]
noisy_pos = simulate_piksi(position)
# print(noisy_pos)
# await simDevice.publish("GPSPosition", pos_list)
await simDevice.publish("singlePointGPS", noisy_pos)
accel = simDevice.storage.rover.acceleration
accel = simulate_bno(accel)
# print(accel)
await simDevice.publish("Acceleration", accel)
await simDevice.publish('roverHeading', simDevice.storage.rover.heading)
@simDevice.on('*/wheelLF')
def update_wheel_l(event, data):
simDevice.storage.rover.wheelspeed[0] = data['SetRPM']/(12*19*60)
@simDevice.on('*/wheelRF')
def update_wheel_l(event, data):
simDevice.storage.rover.wheelspeed[1] = data['SetRPM']/(12*19*60)
simDevice.start()
simDevice.wait()