Compare commits

...

2 Commits

Author SHA1 Message Date
c79abb1be9
Add peak detection script 2021-08-28 23:35:43 +02:00
f88a987204
Add initial MCU software 2021-08-28 23:20:59 +02:00
4 changed files with 287 additions and 0 deletions

24
microcontroller/ctrl.py Normal file
View File

@ -0,0 +1,24 @@
import serial
channels = [
192, # pan
0, # tilt
134, # dimmer
255, # R
255, # G
255, # B
0, # W
1, # movement speed
0, # RST
]
ser = serial.Serial("/dev/ttyUSB0", 115200)
payload = bytearray()
payload.extend(5 * [0])
payload.extend(channels)
ser.write(payload)
ser.flush()
print(ser.read_all())

View File

@ -0,0 +1,106 @@
// Arduino Beat Detector By Damian Peckett 2015
// License: Public Domain.
// Our Global Sample Rate, 5000hz
#define SAMPLEPERIODUS 200
// defines for setting and clearing register bits
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
bool beat_bool=true;
void setup() {
Serial.begin(9600);
// Set ADC to 77khz, max for 10bit
sbi(ADCSRA,ADPS2);
cbi(ADCSRA,ADPS1);
cbi(ADCSRA,ADPS0);
//The pin with the LED
// pinMode(2, OUTPUT);
}
// 20 - 200hz Single Pole Bandpass IIR Filter
float bassFilter(float sample) {
static float xv[3] = {0,0,0}, yv[3] = {0,0,0};
xv[0] = xv[1]; xv[1] = xv[2];
xv[2] = (sample) / 3.f; // change here to values close to 2, to adapt for stronger or weeker sources of line level audio
yv[0] = yv[1]; yv[1] = yv[2];
yv[2] = (xv[2] - xv[0])
+ (-0.7960060012f * yv[0]) + (1.7903124146f * yv[1]);
return yv[2];
}
// 10hz Single Pole Lowpass IIR Filter
float envelopeFilter(float sample) { //10hz low pass
static float xv[2] = {0,0}, yv[2] = {0,0};
xv[0] = xv[1];
xv[1] = sample / 50.f;
yv[0] = yv[1];
yv[1] = (xv[0] + xv[1]) + (0.9875119299f * yv[0]);
return yv[1];
}
// 1.7 - 3.0hz Single Pole Bandpass IIR Filter
float beatFilter(float sample) {
static float xv[3] = {0,0,0}, yv[3] = {0,0,0};
xv[0] = xv[1]; xv[1] = xv[2];
xv[2] = sample / 2.7f;
yv[0] = yv[1]; yv[1] = yv[2];
yv[2] = (xv[2] - xv[0])
+ (-0.7169861741f * yv[0]) + (1.4453653501f * yv[1]);
return yv[2];
}
void loop() {
unsigned long time = micros(); // Used to track rate
float sample, value, envelope, beat, thresh;
unsigned char i;
for(i = 0;;++i){
// Read ADC and center so +-512
sample = (float)analogRead(1)-503.f;
// Filter only bass component
value = bassFilter(sample);
// Take signal amplitude and filter
if(value < 0)value=-value;
envelope = envelopeFilter(value);
// Every 200 samples (25hz) filter the envelope
if(i == 200) {
// Filter out repeating bass sounds 100 - 180bpm
beat = beatFilter(envelope);
// Threshold it based on potentiometer on AN1
thresh = 0.02f * (float)analogRead(1);
// If we are above threshold, light up LED
if(beat > thresh) {
if(beat_bool==true){
//digitalWrite(2, HIGH);
Serial.println("beat");
beat_bool=false;
}
}
else {
beat_bool=true;
Serial.println("-");// digitalWrite(2, LOW);
}
//Reset sample counter
i = 0;
}
// Consume excess clock cycles, to keep at 5000 hz
for(unsigned long up = time+SAMPLEPERIODUS; time > 20 && time < up; time = micros());
}
}

70
microcontroller/test.ino Normal file
View File

@ -0,0 +1,70 @@
unsigned long tic_loop = 0;
const unsigned int FRAME_TIME = 25; // 20 ms -> 50 FPS
byte channels_buffer[512] = {0};
byte read_buffer[9] = {0};
size_t bytes_read = 0;
const unsigned int START_ADDR = 10;
const unsigned int NUM_CHANNELS = 9;
unsigned int bytes_to_write = START_ADDR + NUM_CHANNELS;
void setup()
{
Serial.begin(115200); // USB
while (!Serial.available());
Serial.println();
Serial.println("INIT");
Serial1.begin(250000, SERIAL_8N2); // DMX
tic_loop = millis();
}
void loop()
{
update_buffer();
// this section gets executed at a maximum rate of around 40Hz
if ((millis() - tic_loop) > FRAME_TIME)
{
tic_loop = millis();
send_dmx_header();
Serial1.write(channels_buffer, bytes_to_write);
}
delay(1);
}
void update_buffer()
{
if (!Serial.available()) return;
Serial.read(read_buffer + bytes_read, 1);
Serial.print(".");
bytes_read += 1;
if (bytes_read == NUM_CHANNELS) {
bytes_read = 0;
memcpy(channels_buffer + START_ADDR - 1, read_buffer, NUM_CHANNELS);
Serial.println();
Serial.println("Updated.");
}
}
void send_dmx_header()
{
Serial1.flush();
Serial1.begin(90000, SERIAL_8N2);
while (Serial1.available())
Serial1.read();
// send the break as a "slow" byte
Serial1.write(0);
// switch back to the original baud rate
Serial1.flush();
Serial1.begin(250000, SERIAL_8N2);
while (Serial1.available())
Serial1.read();
Serial1.write(0); // Start-Byte
}

View File

@ -0,0 +1,87 @@
unsigned long tic_loop = 0;
const unsigned int FRAME_TIME = 25; // 20 ms -> 50 FPS
byte channels_buffer[512] = {0};
unsigned int bytes_to_write = 512;
const unsigned int START_ADDR = 10;
const unsigned int NUM_CHANNELS = 9;
byte dmx_data[] = {
0,
0,
134,
255,
0,
0,
0,
0,
0,
};
void setup()
{
Serial.begin(115200); // USB
Serial1.begin(250000, SERIAL_8N2); // DMX
tic_loop = millis();
for (int i = 0; i < NUM_CHANNELS; ++i)
{
channels_buffer[START_ADDR - 1 + i] = dmx_data[i];
}
}
void loop()
{
// update_buffer();
// this section gets executed at a maximum rate of around 40Hz
if ((millis() - tic_loop) > FRAME_TIME)
{
tic_loop = millis();
send_dmx_header();
Serial1.write(channels_buffer, bytes_to_write);
}
delay(1);
}
void update_buffer()
{
int n = Serial.available();
if (n < 1)
return;
n -= 1;
if (Serial.read() == n)
{
Serial.read(channels_buffer, n);
bytes_to_write = n;
}
else
{
// incomplete
while (Serial.available())
Serial.read();
}
}
void send_dmx_header()
{
Serial1.flush();
Serial1.begin(90000, SERIAL_8N2);
while (Serial1.available())
Serial1.read();
// send the break as a "slow" byte
Serial1.write(0);
// switch back to the original baud rate
Serial1.flush();
Serial1.begin(250000, SERIAL_8N2);
while (Serial1.available())
Serial1.read();
Serial1.write(0); // Start-Byte
}