trying different tabs 2 -> 4

This commit is contained in:
Dooho Yi 2024-04-24 04:08:36 +09:00
parent ca9f217eaf
commit dcf62905a2

View file

@ -119,28 +119,28 @@ extern Task rest_task;
int move_target = 0; int move_target = 0;
int move_duration = 10000; int move_duration = 10000;
void moving() { void moving() {
// //
float velocity = (float)move_target / move_duration * 1000; // unit conv.: (steps/msec) --> (steps/sec) float velocity = (float)move_target / move_duration * 1000; // unit conv.: (steps/msec) --> (steps/sec)
float speed = fabs(velocity); float speed = fabs(velocity);
// //
MONITORING_SERIAL.print("move_target --> "); MONITORING_SERIAL.println(move_target); MONITORING_SERIAL.print("move_target --> "); MONITORING_SERIAL.println(move_target);
MONITORING_SERIAL.print("move_duration --> "); MONITORING_SERIAL.println(move_duration); MONITORING_SERIAL.print("move_duration --> "); MONITORING_SERIAL.println(move_duration);
// //
if (speed > STEPS_PER_SEC_MAX) { if (speed > STEPS_PER_SEC_MAX) {
MONITORING_SERIAL.println("oh.. it might be TOO FAST for me.."); MONITORING_SERIAL.println("oh.. it might be TOO FAST for me..");
} else { } else {
MONITORING_SERIAL.println("okay. i m going."); MONITORING_SERIAL.println("okay. i m going.");
} }
// //
stepper.enableOutputs(); stepper.enableOutputs();
stepper.moveTo(move_target + stepper.currentPosition()); stepper.moveTo(move_target + stepper.currentPosition());
stepper.setSpeed(velocity); stepper.setSpeed(velocity);
//NOTE: 'setSpeed' should come LATER than 'moveTo'! //NOTE: 'setSpeed' should come LATER than 'moveTo'!
// --> 'moveTo' re-calculate the velocity. // --> 'moveTo' re-calculate the velocity.
// --> so we need to re-override it. // --> so we need to re-override it.
// //
// } // }
} }
Task moving_task(0, TASK_ONCE, &moving); Task moving_task(0, TASK_ONCE, &moving);
@ -148,45 +148,45 @@ Task moving_task(0, TASK_ONCE, &moving);
int step_target = 0; int step_target = 0;
int step_duration = 10000; int step_duration = 10000;
void stepping() { void stepping() {
// //
// if (stepper.distanceToGo() == 0) { // if (stepper.distanceToGo() == 0) {
// //
float cur_step = stepper.currentPosition(); float cur_step = stepper.currentPosition();
float target_step = step_target; float target_step = step_target;
float dur = step_duration; float dur = step_duration;
// float target_step = notes[score_now][note_idx][0]; // float target_step = notes[score_now][note_idx][0];
// float dur = notes[score_now][note_idx][1]; // float dur = notes[score_now][note_idx][1];
float steps = target_step - cur_step; float steps = target_step - cur_step;
float velocity = steps / dur * 1000; // unit conv.: (steps/msec) --> (steps/sec) float velocity = steps / dur * 1000; // unit conv.: (steps/msec) --> (steps/sec)
float speed = fabs(velocity); float speed = fabs(velocity);
// //
MONITORING_SERIAL.print("target_step --> "); MONITORING_SERIAL.println(target_step); MONITORING_SERIAL.print("target_step --> "); MONITORING_SERIAL.println(target_step);
MONITORING_SERIAL.print("dur --> "); MONITORING_SERIAL.println(dur); MONITORING_SERIAL.print("dur --> "); MONITORING_SERIAL.println(dur);
MONITORING_SERIAL.print("cur_step --> "); MONITORING_SERIAL.println(cur_step); MONITORING_SERIAL.print("cur_step --> "); MONITORING_SERIAL.println(cur_step);
// //
if (speed > STEPS_PER_SEC_MAX) { if (speed > STEPS_PER_SEC_MAX) {
MONITORING_SERIAL.println("oh.. it might be TOO FAST for me.."); MONITORING_SERIAL.println("oh.. it might be TOO FAST for me..");
} else { } else {
MONITORING_SERIAL.println("okay. i m going."); MONITORING_SERIAL.println("okay. i m going.");
} }
// //
stepper.enableOutputs(); stepper.enableOutputs();
stepper.moveTo(target_step); stepper.moveTo(target_step);
stepper.setSpeed(velocity); stepper.setSpeed(velocity);
//NOTE: 'setSpeed' should come LATER than 'moveTo'! //NOTE: 'setSpeed' should come LATER than 'moveTo'!
// --> 'moveTo' re-calculate the velocity. // --> 'moveTo' re-calculate the velocity.
// --> so we need to re-override it. // --> so we need to re-override it.
// //
// } // }
} }
Task stepping_task(0, TASK_ONCE, &stepping); Task stepping_task(0, TASK_ONCE, &stepping);
// //
void rest() { void rest() {
if (stepper.distanceToGo() == 0) { if (stepper.distanceToGo() == 0) {
stepper.disableOutputs(); stepper.disableOutputs();
} }
} }
Task rest_task(1000, TASK_FOREVER, &rest); Task rest_task(1000, TASK_FOREVER, &rest);
//*-*-*-*-*-*-*-*-*-*-*-*-* //*-*-*-*-*-*-*-*-*-*-*-*-*
@ -195,250 +195,250 @@ Task rest_task(1000, TASK_FOREVER, &rest);
extern Task hello_task; extern Task hello_task;
static int hello_delay = 0; static int hello_delay = 0;
void hello() { void hello() {
// //
byte mac[6]; byte mac[6];
WiFi.macAddress(mac); WiFi.macAddress(mac);
uint32_t mac32 = (((((mac[2] << 8) + mac[3]) << 8) + mac[4]) << 8) + mac[5]; 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' Hello hello(String(MY_SIGN), MY_ID, mac32); // the most basic 'hello'
// and you can append some floats // and you can append some floats
static int count = 0; static int count = 0;
count++; count++;
hello.h1 = (count % 1000); hello.h1 = (count % 1000);
hello.h2 = stepper.currentPosition(); hello.h2 = stepper.currentPosition();
hello.h3 = stepper.distanceToGo(); hello.h3 = stepper.distanceToGo();
// hello.h4 = 0; // hello.h4 = 0;
// //
uint8_t frm_size = sizeof(Hello) + 2; uint8_t frm_size = sizeof(Hello) + 2;
uint8_t frm[frm_size]; uint8_t frm[frm_size];
frm[0] = '{'; frm[0] = '{';
memcpy(frm + 1, (uint8_t *) &hello, sizeof(Hello)); memcpy(frm + 1, (uint8_t *) &hello, sizeof(Hello));
frm[frm_size - 1] = '}'; frm[frm_size - 1] = '}';
// //
esp_now_send(NULL, frm, frm_size); // to all peers in the list. esp_now_send(NULL, frm, frm_size); // to all peers in the list.
// //
MONITORING_SERIAL.write(frm, frm_size); MONITORING_SERIAL.write(frm, frm_size);
MONITORING_SERIAL.println(" ==(esp_now_send/0)==> "); MONITORING_SERIAL.println(" ==(esp_now_send/0)==> ");
// //
if (hello_delay > 0) { if (hello_delay > 0) {
if (hello_delay < 100) hello_delay = 100; if (hello_delay < 100) hello_delay = 100;
hello_task.restartDelayed(hello_delay); hello_task.restartDelayed(hello_delay);
} }
} }
Task hello_task(0, TASK_ONCE, &hello, &runner, false); Task hello_task(0, TASK_ONCE, &hello, &runner, false);
//task #0 : blink led //task #0 : blink led
extern Task blink_task; extern Task blink_task;
void blink() { void blink() {
// //
static int count = 0; static int count = 0;
count++; count++;
// //
switch (count % 4) { switch (count % 4) {
case 0: case 0:
digitalWrite(LED_PIN, LOW); // first ON digitalWrite(LED_PIN, LOW); // first ON
blink_task.delay(LED_ONTIME); blink_task.delay(LED_ONTIME);
break; break;
case 1: case 1:
digitalWrite(LED_PIN, HIGH); // first OFF digitalWrite(LED_PIN, HIGH); // first OFF
blink_task.delay(LED_GAPTIME); blink_task.delay(LED_GAPTIME);
break; break;
case 2: case 2:
digitalWrite(LED_PIN, LOW); // second ON digitalWrite(LED_PIN, LOW); // second ON
blink_task.delay(LED_ONTIME); blink_task.delay(LED_ONTIME);
break; break;
case 3: case 3:
digitalWrite(LED_PIN, HIGH); // second OFF digitalWrite(LED_PIN, HIGH); // second OFF
blink_task.delay(LED_PERIOD - 2* LED_ONTIME - LED_GAPTIME); blink_task.delay(LED_PERIOD - 2* LED_ONTIME - LED_GAPTIME);
break; break;
} }
} }
Task blink_task(0, TASK_FOREVER, &blink, &runner, true); // -> ENABLED, at start-up. Task blink_task(0, TASK_FOREVER, &blink, &runner, true); // -> ENABLED, at start-up.
// on 'Note' // on 'Note'
void onNoteHandler(Note & n) { void onNoteHandler(Note & n) {
//is it for me? //is it for me?
if (n.id == MY_GROUP_ID || n.id == MY_ID) { if (n.id == MY_GROUP_ID || n.id == MY_ID) {
//
if (n.pitch == 0) {
//
if (n.onoff == 1) {
// //
step_target = n.x1; if (n.pitch == 0) {
step_duration = n.x2; //
if (step_duration < 1000) step_duration = 1000; if (n.onoff == 1) {
stepping_task.restartDelayed(10); //
// step_target = n.x1;
} else if (n.onoff == 0) { step_duration = n.x2;
// if (step_duration < 1000) step_duration = 1000;
move_target = 0; stepping_task.restartDelayed(10);
move_duration = 1000; //
moving_task.restartDelayed(10); } else if (n.onoff == 0) {
} //
// move_target = 0;
} else if (n.pitch == 1) { move_duration = 1000;
// moving_task.restartDelayed(10);
if (n.onoff == 1) { }
// //
move_target = n.x1; } else if (n.pitch == 1) {
move_duration = n.x2; //
if (move_duration < 1000) move_duration = 1000; if (n.onoff == 1) {
moving_task.restartDelayed(10); //
// move_target = n.x1;
} else if (n.onoff == 0) { move_duration = n.x2;
move_target = 0; if (move_duration < 1000) move_duration = 1000;
move_duration = 1000; moving_task.restartDelayed(10);
moving_task.restartDelayed(10); //
} } else if (n.onoff == 0) {
// move_target = 0;
move_duration = 1000;
moving_task.restartDelayed(10);
}
//
}
} }
}
} }
// on 'receive' // on 'receive'
void onDataReceive(uint8_t * mac, uint8_t *incomingData, uint8_t len) { void onDataReceive(uint8_t * mac, uint8_t *incomingData, uint8_t len) {
// //
//MONITORING_SERIAL.write(incomingData, len); //MONITORING_SERIAL.write(incomingData, len);
// //
#if defined(HAVE_CLIENT) #if defined(HAVE_CLIENT)
Serial.write(incomingData, len); // we pass it over to the client. Serial.write(incomingData, len); // we pass it over to the client.
#endif #endif
// open => identify => use. // open => identify => use.
if (incomingData[0] == '{' && incomingData[len - 1] == '}' && len == (sizeof(Hello) + 2)) { if (incomingData[0] == '{' && incomingData[len - 1] == '}' && len == (sizeof(Hello) + 2)) {
Hello hello(""); Hello hello("");
memcpy((uint8_t *) &hello, incomingData + 1, sizeof(Hello)); memcpy((uint8_t *) &hello, incomingData + 1, sizeof(Hello));
// //
MONITORING_SERIAL.println(hello.to_string()); 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()); // 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());
}
} }
// on 'sent' // on 'sent'
void onDataSent(uint8_t *mac_addr, uint8_t sendStatus) { void onDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
if (sendStatus != 0) MONITORING_SERIAL.println("Delivery failed!"); if (sendStatus != 0) MONITORING_SERIAL.println("Delivery failed!");
} }
// //
void setup() { void setup() {
//led //led
pinMode(LED_PIN, OUTPUT); pinMode(LED_PIN, OUTPUT);
//serial //serial
Serial.begin(115200); Serial.begin(115200);
delay(100); delay(100);
//info //info
Serial.println(); Serial.println();
Serial.println(); Serial.println();
Serial.println("\"hi, i m your postman.\""); Serial.println("\"hi, i m your postman.\"");
Serial.println("-"); Serial.println("-");
Serial.println("- my id: " + String(MY_ID) + ", gid: " + String(MY_GROUP_ID) + ", call me ==> \"" + String(MY_SIGN) + "\""); 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)); Serial.println("- mac address: " + WiFi.macAddress() + ", channel: " + String(WIFI_CHANNEL));
#if defined(HAVE_CLIENT) #if defined(HAVE_CLIENT)
Serial.println("- ======== 'HAVE_CLIENT' ========"); Serial.println("- ======== 'HAVE_CLIENT' ========");
#endif #endif
#if defined(SERIAL_SWAP) #if defined(SERIAL_SWAP)
Serial.println("- ======== 'SERIAL_SWAP' ========"); Serial.println("- ======== 'SERIAL_SWAP' ========");
#endif #endif
#if defined(DISABLE_AP) #if defined(DISABLE_AP)
Serial.println("- ======== 'DISABLE_AP' ========"); Serial.println("- ======== 'DISABLE_AP' ========");
#endif #endif
#if defined(HAVE_CLIENT_I2C) #if defined(HAVE_CLIENT_I2C)
Serial.println("- ======== 'HAVE_CLIENT_I2C' ========"); Serial.println("- ======== 'HAVE_CLIENT_I2C' ========");
#endif #endif
Serial.println("-"); Serial.println("-");
//wifi //wifi
WiFiMode_t node_type = WIFI_AP_STA; WiFiMode_t node_type = WIFI_AP_STA;
#if defined(DISABLE_AP) #if defined(DISABLE_AP)
system_phy_set_max_tpw(0); system_phy_set_max_tpw(0);
node_type = WIFI_STA; node_type = WIFI_STA;
#endif #endif
WiFi.mode(node_type); WiFi.mode(node_type);
//esp-now //esp-now
if (esp_now_init() != 0) { if (esp_now_init() != 0) {
Serial.println("Error initializing ESP-NOW"); Serial.println("Error initializing ESP-NOW");
return; return;
} }
esp_now_set_self_role(ESP_NOW_ROLE_COMBO); esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
esp_now_register_send_cb(onDataSent); esp_now_register_send_cb(onDataSent);
esp_now_register_recv_cb(onDataReceive); esp_now_register_recv_cb(onDataReceive);
// //
Serial.println("- ! (esp_now_add_peer) ==> add a 'broadcast peer' (FF:FF:FF:FF:FF:FF)."); 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}; uint8_t broadcastmac[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
esp_now_add_peer(broadcastmac, ESP_NOW_ROLE_COMBO, 1, NULL, 0); esp_now_add_peer(broadcastmac, ESP_NOW_ROLE_COMBO, 1, NULL, 0);
// //
Serial.println("-"); Serial.println("-");
Serial.println("\".-.-.-. :)\""); Serial.println("\".-.-.-. :)\"");
Serial.println(); Serial.println();
#if defined(SERIAL_SWAP) #if defined(SERIAL_SWAP)
Serial.println("- ======== 'SERIAL_SWAP' ========"); Serial.println("- ======== 'SERIAL_SWAP' ========");
// a proper say goodbye. // a proper say goodbye.
Serial.println("\"bye, i will do 'swap' in 1 second. find me on alternative pins!\""); 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("\" hint: osc wiring ==> esp8266(serial.swap) <-> teensy(serial3)\"");
Serial.println("-"); Serial.println("-");
Serial.println("\".-.-.-. :)\""); Serial.println("\".-.-.-. :)\"");
delay(1000); // flush out unsent serial messages. delay(1000); // flush out unsent serial messages.
// moving... // moving...
Serial.swap(); // use RXD2/TXD2 pins, afterwards. Serial.swap(); // use RXD2/TXD2 pins, afterwards.
delay(100); // wait re-initialization of the 'Serial' delay(100); // wait re-initialization of the 'Serial'
#endif #endif
//random seed //random seed
randomSeed(analogRead(0)); randomSeed(analogRead(0));
//stepper //stepper
// "The fastest motor speed that can be reliably supported is about 4000 steps per // "The fastest motor speed that can be reliably supported is about 4000 steps per
// second at a clock frequency of 16 MHz on Arduino such as Uno etc." // second at a clock frequency of 16 MHz on Arduino such as Uno etc."
// @ AccelStepper.h // @ AccelStepper.h
stepper.setMaxSpeed(STEPS_PER_SEC_MAX); //steps per second (trade-off between speed vs. torque) stepper.setMaxSpeed(STEPS_PER_SEC_MAX); //steps per second (trade-off between speed vs. torque)
#if (STEP_MODE == STEP_MODE_ACCELERATING) #if (STEP_MODE == STEP_MODE_ACCELERATING)
stepper.setAcceleration(ACCELERATION_MAX); stepper.setAcceleration(ACCELERATION_MAX);
#endif #endif
//tasks //tasks
runner.addTask(stepping_task); runner.addTask(stepping_task);
runner.addTask(moving_task); runner.addTask(moving_task);
runner.addTask(rest_task); runner.addTask(rest_task);
rest_task.restartDelayed(500); rest_task.restartDelayed(500);
} }
void loop() { void loop() {
// //
runner.execute(); runner.execute();
// //
//stepper //stepper
if (stepper.distanceToGo() != 0) { if (stepper.distanceToGo() != 0) {
#if (STEP_MODE == STEP_MODE_CONSTANT_VEL) #if (STEP_MODE == STEP_MODE_CONSTANT_VEL)
stepper.runSpeed(); stepper.runSpeed();
#elif (STEP_MODE == STEP_MODE_ACCELERATING) #elif (STEP_MODE == STEP_MODE_ACCELERATING)
stepper.run(); stepper.run();
#endif #endif
} }
} }