From 448a99d74336d70fadc46e35224a4014f321b855 Mon Sep 17 00:00:00 2001 From: Riviera Taylor Date: Tue, 22 Oct 2024 20:45:25 +0200 Subject: [PATCH] edit Motor Shield One code --- arduino/motor_shield_one/motor_shield_one.ino | 118 ++++++++++ tidal/boot/MotorShieldOne.hs | 217 ++++++++++++++++++ tidal/compositions/motor-shield-one.tidal | 7 + 3 files changed, 342 insertions(+) create mode 100644 tidal/boot/MotorShieldOne.hs create mode 100644 tidal/compositions/motor-shield-one.tidal diff --git a/arduino/motor_shield_one/motor_shield_one.ino b/arduino/motor_shield_one/motor_shield_one.ino index e69de29..a2dd58e 100644 --- a/arduino/motor_shield_one/motor_shield_one.ino +++ b/arduino/motor_shield_one/motor_shield_one.ino @@ -0,0 +1,118 @@ +//libraries +#include +#include + +//motors +const int C_HEAD = 1; +const int C_FEED = 2; + +int head_speed = 150; +int feed_speed = 150; + +Adafruit_MotorShield AFMS = Adafruit_MotorShield(); + +Adafruit_DCMotor *headMotor = AFMS.getMotor(C_HEAD); +Adafruit_DCMotor *feedMotor = AFMS.getMotor(C_FEED); + +//OSCWiFi +const char* ssid = "XPUB"; +const char* pwd = "5fff132d"; +const IPAddress esp_ip(192, 168, 1, 100); +const IPAddress gateway(192, 168, 1, 1); +const IPAddress subnet(255, 255, 255, 0); +const String tidal = "192.168.1.202"; +const int recv_port = 7000; + +void setup(){ + Serial.begin(115200); + WiFi.mode(WIFI_STA); + WiFi.begin(ssid, pwd); + WiFi.config(esp_ip, gateway, subnet); + while (WiFi.status() != WL_CONNECTED) { + Serial.print('.'); + delay(500); + }; + + if (!AFMS.begin()) { // here for pwm + Serial.println("Could not find Motor Shield. Check wiring."); + while (1); + }; + + Serial.println(""); + Serial.println("WiFi connected."); + Serial.print("IP address: "); + Serial.println(WiFi.localIP()); + Serial.print("ESP CPU Frequency in MHz: "); + Serial.println(ESP.getCpuFreqMHz()); + + headMotor->setSpeed(head_speed); + headMotor->run(RELEASE); + feedMotor->setSpeed(feed_speed); + feedMotor->run(RELEASE); + + OscWiFi.subscribe(recv_port, "/stopall", + [](const OscMessage& m) { + feedMotor->run(RELEASE); + headMotor->run(RELEASE); + } + ); + + OscWiFi.subscribe(recv_port, "/feed/direction", + [&](const String& direction) { + if (direction == "f" || direction == "forward" || direction == "forwards" || direction == "forth"){ + Serial.println("Feed motor direction: forward"); + feedMotor->run(FORWARD); + } else if (direction == "b" || direction == "backward" || direction == "backwards" || direction == "back") { + Serial.println("Feed motor direction: backward"); + feedMotor->run(BACKWARD); + } else if (direction == "s" || direction == "stop" || direction == "r" || direction == "release" || direction == "rel") { + Serial.println("Feed motor direction: release"); + feedMotor->run(RELEASE); + } else { + Serial.println("Don't know what to do with this OSC message."); + } + } + ); + + OscWiFi.subscribe(recv_port, "/feed/speed", + [&](const int& speed) { + feed_speed = speed; + Serial.print("Feed motor speed: "); + Serial.print(feed_speed); + Serial.println(); + feedMotor->setSpeed(speed); + } + ); + + OscWiFi.subscribe(recv_port, "/head/direction", + [&](const String& direction) { + if (direction == "f" || direction == "forward" || direction == "forwards" || direction == "forth"){ + Serial.println("Head motor direction: forward"); + headMotor->run(FORWARD); + } else if (direction == "b" || direction == "backward" || direction == "backwards" || direction == "back") { + Serial.println("Head motor direction: backward"); + headMotor->run(BACKWARD); + } else if (direction == "s" || direction == "stop" || direction == "r" || direction == "release" || direction == "rel") { + Serial.println("Head motor direction: release"); + headMotor->run(RELEASE); + } else { + Serial.println("Don't know what to do with this OSC message."); + } + } + ); + + OscWiFi.subscribe(recv_port, "/head/speed", + [&](const int& speed) { + head_speed = speed; + Serial.print("Head motor speed: "); + Serial.print(head_speed); + Serial.println(); + headMotor->setSpeed(head_speed); + } + ); +} + +void loop(){ + OscWiFi.update(); +} + diff --git a/tidal/boot/MotorShieldOne.hs b/tidal/boot/MotorShieldOne.hs new file mode 100644 index 0000000..03ca8f6 --- /dev/null +++ b/tidal/boot/MotorShieldOne.hs @@ -0,0 +1,217 @@ +:set -XOverloadedStrings +:set prompt "" + +import Sound.Tidal.Context + +import System.IO (hSetEncoding, stdout, utf8) + +hSetEncoding stdout utf8 + +listenPortOne = 8001 +listenPortTwo = 8002 +listenPortThree = 8003 +listenPortFour = 8004 +listenPortFive = 8005 +listenPortSix = 8006 +listenPortSeven = 8007 +listenPortEight = 8008 +listenPortNine = 8009 +listenPortTen = 8010 +listenPortEleven = 8011 +listenPortTwelve = 8012 +listenPortThriteen = 8013 +listenPortFourteen = 8014 +listenPortFifteen = 8015 +listenPortSixteen = 8016 +listenPortSeventeen = 8017 +listenPortEighteen = 8018 +listenPortNineteen = 8019 +listenPortTwenty = 8020 +listenPortTwentyOne = 8021 +listenPortTwentyTwo = 8022 +listenPortTwentyThree = 8023 +listenPortTwentyFour = 8024 +listenPortTwentyFive = 8025 +listenPortTwentySix = 8026 +listenPortTwentySeven = 8027 +listenPortTwentyEight = 8028 +listenPortTwentyNine = 8029 +listenPortThirty = 8030 +listenPortThirtyOne = 8031 +listenPortThirtyTwo = 8032 + +:{ + let lolinTarget = + Target {oName = "lolin", + oAddress = "192.168.1.110", + oPort = 7000, + oLatency = 0.15, + oSchedule = Live, + oWindow = Nothing, + oHandshake = False, + oBusPort = Nothing + } + + lolinLedAnalog = [("intensity", Just $ VF 0)] + lolinLedDigital = [("blink", Just $ VS "off")] + + lolinFormats = [(OSC "/led/analog" $ ArgList lolinLedAnalog), + (OSC "/led/digital" $ ArgList lolinLedDigital) + ] + lolinMap = [(lolinTarget, lolinFormats)] +:} + +lolinStream <- startStream (defaultConfig {cCtrlAddr = "0.0.0.0", cCtrlPort = 6010}) lolinMap + +h1 = streamReplace lolinStream 1 + +:{ + let devkitTarget = + Target {oName = "devkit", + oAddress = "192.168.1.111", + oPort = 7000, + oLatency = 0.15, + oSchedule = Live, + oWindow = Nothing, + oHandshake = False, + oBusPort = Nothing + } + + devkitLedAnalog = [("intensity", Just $ VF 0)] + devkitLedDigital = [("blink", Just $ VS "off")] + + devkitFormats = [(OSC "/led/analog" $ ArgList devkitLedAnalog), + (OSC "/led/digital" $ ArgList devkitLedDigital) + ] + devkitMap = [(devkitTarget, devkitFormats)] +:} + +devkitStream <- startStream (defaultConfig {cCtrlAddr = "0.0.0.0", cCtrlPort = 6020}) devkitMap + +h2 = streamReplace devkitStream 1 + + +:{ + let intensity = pF "intensity" + blink = pS "blink" +:} + + +:{ + let motorShieldOneTarget = + Target {oName = "motorShieldOne", + oAddress = "192.168.1.100", + oPort = 7000, + oLatency = 0.15, + oSchedule = Live, + oWindow = Nothing, + oHandshake = False, + oBusPort = Nothing + } + + motorShieldOneSpeed = [("speed", Just $ VI 150)] + motorShieldOneDirection = [("direction", Just $ VS "r")] + + motorShieldOneFeedFormats = [(OSC "/feed/direction" $ ArgList motorShieldOneDirection), + (OSC "/feed/speed" $ ArgList motorShieldOneSpeed) + ] + + motorShieldOneHeadFormats = [(OSC "/head/speed" $ ArgList motorShieldOneSpeed), + (OSC "/head/direction" $ ArgList motorShieldOneDirection) + ] + + motorShieldOneFeedMap = [(motorShieldOneTarget, motorShieldOneFeedFormats)] + motorShieldOneHeadMap = [(motorShieldOneTarget, motorShieldOneHeadFormats)] +:} + +motorShieldOneFeedStream <- startStream (defaultConfig {cCtrlAddr = "0.0.0.0", cCtrlPort = 6040}) motorShieldOneFeedMap + +m1f = streamReplace motorShieldOneFeedStream + +motorShieldOneHeadStream <- startStream (defaultConfig {cCtrlAddr = "0.0.0.0", cCtrlPort = 6040}) motorShieldOneHeadMap + +m1h = streamReplace motorShieldOneHeadStream + +m1 = m1f 1 +m2 = m1h 1 + +:{ + let speed = pI "speed" + direction = pS "direction" +:} + + +tidal <- startTidal (superdirtTarget {oLatency = 0.05, oAddress = "127.0.0.1", oPort = 57120}) (defaultConfig {cVerbose = True, cFrameTimespan = 1/20, cCtrlAddr = "0.0.0.0", cCtrlPort = 6030}) + +:{ +let only = (hush >>) + p = streamReplace tidal + hush = streamHush tidal + panic = do hush + once $ sound "superpanic" + list = streamList tidal + mute = streamMute tidal + unmute = streamUnmute tidal + unmuteAll = streamUnmuteAll tidal + unsoloAll = streamUnsoloAll tidal + solo = streamSolo tidal + unsolo = streamUnsolo tidal + once = streamOnce tidal + first = streamFirst tidal + asap = once + nudgeAll = streamNudgeAll tidal + all = streamAll tidal + resetCycles = streamResetCycles tidal + setCycle = streamSetCycle tidal + setcps = asap . cps + getcps = streamGetcps tidal + getnow = streamGetnow tidal + xfade i = transition tidal True (Sound.Tidal.Transition.xfadeIn 4) i + xfadeIn i t = transition tidal True (Sound.Tidal.Transition.xfadeIn t) i + histpan i t = transition tidal True (Sound.Tidal.Transition.histpan t) i + wait i t = transition tidal True (Sound.Tidal.Transition.wait t) i + waitT i f t = transition tidal True (Sound.Tidal.Transition.waitT f t) i + jump i = transition tidal True (Sound.Tidal.Transition.jump) i + jumpIn i t = transition tidal True (Sound.Tidal.Transition.jumpIn t) i + jumpIn' i t = transition tidal True (Sound.Tidal.Transition.jumpIn' t) i + jumpMod i t = transition tidal True (Sound.Tidal.Transition.jumpMod t) i + jumpMod' i t p = transition tidal True (Sound.Tidal.Transition.jumpMod' t p) i + mortal i lifespan release = transition tidal True (Sound.Tidal.Transition.mortal lifespan release) i + interpolate i = transition tidal True (Sound.Tidal.Transition.interpolate) i + interpolateIn i t = transition tidal True (Sound.Tidal.Transition.interpolateIn t) i + clutch i = transition tidal True (Sound.Tidal.Transition.clutch) i + clutchIn i t = transition tidal True (Sound.Tidal.Transition.clutchIn t) i + anticipate i = transition tidal True (Sound.Tidal.Transition.anticipate) i + anticipateIn i t = transition tidal True (Sound.Tidal.Transition.anticipateIn t) i + forId i t = transition tidal False (Sound.Tidal.Transition.mortalOverlay t) i + d1 = p 1 . (|< orbit 0) + d2 = p 2 . (|< orbit 1) + d3 = p 3 . (|< orbit 2) + d4 = p 4 . (|< orbit 3) + d5 = p 5 . (|< orbit 4) + d6 = p 6 . (|< orbit 5) + d7 = p 7 . (|< orbit 6) + d8 = p 8 . (|< orbit 7) + d9 = p 9 . (|< orbit 8) + d10 = p 10 . (|< orbit 9) + d11 = p 11 . (|< orbit 10) + d12 = p 12 . (|< orbit 11) + d13 = p 13 + d14 = p 14 + d15 = p 15 + d16 = p 16 +:} + +:{ +let getState = streamGet tidal + setI = streamSetI tidal + setF = streamSetF tidal + setS = streamSetS tidal + setR = streamSetR tidal + setB = streamSetB tidal +:} + +:set prompt "> " +:set prompt-cont "" + +default (Pattern String, Integer, Double) diff --git a/tidal/compositions/motor-shield-one.tidal b/tidal/compositions/motor-shield-one.tidal new file mode 100644 index 0000000..6400267 --- /dev/null +++ b/tidal/compositions/motor-shield-one.tidal @@ -0,0 +1,7 @@ +(setq tidal-boot-script-path "~/.config/tidal/MotorShieldOne.hs") + +m1 $ slow 6 $ speed 125 # direction "f b f b" + +m2 $ slow 4 $ speed 150 # direction "f b r f b r" + + hush