2024-09-03 15:04:59 +02:00
|
|
|
// Dependencies:
|
|
|
|
// - SLIPDecoder (https://git.kompot.si/g1smo/SLIPDecoder)
|
|
|
|
// - MathLib (https://depts.washington.edu/dxscdoc/Help/Browse.html#Libraries%3EMathLib)
|
2024-09-18 13:56:49 +02:00
|
|
|
// - OSCRecorder
|
2024-09-03 15:04:59 +02:00
|
|
|
|
2024-09-18 13:56:49 +02:00
|
|
|
Quarks.install("https://github.com/cappelnord/OSCRecorder.git");
|
|
|
|
|
|
|
|
OSCRecorderGUI();
|
|
|
|
NetAddr.langPort;
|
|
|
|
OSCFunc.trace(true);
|
2024-09-26 19:40:14 +02:00
|
|
|
OSCFunc.trace(false);
|
2024-10-21 23:24:59 +02:00
|
|
|
|
|
|
|
Platform.userExtensionDir; // Extensions available only to your user account
|
|
|
|
"AHRSensor.sc".resolveRelative.load;
|
|
|
|
AHRSensor;
|
|
|
|
s.boot;
|
|
|
|
|
2024-09-03 15:04:59 +02:00
|
|
|
(
|
|
|
|
|
2024-09-26 19:40:14 +02:00
|
|
|
// To send OSC
|
|
|
|
~n = NetAddr("127.0.0.1", 57120);
|
|
|
|
|
2024-09-03 15:04:59 +02:00
|
|
|
// Initialize the the receiver via SLIP decoder
|
2024-09-18 13:56:49 +02:00
|
|
|
~receiverPath = "/dev/ttyACM0";
|
2024-09-26 19:40:14 +02:00
|
|
|
//~baudRate = 115200;
|
2024-09-18 13:56:49 +02:00
|
|
|
~baudRate = 230400;
|
2024-09-03 15:04:59 +02:00
|
|
|
|
2024-09-06 18:19:53 +02:00
|
|
|
OSCFunc.trace(true); // debug osc
|
|
|
|
OSCFunc.trace(false);
|
2024-09-03 15:04:59 +02:00
|
|
|
|
|
|
|
/*******
|
|
|
|
* GUI *
|
|
|
|
******/
|
2024-09-06 18:19:53 +02:00
|
|
|
|
|
|
|
// Indeksirani so z IDjem
|
|
|
|
~senzorji = Dictionary.new;
|
2024-09-18 13:56:49 +02:00
|
|
|
// Indeksirani so s parent IDjem
|
|
|
|
~starsi = Dictionary.new;
|
2024-09-06 18:19:53 +02:00
|
|
|
|
|
|
|
// Dodaj senzorje
|
2024-09-18 13:56:49 +02:00
|
|
|
// Pari: parent/child; core nima parenta, ostali pa imajo "relativno rotacijo", ki je odvisna od jedra
|
|
|
|
|
|
|
|
~razmerja = [
|
|
|
|
// Core
|
|
|
|
[1, nil],
|
2024-09-26 19:40:14 +02:00
|
|
|
// Leva/desna roka
|
|
|
|
[3, 1],
|
|
|
|
[4, 1],
|
|
|
|
// Leva/desna rama
|
|
|
|
[5, 1],
|
|
|
|
[6, 1]
|
|
|
|
// Testni
|
|
|
|
//[7, 1]
|
|
|
|
/*
|
|
|
|
[6, 4]
|
|
|
|
*/
|
|
|
|
// Test
|
|
|
|
//[2, nil],
|
|
|
|
//[7, 2]
|
|
|
|
];
|
|
|
|
|
|
|
|
// Senzorji
|
|
|
|
~razmerja.do({ |par|
|
|
|
|
var s = AHRSensor.new(par[0]);
|
2024-09-18 13:56:49 +02:00
|
|
|
~senzorji.put(par[0], s);
|
2024-09-26 19:40:14 +02:00
|
|
|
});
|
|
|
|
// Starsi
|
|
|
|
~razmerja.do({ |par|
|
|
|
|
if (par[1] != nil) {
|
|
|
|
~senzorji[par[0]].parent = ~senzorji[par[1]];
|
|
|
|
};
|
2024-09-18 13:56:49 +02:00
|
|
|
});
|
|
|
|
/*
|
|
|
|
~senzorji.put(1, AHRSensor.new(1));
|
|
|
|
~senzorji.put(2, AHRSensor.new(2));
|
|
|
|
// Ostali (3 do 6)
|
|
|
|
((3..6)).do({ |id|
|
2024-09-06 18:19:53 +02:00
|
|
|
~senzorji.put(id, AHRSensor.new(id));
|
2024-09-03 15:04:59 +02:00
|
|
|
});
|
2024-09-18 13:56:49 +02:00
|
|
|
*/
|
2024-09-03 15:04:59 +02:00
|
|
|
|
2024-09-06 18:19:53 +02:00
|
|
|
// Olimex test
|
2024-09-18 13:56:49 +02:00
|
|
|
//~senzorji.put(9, AHRSensor.new(9));
|
2024-09-03 15:04:59 +02:00
|
|
|
|
2024-09-06 18:19:53 +02:00
|
|
|
~decoder = SLIPDecoder.new(~receiverPath);
|
2024-09-26 19:40:14 +02:00
|
|
|
~w = Window.new("Utopia || C²", Rect(300, ~senzorji.size * 60, 900, ~senzorji.size * 100),true);
|
2024-09-18 13:56:49 +02:00
|
|
|
|
|
|
|
// Midi naprava
|
|
|
|
MIDIClient.init;
|
|
|
|
MIDIClient.destinations;
|
|
|
|
~midi = MIDIOut.new(0);
|
|
|
|
~midi.connect(1);
|
2024-09-06 18:19:53 +02:00
|
|
|
|
|
|
|
~elementi = ~senzorji.values.sort({ |a, b| a.id < b.id;}).collect({|s| s.getGui;});
|
2024-09-03 15:04:59 +02:00
|
|
|
|
2024-09-18 13:56:49 +02:00
|
|
|
~startStopDecoder = { |butt|
|
|
|
|
if ((~decoder.running.not), {
|
|
|
|
// If not running, start decoder
|
|
|
|
~decoder.trace(true); // debug slip decoder
|
|
|
|
~decoder = SLIPDecoder.new(~ttyInput.string, ~baudRate);
|
|
|
|
~decoder.start;
|
2024-09-26 19:40:14 +02:00
|
|
|
~startStopBtn.string_("Stop")
|
2024-09-18 13:56:49 +02:00
|
|
|
}, {
|
|
|
|
// Else stop the decoder
|
|
|
|
~decoder.stop;
|
2024-09-26 19:40:14 +02:00
|
|
|
~startStopBtn.string_("Start")
|
2024-09-18 13:56:49 +02:00
|
|
|
};
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2024-09-26 19:40:14 +02:00
|
|
|
~startStopBtn = Button().string_("Start").action_(~startStopDecoder);
|
|
|
|
~ttyInput = TextField().string_(~receiverPath).keyDownAction_({ |input, char| if (char == $\r, { ~startStopDecoder.value; }); });
|
|
|
|
~btnRefAll = Button().string_("ref all").action_({
|
|
|
|
~senzorji.values.do({ | s | s.setRefQuat(); });
|
|
|
|
});
|
2024-09-18 13:56:49 +02:00
|
|
|
|
2024-09-03 15:04:59 +02:00
|
|
|
~w.layout_(
|
2024-09-26 19:40:14 +02:00
|
|
|
GridLayout.rows(
|
|
|
|
// HEADER; serial path, buttons
|
|
|
|
[
|
|
|
|
StaticText().string_("Serial path: "),
|
|
|
|
[~ttyInput, columns: 7],
|
|
|
|
~startStopBtn,
|
|
|
|
// Gumb za referencni quat na vseh
|
|
|
|
~btnRefAll
|
|
|
|
],
|
2024-09-06 18:19:53 +02:00
|
|
|
// Sensor rows
|
2024-09-26 19:40:14 +02:00
|
|
|
*~elementi.flatten
|
2024-09-03 15:04:59 +02:00
|
|
|
)
|
|
|
|
);
|
2024-09-06 18:19:53 +02:00
|
|
|
|
|
|
|
// On window close stop the decoder
|
|
|
|
~w.onClose_({ |w|
|
|
|
|
~decoder.stop;
|
|
|
|
});
|
2024-09-03 15:04:59 +02:00
|
|
|
~w.front;
|
|
|
|
|
2024-09-26 19:40:14 +02:00
|
|
|
// Automatic reflow
|
|
|
|
// does NOT work with layouts!
|
|
|
|
//~w.view.decorator = FlowLayout(~w.view.bounds);
|
|
|
|
|
|
|
|
// Pucamo stare listenerje
|
|
|
|
OSCdef.freeAll;
|
|
|
|
|
2024-09-06 18:19:53 +02:00
|
|
|
// OSC listeners for sensors
|
|
|
|
~senzorji.collect({ |s|
|
|
|
|
var oscHeader = "/ww/" ++ s.id,
|
|
|
|
senzor = ~senzorji[s.id];
|
|
|
|
|
|
|
|
// Quat listener
|
2024-09-26 19:40:14 +02:00
|
|
|
q = OSCdef.new((\quat ++ s.id).asSymbol, { |msg, time, addr, recvPort|
|
2024-09-06 18:19:53 +02:00
|
|
|
var q;
|
2024-09-18 13:56:49 +02:00
|
|
|
// We get X Y Z W, but supercollider has W X Y Z!
|
2024-09-26 19:40:14 +02:00
|
|
|
q = Quaternion.new(msg[1], msg[2], msg[3], msg[4]);
|
2024-09-06 18:19:53 +02:00
|
|
|
|
|
|
|
// Count quat events
|
|
|
|
Routine {
|
2024-09-18 13:56:49 +02:00
|
|
|
var quatD;
|
|
|
|
|
2024-09-26 19:40:14 +02:00
|
|
|
// Stetje dogodkov
|
2024-09-06 18:19:53 +02:00
|
|
|
senzor.eps = senzor.eps + 1;
|
2024-09-26 19:40:14 +02:00
|
|
|
|
|
|
|
// Nov quaternion!
|
|
|
|
senzor.setQuat(q);
|
2024-09-06 18:19:53 +02:00
|
|
|
senzor.refreshGuiQuat;
|
|
|
|
|
2024-09-26 19:40:14 +02:00
|
|
|
// razlika od parenta
|
|
|
|
//quatD = q;
|
|
|
|
quatD = senzor.relQuat;
|
|
|
|
|
2024-09-18 13:56:49 +02:00
|
|
|
senzor.updateEuler(quatD);
|
2024-09-26 19:40:14 +02:00
|
|
|
//senzor.rotateVector(quatD);
|
|
|
|
|
2024-09-06 18:19:53 +02:00
|
|
|
senzor.refreshGuiEuler;
|
2024-09-26 19:40:14 +02:00
|
|
|
//senzor.refreshGuiVector;
|
2024-09-06 18:19:53 +02:00
|
|
|
|
2024-09-26 19:40:14 +02:00
|
|
|
// Posljemo kot OSC, za lazje mapiranje v midi
|
|
|
|
~n.sendMsg("/euler/" ++ senzor.id, senzor.euler[0], senzor.euler[1], senzor.euler[2]);
|
|
|
|
//~n.sendMsg("/vector/" ++ senzor.id, senzor.vecF.x, senzor.vecF.y, senzor.vecF.z, senzor.vecT.x, senzor.vecT.y, senzor.vecT.z);
|
2024-09-06 18:19:53 +02:00
|
|
|
}.play(AppClock);
|
2024-09-18 13:56:49 +02:00
|
|
|
|
2024-09-06 18:19:53 +02:00
|
|
|
}, oscHeader ++ "/quat");
|
|
|
|
|
|
|
|
// Acceleration listener
|
2024-09-26 19:40:14 +02:00
|
|
|
q = OSCdef.new((\acc ++ s.id).asSymbol, { |msg, time, addr, recvPort|
|
|
|
|
|
2024-09-06 18:19:53 +02:00
|
|
|
var a;
|
|
|
|
a = msg.at((1..3));
|
|
|
|
Routine {
|
2024-09-18 13:56:49 +02:00
|
|
|
var vectorSum = 0, xy = 0, xyz = 0;
|
|
|
|
|
2024-09-06 18:19:53 +02:00
|
|
|
senzor.accel = a;
|
2024-09-18 13:56:49 +02:00
|
|
|
|
|
|
|
// Vsota vektorjev:
|
|
|
|
xy = sqrt((a[0] * a[0]) + (a[1] * a[1]));
|
|
|
|
xyz = sqrt((xy * xy) + (a[2] * a[2]));
|
|
|
|
senzor.accelSum = xyz;
|
|
|
|
|
2024-09-26 19:40:14 +02:00
|
|
|
// Poslji sumo
|
|
|
|
~n.sendMsg("/acc/" ++ senzor.id, senzor.accel[0], senzor.accel[1], senzor.accel[2], senzor.accelSum);
|
2024-09-18 13:56:49 +02:00
|
|
|
|
2024-09-06 18:19:53 +02:00
|
|
|
senzor.refreshGuiAccel;
|
|
|
|
}.play(AppClock);
|
|
|
|
}, oscHeader ++ "/acc");
|
|
|
|
|
|
|
|
// Battery listener
|
2024-09-26 19:40:14 +02:00
|
|
|
q = OSCdef.new((\bat ++ s.id).asSymbol, { |msg, time, addr, recvPort|
|
2024-09-06 18:19:53 +02:00
|
|
|
var b = msg[1];
|
|
|
|
// Since we get a battery reading every second, use this as the events/s refresh
|
|
|
|
Routine {
|
|
|
|
senzor.battery = b;
|
|
|
|
senzor.refreshGuiBat;
|
|
|
|
}.play(AppClock);
|
|
|
|
}, oscHeader ++ "/bat");
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
// Events per second counter
|
|
|
|
AppClock.clear;
|
|
|
|
AppClock.sched(0, {
|
|
|
|
~senzorji.collect({ |s|
|
|
|
|
s.refreshGuiEps;
|
|
|
|
s.eps = 0;
|
|
|
|
// Do this every second
|
|
|
|
});
|
|
|
|
1;
|
|
|
|
});
|
2024-09-03 15:04:59 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
~decoder.trace;
|
|
|
|
~decoder.stop;
|
|
|
|
~decoder.start;
|
|
|
|
|
2024-09-06 18:19:53 +02:00
|
|
|
/**********
|
|
|
|
* SOUND! *
|
|
|
|
*********/
|
2024-09-18 13:56:49 +02:00
|
|
|
|
2024-10-21 23:24:59 +02:00
|
|
|
~e = Env([1, 1, 0.2]);
|
2024-09-26 19:40:14 +02:00
|
|
|
s.boot;
|
2024-10-21 23:24:59 +02:00
|
|
|
{SinOsc.ar(290) * EnvGen.kr(~e, doneAction: Done.freeSelf)}.play;
|
2024-09-26 19:40:14 +02:00
|
|
|
|