This repository has been archived by the owner on Sep 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
dw1000_ranging_tag.rs
199 lines (164 loc) · 5.67 KB
/
dw1000_ranging_tag.rs
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
//! Range measurement tag node
//!
//! This is a tag node used for range measurement. Tags use anchor nodes to
//! measure their distance from those anchors.
//!
//! Currently, distance measurements have a highly inaccurate result. One reason
//! that could account for this is the lack of antenna delay calibration, but
//! it's possible that there are various hidden bugs that contribute to this.
#![no_main]
#![no_std]
extern crate panic_semihosting;
use cortex_m_rt::entry;
use dwm1001::{
prelude::*,
debug,
dw1000::{
RxConfig,
mac,
ranging::{
self,
Message as _RangingMessage,
},
},
nrf52832_hal::{
gpio::{
p0::P0_17,
Output,
PushPull,
},
nrf52832_pac::SPIM2,
Delay,
Spim,
},
DWM1001,
block_timeout,
print,
};
#[entry]
fn main() -> ! {
debug::init();
let mut dwm1001 = DWM1001::take().unwrap();
let mut delay = Delay::new(dwm1001.SYST);
let mut rng = dwm1001.RNG.constrain();
dwm1001.DW_RST.reset_dw1000(&mut delay);
let mut dw1000 = dwm1001.DW1000.init()
.expect("Failed to initialize DW1000");
dw1000.enable_tx_interrupts()
.expect("Failed to enable TX interrupts");
dw1000.enable_rx_interrupts()
.expect("Failed to enable RX interrupts");
let mut dw_irq = dwm1001.DW_IRQ;
let mut nvic = dwm1001.NVIC;
let mut gpiote = dwm1001.GPIOTE;
// These are the hardcoded calibration values from the dwm1001-examples
// repository[1]. Ideally, the calibration values would be determined using
// the proper calibration procedure, but hopefully those are good enough for
// now.
//
// [1] https://github.com/Decawave/dwm1001-examples
dw1000.set_antenna_delay(16456, 16300)
.expect("Failed to set antenna delay");
// Set network address
dw1000
.set_address(
mac::PanId(0x0d57), // hardcoded network id
mac::ShortAddress(rng.random_u16()), // random device address
)
.expect("Failed to set address");
let mut timeout_timer = dwm1001.TIMER1.constrain();
let mut buf = [0; 128];
loop {
let mut receiving = dw1000
.receive(RxConfig::default())
.expect("Failed to receive message");
timeout_timer.start(500_000u32);
let message = block_timeout!(&mut timeout_timer, {
dw_irq.wait_for_interrupts(
&mut nvic,
&mut gpiote,
&mut timeout_timer,
);
receiving.wait(&mut buf)
});
dw1000 = receiving.finish_receiving()
.expect("Failed to finish receiving");
let message = match message {
Ok(message) => message,
Err(_) => continue, //ignore error
};
let ping =
ranging::Ping::decode::<
Spim<SPIM2>,
P0_17<Output<PushPull>>,
>(&message)
.expect("Failed to decode ping");
if let Some(ping) = ping {
// Received ping from an anchor. Reply with a ranging
// request.
dwm1001.leds.D10.enable();
delay.delay_ms(10u32);
dwm1001.leds.D10.disable();
// Wait for a moment, to give the anchor a chance to start listening
// for the reply.
delay.delay_ms(100u32);
let mut sending = ranging::Request::new(&mut dw1000, &ping)
.expect("Failed to initiate request")
.send(dw1000)
.expect("Failed to initiate request transmission");
timeout_timer.start(500_000u32);
block_timeout!(&mut timeout_timer, {
dw_irq.wait_for_interrupts(
&mut nvic,
&mut gpiote,
&mut timeout_timer,
);
sending.wait()
})
.expect("Failed to send ranging request");
dw1000 = sending.finish_sending()
.expect("Failed to finish sending");
continue;
}
let response =
ranging::Response::decode::<
Spim<SPIM2>,
P0_17<Output<PushPull>>,
>(&message)
.expect("Failed to decode response");
if let Some(response) = response {
// Received ranging response from anchor. Now we can compute the
// distance.
dwm1001.leds.D11.enable();
delay.delay_ms(10u32);
dwm1001.leds.D11.disable();
// If this is not a PAN ID and short address, it doesn't
// come from a compatible node. Ignore it.
let (pan_id, addr) = match response.source {
mac::Address::Short(pan_id, addr) =>
(pan_id, addr),
_ =>
continue,
};
// Ranging response received. Compute distance.
match ranging::compute_distance_mm(&response) {
Some(distance_mm) => {
dwm1001.leds.D9.enable();
delay.delay_ms(10u32);
dwm1001.leds.D9.disable();
print!("{:04x}:{:04x} - {} mm\n",
pan_id.0,
addr.0,
distance_mm,
);
}
None => {
print!("Distance too large; can't compute");
continue;
}
}
continue;
}
print!("Ignored message that was neither ping nor response\n");
}
}