Skip to content
Snippets Groups Projects
Commit 2472f0a2 authored by Andrew Tridgell's avatar Andrew Tridgell
Browse files

SITL: increase the amount of noise in the simulated ADC

this increases the noise to 2 bits, which actually can have the effect
of improving accuracy, as it leads to better averaging
parent 9602b1f9
No related branches found
No related tags found
No related merge requests found
...@@ -8,6 +8,15 @@ ...@@ -8,6 +8,15 @@
#include <stdlib.h> #include <stdlib.h>
#define NOISE_BITS 4
static inline float noise_generator(void)
{
float noise = ((unsigned)random()) & ((1<<NOISE_BITS)-1);
noise -= 0.5*(1<<NOISE_BITS);
return noise;
}
// this implements the UDR2 register // this implements the UDR2 register
struct ADC_UDR2 { struct ADC_UDR2 {
uint16_t value, next_value; uint16_t value, next_value;
...@@ -42,8 +51,7 @@ struct ADC_UDR2 { ...@@ -42,8 +51,7 @@ struct ADC_UDR2 {
default: return *this; default: return *this;
} }
idx = 1; idx = 1;
int noise = (((unsigned long)random()) % 3) - 1; next_value = (unsigned)(next_analog + noise_generator() + 0.5);
next_value = (unsigned)(next_analog + noise + 0.5);
if (next_value >= 0x1000) { if (next_value >= 0x1000) {
next_value = 0xFFF; next_value = 0xFFF;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment