revised '@bell'

- now support 'live' angles w/ x1, x2
This commit is contained in:
Dooho Yi 2022-05-04 00:43:00 +09:00
parent 4d5bc1a114
commit fa4cb573d1
2 changed files with 371 additions and 173 deletions

View file

@ -1,49 +1,66 @@
//
// wirelessly connected cloud (Wireless Mesh Networking)
// MIDI-like
// spacial
// sampler keyboard
// wirelessly connected cloud (based on ESP-NOW, a kind of LPWAN?)
//
//
// Forest all/around @ MMCA, Seoul
// `hing @ dianaband studio, Seoul
//
//
// 2020 10 14
// 2021 02 15
//
// this module will be an esp-now node in a group.
// like, a bird in a group of birds.
//
// esp-now @ esp8266 w/ broadcast address (FF:FF:FF:FF:FF:FF)
// always broadcasting. everyone is 'talkative'.
//
//==========<configurations>===========
// then, let it save a value in EEPROM (object with memory=mind?)
//============<identities>============
//
#define MY_GROUP_ID (11000)
#define MY_ID (MY_GROUP_ID + 1)
#define MY_SIGN ("BELL")
#define ADDRESSBOOK_TITLE ("broadcast only")
//
//============</identities>============
//==========<list-of-configurations>===========
//
// 'HAVE_CLIENT'
// --> i have a client. enable the client task.
//
// 'SERIAL_SWAP'
// --> UART pin swapped.
// you want this, when you want a bi-directional comm. to external client boards (e.g. teensy).
//
// 'DISABLE_AP'
// --> disabling AP is for teensy audio samplers.
// they need this to reduce noise from AP beacon signals.
// but, then they cannot build-up net. by themselves.
// we need who can do AP..
// ==> TODO! just prepare some 'dummy' postmans around. w/ AP activated.
// --> (questioning)...
//
// 'DISABLE_I2C_REQ'
// --> a quirk.. due to bi-directional I2C hardship.
// ideally, we want to make this sampler node also speak.
// but, I2C doesn't work. maybe middleware bug.. we later want to change to diff. proto.
// for example, UART or so.
// ==> BEWARE! yet, still we need to take off this.. for 'osc' node.
//
// 'SET_ROOT'
// 'SET_CONTAINSROOT'
// --> for the network stability
// declare 1 root node and branches(constricted to 'contains the root')
// to improve the stability of the net
//
//==========</configurations>==========
// 'REPLICATE_NOTE_REQ' (+ N_SEC_BLOCKING_NOTE_REQ)
// --> for supporting wider area with simple esp_now protocol,
// all receipents will replicate NOTE msg. when they are newly appeared.
// + then, network would be flooded by infinite duplicating msg.,
// unless they stop reacting to 'known' req. for some seconds. (e.g. 3 seconds)
//==========<preset>===========
#define SET_CONTAINSROOT
//==========</preset>==========
// 'HAVE_CLIENT_I2C'
// --> i have a client w/ I2C i/f. enable the I2C client task.
//
// 'ADDRESSBOOK_TITLE'
// --> peer list limited max. 20.
// so, we might use different address books for each node to cover a network of more than 20 nodes.
//
//==========</list-of-configurations>==========
//
// (EMPTY)
#define DISABLE_AP
// #define REPLICATE_NOTE_REQ
//============<gastank>============
//============<bell>============
#define BELL_HIT_KEY 105
//============</gastank>===========
//============</bell>===========
//============<parameters>============
//
@ -68,30 +85,68 @@
//
//============</parameters>===========
//============<board-specifics>============
#if defined(ARDUINO_FEATHER_ESP32) // featheresp32
#define LED_PIN 13
#else
#define LED_PIN 2
#endif
//============</board-specifics>===========
//arduino
#include <Arduino.h>
//post & addresses
#include "../../post.h"
//vector
#include <vector>
std::vector<Note> recentNotes;
//espnow
#include <ESP8266WiFi.h>
#include <espnow.h>
AddressLibrary lib;
//task
#include <TaskScheduler.h>
Scheduler runner;
//
#if defined(REPLICATE_NOTE_REQ)
Note note_now = {
-1, // int32_t id;
-1, // float pitch;
-1, // float velocity;
-1, // float onoff;
-1, // float x1;
-1, // float x2;
-1, // float x3;
-1, // float x4;
-1 // float ps;
};
#define RECENT_NOTES_TIMEOUT (3000)
static unsigned long last_note_time = 0;
void recent_clear() {
//
if (millis() - last_note_time > RECENT_NOTES_TIMEOUT) {
recentNotes.clear();
Serial.println("recent list cleared");
last_note_time = millis();
}
//
}
Task recent_clear_task(100, TASK_FOREVER, &recent_clear, &runner, true);
#endif
//-*-*-*-*-*-*-*-*-*-*-*-*-
// servo
#include <Servo.h>
#define SERVO_PIN D6
Servo myservo;
// #define HITTING_ANGLE 87
#define HITTING_ANGLE 90
#define RELEASE_ANGLE 60
#define STABILIZE_ANGLE 53
int hitting_angle = 90;
int release_angle = 60;
int stabilize_angle = 53;
//
extern Task hit_task;
@ -117,32 +172,32 @@ void hit() {
if (count % 3 == 0) {
//
myservo.attach(SERVO_PIN);
myservo.write(RELEASE_ANGLE);
myservo.write(release_angle);
// servo_release_task.restartDelayed(200);
//
} else if (count % 3 == 1) {
//
myservo.attach(SERVO_PIN);
myservo.write(HITTING_ANGLE);
myservo.write(hitting_angle);
// servo_release_task.restartDelayed(200);
//
Serial.print("bell, bell, bell! : ");
Serial.print(HITTING_ANGLE);
Serial.print(hitting_angle);
Serial.println(" deg.");
//
} else {
//
myservo.attach(SERVO_PIN);
myservo.write(RELEASE_ANGLE);
myservo.write(release_angle);
servo_release_task.restartDelayed(200);
//
Serial.print("release to .. : ");
Serial.print(RELEASE_ANGLE);
Serial.print(release_angle);
Serial.println(" deg.");
// start stablizing..
pcontrol_new = true;
pcontrol_start = RELEASE_ANGLE;
pcontrol_target = STABILIZE_ANGLE;
pcontrol_start = release_angle;
pcontrol_target = stabilize_angle;
pcontrol_task.restartDelayed(80);
//
control_count = 0;
@ -181,13 +236,13 @@ void pcontrol() {
// stand-by processes
if (control_count % 2 == 0) {
pcontrol_new = true;
pcontrol_start = STABILIZE_ANGLE;
pcontrol_target = RELEASE_ANGLE;
pcontrol_start = stabilize_angle;
pcontrol_target = release_angle;
pcontrol_task.restartDelayed(300);
} else if (control_count % 2 == 1) {
pcontrol_new = true;
pcontrol_start = RELEASE_ANGLE;
pcontrol_target = STABILIZE_ANGLE;
pcontrol_start = release_angle;
pcontrol_target = stabilize_angle;
pcontrol_task.restartDelayed(300);
}
//
@ -201,148 +256,260 @@ void servo_release() {
myservo.detach();
}
Task servo_release_task(0, TASK_ONCE, &servo_release);
//*-*-*-*-*-*-*-*-*-*-*-*-*
// mesh callbacks
void receivedCallback(uint32_t from, String & msg) { // REQUIRED
Serial.print("got msg.: ");
Serial.println(msg);
//parse now.
//parse letter string.
// letter frame ( '[' + 30 bytes + ']' )
// : [123456789012345678901234567890]
// 'MIDI' letter frame
// : [123456789012345678901234567890]
// : [KKKVVVG.......................]
// : KKK - Key
// .substring(1, 4);
// : VVV - Velocity (volume/amp.)
// .substring(4, 7);
// : G - Gate (note on/off)
// .substring(7, 8);
String str_key = msg.substring(1, 4);
String str_velocity = msg.substring(4, 7);
String str_gate = msg.substring(7, 8);
int key = str_key.toInt();
int velocity = str_velocity.toInt(); // 0 ~ 127
int gate = str_gate.toInt();
//is it for me, the bell?
if (key == BELL_HIT_KEY && gate == 1) {
hit_task.restartDelayed(10);
//
extern Task hello_task;
static int hello_delay = 0;
void hello() {
//
byte mac[6];
WiFi.macAddress(mac);
uint32_t mac32 = (((((mac[2] << 8) + mac[3]) << 8) + mac[4]) << 8) + mac[5];
//
Hello hello(String(MY_SIGN), MY_ID, mac32); // the most basic 'hello'
// and you can append some floats
static int count = 0;
count++;
hello.h1 = (count % 1000);
// hello.h2 = 0;
// hello.h3 = 0;
// hello.h4 = 0;
//
uint8_t frm_size = sizeof(Hello) + 2;
uint8_t frm[frm_size];
frm[0] = '{';
memcpy(frm + 1, (uint8_t *) &hello, sizeof(Hello));
frm[frm_size - 1] = '}';
//
esp_now_send(NULL, frm, frm_size); // to all peers in the list.
//
// MONITORING_SERIAL.write(frm, frm_size);
// MONITORING_SERIAL.println(" ==(esp_now_send/0)==> ");
//
if (hello_delay > 0) {
if (hello_delay < 100) hello_delay = 100;
hello_task.restartDelayed(hello_delay);
}
}
void changedConnectionCallback() {
Serial.println(mesh.getNodeList().size());
// check status -> modify status LED
if (mesh.getNodeList().size() > 0) {
// (still) connected.
onFlag = false; //reset flag stat.
statusblinks.set(LED_PERIOD, 2, &taskStatusBlink_slowblink_insync);
// statusblinks.set(0, 1, &taskStatusBlink_steadyOff);
statusblinks.enable();
Serial.println("connected!");
Task hello_task(0, TASK_ONCE, &hello, &runner, false);
//task #0 : blink led
extern Task blink_task;
void blink() {
//
static int count = 0;
count++;
//
switch (count % 4) {
case 0:
digitalWrite(LED_PIN, LOW); // first ON
blink_task.delay(LED_ONTIME);
break;
case 1:
digitalWrite(LED_PIN, HIGH); // first OFF
blink_task.delay(LED_GAPTIME);
break;
case 2:
digitalWrite(LED_PIN, LOW); // second ON
blink_task.delay(LED_ONTIME);
break;
case 3:
digitalWrite(LED_PIN, HIGH); // second OFF
blink_task.delay(LED_PERIOD - 2* LED_ONTIME - LED_GAPTIME);
break;
}
}
Task blink_task(0, TASK_FOREVER, &blink, &runner, true); // -> ENABLED, at start-up.
// on 'Note'
void onNoteHandler(Note & n) {
//is it for me?
if (n.id == MY_GROUP_ID || n.id == MY_ID) {
//
isConnected = true;
runner.addTask(nothappyalone_task);
nothappyalone_task.enable();
if (n.pitch == BELL_HIT_KEY) {
if (n.onoff == 1) {
hitting_angle = n.velocity;
release_angle = n.x1;
stabilize_angle = n.x2;
hit_task.restartDelayed(10);
}
}
}
else {
// disconnected!!
statusblinks.set(0, 1, &taskStatusBlink_steadyOn);
statusblinks.enable();
//
isConnected = false;
}
// let I2C device know
/////
Serial.println("hi. client, we ve got a change in the net.");
}
void newConnectionCallback(uint32_t nodeId) {
Serial.println(mesh.getNodeList().size());
Serial.println("newConnectionCallback.");
changedConnectionCallback();
}
// on 'receive'
void onDataReceive(uint8_t * mac, uint8_t *incomingData, uint8_t len) {
//
//MONITORING_SERIAL.write(incomingData, len);
//
#if defined(HAVE_CLIENT)
Serial.write(incomingData, len); // we pass it over to the client.
#endif
// open => identify => use.
if (incomingData[0] == '{' && incomingData[len - 1] == '}' && len == (sizeof(Hello) + 2)) {
Hello hello("");
memcpy((uint8_t *) &hello, incomingData + 1, sizeof(Hello));
//
MONITORING_SERIAL.println(hello.to_string());
//
}
// open => identify => use.
if (incomingData[0] == '[' && incomingData[len - 1] == ']' && len == (sizeof(Note) + 2)) {
Note note;
memcpy((uint8_t *) &note, incomingData + 1, sizeof(Note));
onNoteHandler(note);
//is it for me?
if (note.id == MY_GROUP_ID || note.id == MY_ID) {
hello_delay = note.ps;
if (hello_delay > 0 && hello_task.isEnabled() == false) {
hello_task.restart();
}
}
MONITORING_SERIAL.println(note.to_string());
#if defined(REPLICATE_NOTE_REQ)
last_note_time = millis(); //clear timer reset : the recent list holding (re)started
// check if this note is in the list?
bool check = false;
for (uint32_t idx = 0; idx < recentNotes.size(); idx++) {
if (recentNotes[idx].pitch == note.pitch && recentNotes[idx].id == note.id) {
check = true;
}
}
// if not, add this into the list and repeat!
if (check == false) {
//
recentNotes.push_back(note);
//
uint8_t frm_size = sizeof(Note) + 2;
uint8_t frm[frm_size];
frm[0] = '[';
memcpy(frm + 1, (uint8_t *) &note, sizeof(Note));
frm[frm_size - 1] = ']';
//
esp_now_send(NULL, frm, frm_size); // to all peers in the list.
//
MONITORING_SERIAL.print("repeat! ==> ");
MONITORING_SERIAL.println(note.to_string());
//
}
//EMERGENCY PATCH.HACK:
// original code is not intended for a BURST of notes.
// so, only 1 msg. will be repeated in 3 sec. all others will be simply ignored.
// to make a burst of msgs repeatible:
// --> make a list of recent 'pitches'
// if there is any new msg. check if this is in the list, if not, add it & repeat, if yes, skip it.
// after 3sec no new msg., the list will be flushed. every new msg. will reset timeout + save the list for extra. 3 sec.
#endif
}
}
// on 'sent'
void onDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
char buff[256] = "";
sprintf(buff, "Delivery failed! -> %02X:%02X:%02X:%02X:%02X:%02X", mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
if (sendStatus != 0) MONITORING_SERIAL.println(buff);
}
//
void setup() {
//led
pinMode(LED_PIN, OUTPUT);
//mesh
//serial
Serial.begin(115200);
delay(100);
//info
Serial.println();
Serial.println();
Serial.println("\"hi, i m your postman.\"");
Serial.println("-");
Serial.println("- my id: " + String(MY_ID) + ", gid: " + String(MY_GROUP_ID) + ", call me ==> \"" + String(MY_SIGN) + "\"");
Serial.println("- mac address: " + WiFi.macAddress() + ", channel: " + String(WIFI_CHANNEL));
#if defined(HAVE_CLIENT)
Serial.println("- ======== 'HAVE_CLIENT' ========");
#endif
#if defined(SERIAL_SWAP)
Serial.println("- ======== 'SERIAL_SWAP' ========");
#endif
#if defined(DISABLE_AP)
Serial.println("- ======== 'DISABLE_AP' ========");
#endif
#if defined(HAVE_CLIENT_I2C)
Serial.println("- ======== 'HAVE_CLIENT_I2C' ========");
#endif
#if defined(REPLICATE_NOTE_REQ)
Serial.println("- ======== 'REPLICATE_NOTE_REQ' ========");
#endif
Serial.println("-");
//wifi
WiFiMode_t node_type = WIFI_AP_STA;
#if defined(DISABLE_AP)
system_phy_set_max_tpw(0);
node_type = WIFI_STA;
#endif
// mesh.setDebugMsgTypes(ERROR | DEBUG | CONNECTION);
mesh.setDebugMsgTypes( ERROR | STARTUP );
mesh.init(MESH_SSID, MESH_PASSWORD, &runner, MESH_PORT, node_type, MESH_CHANNEL);
WiFi.mode(node_type);
//
// void init(String ssid, String password, Scheduler *baseScheduler, uint16_t port = 5555, WiFiMode_t connectMode = WIFI_AP_STA, uint8_t channel = 1, uint8_t hidden = 0, uint8_t maxconn = MAX_CONN);
// void init(String ssid, String password, uint16_t port = 5555, WiFiMode_t connectMode = WIFI_AP_STA, uint8_t channel = 1, uint8_t hidden = 0, uint8_t maxconn = MAX_CONN);
//
#if defined(SET_ROOT)
mesh.setRoot(true);
#endif
#if defined(SET_CONTAINSROOT)
mesh.setContainsRoot(true);
#endif
//callbacks
mesh.onReceive(&receivedCallback);
mesh.onNewConnection(&newConnectionCallback);
mesh.onChangedConnections(&changedConnectionCallback);
Serial.println(mesh.getNodeList().size());
//tasks
runner.addTask(statusblinks);
statusblinks.enable();
//serial
Serial.begin(115200);
delay(100);
Serial.println("hi, postman ready.");
#if defined(DISABLE_AP)
Serial.println("!NOTE!: we are in the WIFI_STA mode!");
#endif
//understanding what is 'the nodeId' ==> last 4 bytes of 'softAPmacAddress'
// uint32_t nodeId = tcp::encodeNodeId(MAC);
Serial.print("nodeId (dec) : ");
Serial.println(mesh.getNodeId(), DEC);
Serial.print("nodeId (hex) : ");
Serial.println(mesh.getNodeId(), HEX);
uint8_t MAC[] = {0, 0, 0, 0, 0, 0};
if (WiFi.softAPmacAddress(MAC) == 0) {
Serial.println("init(): WiFi.softAPmacAddress(MAC) failed.");
//esp-now
if (esp_now_init() != 0) {
Serial.println("Error initializing ESP-NOW");
return;
}
Serial.print("MAC : ");
Serial.print(MAC[0], HEX); Serial.print(", ");
Serial.print(MAC[1], HEX); Serial.print(", ");
Serial.print(MAC[2], HEX); Serial.print(", ");
Serial.print(MAC[3], HEX); Serial.print(", ");
Serial.print(MAC[4], HEX); Serial.print(", ");
Serial.println(MAC[5], HEX);
esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
esp_now_register_send_cb(onDataSent);
esp_now_register_recv_cb(onDataReceive);
//
// Serial.println("- ! (esp_now_add_peer) ==> add a 'broadcast peer' (FF:FF:FF:FF:FF:FF).");
// uint8_t broadcastmac[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
// esp_now_add_peer(broadcastmac, ESP_NOW_ROLE_COMBO, 1, NULL, 0);
// for instance,
AddressBook * book = lib.getBookByTitle(ADDRESSBOOK_TITLE);
if (book == NULL) {
Serial.println("- ! wrong book !! : \"" + String(ADDRESSBOOK_TITLE) + "\""); while(1);
}
for (int idx = 0; idx < book->list.size(); idx++) {
Serial.println("- ! (esp_now_add_peer) ==> add a '" + book->list[idx].name + "'.");
#if defined(ESP32)
esp_now_peer_info_t peerInfo = {};
memcpy(peerInfo.peer_addr, book->list[idx].mac, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
esp_now_add_peer(&peerInfo);
#else
esp_now_add_peer(book->list[idx].mac, ESP_NOW_ROLE_COMBO, 1, NULL, 0);
#endif
}
// (DEBUG) fetch full peer list
{ PeerLister a; a.print(); }
//
Serial.println("-");
Serial.println("\".-.-.-. :)\"");
Serial.println();
// a huzzah board
// nodeId (dec) : 3256120530
// nodeId (hex) : C21474D2
// MAC : BE, DD, C2, 14, 74, D2
#if defined(SERIAL_SWAP)
Serial.println("- ======== 'SERIAL_SWAP' ========");
// a proper say goodbye.
Serial.println("\"bye, i will do 'swap' in 1 second. find me on alternative pins!\"");
Serial.println("\" hint: osc wiring ==> esp8266(serial.swap) <-> teensy(serial3)\"");
Serial.println("-");
Serial.println("\".-.-.-. :)\"");
delay(1000); // flush out unsent serial messages.
// a esp8266 board (node mcu)
// nodeId (dec) : 758581767
// nodeId (hex) : 2D370A07
// MAC : B6, E6, 2D, 37, A, 7
//i2c master
Wire.begin();
// moving...
Serial.swap(); // use RXD2/TXD2 pins, afterwards.
delay(100); // wait re-initialization of the 'Serial'
#endif
//tasks
runner.addTask(hit_task);
@ -351,11 +518,7 @@ void setup() {
}
void loop() {
//
runner.execute();
mesh.update();
#if defined(ESP32)
digitalWrite(LED_PIN, onFlag); // value == true is ON.
#else
digitalWrite(LED_PIN, !onFlag); // value == false is ON. so onFlag == true is ON. (pull-up)
#endif
//
}

35
puredata/radio_bell.pd Normal file
View file

@ -0,0 +1,35 @@
#N canvas 625 33 450 324 12;
#X obj 74 276 s NOTE;
#X floatatom 291 201 5 0 0 0 release\ angle - - 0;
#X floatatom 279 97 5 0 0 2 stabilizing\ angle - - 0;
#X obj 291 225 s X1;
#X obj 279 121 s X2;
#X floatatom 137 70 5 0 0 2 hitting\ angle - - 0;
#X obj 137 131 t b a b b;
#X obj 74 235 pack 11001 105 f 1;
#X obj 76 197 bng 15 250 50 0 empty empty empty 17 7 0 10 #fcfcfc #000000
#000000;
#X obj 163 281 print;
#X obj 137 100 f;
#X obj 16 63 bng 15 250 50 0 empty empty empty 17 7 0 10 #fcfcfc #000000
#000000;
#X obj 138 7 inlet;
#X obj 254 52 init 55;
#X obj 303 161 init 55;
#X obj 138 32 f 92;
#X connect 1 0 3 0;
#X connect 2 0 4 0;
#X connect 5 0 10 0;
#X connect 6 0 8 0;
#X connect 6 1 7 2;
#X connect 6 2 1 0;
#X connect 6 3 2 0;
#X connect 7 0 0 0;
#X connect 7 0 9 0;
#X connect 8 0 7 0;
#X connect 10 0 6 0;
#X connect 11 0 5 0;
#X connect 12 0 15 0;
#X connect 13 0 2 0;
#X connect 14 0 1 0;
#X connect 15 0 5 0;