Browse Source

Added working switch control code

Toby Chui 10 months ago
parent
commit
69dd37030f

+ 1 - 1
firmware/cute_useless_robot/animation.ino

@@ -45,7 +45,7 @@ void handleAnimationRendering(char anicode) {
 //Default 500 unless specially programmed
 int getFrameDuration(char anicode, int framecount){
   if ((anicode == 'a' || anicode == 'b') && framecount == 0){
-    return 4000;
+    return 2500;
   }
   return 500;
 }

+ 22 - 9
firmware/cute_useless_robot/cute_useless_robot.ino

@@ -5,27 +5,31 @@
     Developed by tobychui
     Idea come from Kairoshi
 
+    Board Settings:
+    ESP32 v2.014
+    -> ESP32 Dev Module
 */
 
 /* Libraries */
 #include <MD_MAX72xx.h>
 #include <SPI.h>
 #include <SD.h>
-#include <Servo.h> //Require ESP32Servo, not the Arduino build in Servo.h
+#include <ESP32Servo.h> //Require ESP32Servo
 
 /* Pins Definations */
 #define STP_DATA_PIN 4    // Stepper Shift Register DS
 #define STP_CLOCK_PIN 16  // Stepper Shift Register SH_CP
 #define STP_LATCH_PIN 17  // Stepper Shift Register ST_CP
 
-#define SERVO_SWITCH 27 //Servo to push the switch
-#define SERVO_COVER 14  //Servo to push the cover
+#define SERVO_SWITCH 27  //Servo to push the switch
+#define SERVO_COVER 14   //Servo to push the cover
 
-#define DP_CLK_PIN   32 //Display CLK
-#define DP_DATA_PIN  33 //Display DIN
-#define DP_CS_PIN    25 //Display CS
+#define DP_CLK_PIN 32   //Display CLK
+#define DP_DATA_PIN 33  //Display DIN
+#define DP_CS_PIN 25    //Display CS
 
-#define SD_CS_PIN 5 //SD Card CS pin
+#define SD_CS_PIN 5       //SD Card CS pin
+#define TOGGLE_SWITCH 13  //Switch on top of the matrix display
 
 /* Display settings generated by trial and error. Don't touch these */
 #define HARDWARE_TYPE MD_MAX72XX::DR1CR0RR0_HW
@@ -37,11 +41,18 @@ Servo servoCoverPusher;
 MD_MAX72XX mx = MD_MAX72XX(HARDWARE_TYPE, DP_DATA_PIN, DP_CLK_PIN, DP_CS_PIN, MAX_DEVICES);
 
 /* Global Variables */
-char animation = 'a'; //Animation ID to render
+char animation = 'a';  //Animation ID to render
 
 void setup() {
   Serial.begin(115200);
 
+  // Allow allocation of all timers
+
+  ESP32PWM::allocateTimer(0);
+  ESP32PWM::allocateTimer(1);
+  ESP32PWM::allocateTimer(2);
+  ESP32PWM::allocateTimer(3);
+
   /* Stepper IO */
   pinMode(STP_DATA_PIN, OUTPUT);
   pinMode(STP_CLOCK_PIN, OUTPUT);
@@ -49,6 +60,8 @@ void setup() {
   standbySteppers();
 
   /* Servo IO */
+  servoSwitchPusher.setPeriodHertz(50); 
+  servoCoverPusher.setPeriodHertz(50); 
   servoSwitchPusher.attach(SERVO_SWITCH);
   servoCoverPusher.attach(SERVO_COVER);
   servoCoverPusher.write(0);
@@ -63,7 +76,7 @@ void setup() {
   /* Display Module */
   mx.begin();
   setDisplayBrightness(0x4);
-  renderFrame(); //Render the default frame to matrix
+  renderFrame();  //Render the default frame to matrix
   delay(5000);
 
   /* Start Dual-core processes */

+ 15 - 8
firmware/cute_useless_robot/display.ino

@@ -5,7 +5,7 @@
   https://majicdesigns.github.io/MD_MAX72XX/class_m_d___m_a_x72_x_x.html
 */
 
-#define FRAME_BUFFER_SIZE 64 //4(32 bits) x 16 bytes
+#define FRAME_BUFFER_SIZE 64  //4(32 bits) x 16 bytes
 
 //frame buffer, 32x16px
 //each LED is 1 bit
@@ -30,19 +30,19 @@ unsigned char frame_buffer[] = {
 
 //A combined function for read frame to framebuffer and render to display
 //Example usage: loadFrameAndRender("/a.bin");
-void loadFrameAndRender(String filepath){
+void loadFrameAndRender(String filepath) {
   readFrameToFrameBuffer(filepath);
   renderFrame();
 }
 
 //Read a binary file from SD card to current framebuffer
-int readFrameToFrameBuffer(String filepath){
+int readFrameToFrameBuffer(String filepath) {
   File file = SD.open(filepath, FILE_READ);
   if (!file) {
     Serial.println("Failed to open file for reading");
     return 1;
   }
-  
+
   // Read file byte by byte
   size_t bytesRead = 0;
   while (file.available() && bytesRead < FRAME_BUFFER_SIZE) {
@@ -87,11 +87,17 @@ void renderFrame() {
   }
 }
 
+//Clear the display to off state
+void clearFrame() {
+  //Z is reserved for empty screen
+  setAnimationCode('z');
+}
+
 /* Utilities Functions */
 //Set display brightness, from 0x0(min) to 0xF (max)
-void setDisplayBrightness(byte brightness){
-  for(int i =0; i<MAX_DEVICES; i++){
-    mx.control(i,MD_MAX72XX::INTENSITY, brightness);
+void setDisplayBrightness(byte brightness) {
+  for (int i = 0; i < MAX_DEVICES; i++) {
+    mx.control(i, MD_MAX72XX::INTENSITY, brightness);
   }
 }
 
@@ -103,5 +109,6 @@ byte fByte(byte c) {
     r <<= 1;
     r |= c & 1;
     c >>= 1;
-  } return r;
+  }
+  return r;
 }

+ 30 - 0
firmware/cute_useless_robot/servo.ino

@@ -0,0 +1,30 @@
+/*
+
+  Servo Logic Code
+
+  This script handles logic related to servo operations
+
+*/
+
+//Calibrated offset to add or reduce for this particilar machine
+const int servoAlignmentOffset = 0;
+
+//Quickily push the switch back
+void pushSwitchNow() {
+  pushSwitchDelayed(1000, 1000);
+}
+
+//Push switch back with delay
+void pushSwitchDelayed(int coverDelay, int pusherDelay) {
+  servoCoverPusher.write(90);
+  delay(coverDelay);
+  servoSwitchPusher.write(130 + servoAlignmentOffset);
+  delay(pusherDelay);
+  servoCoverPusher.write(0);
+  servoSwitchPusher.write(0);
+}
+
+//Set the pusher servo to arm installation position
+void setSwitchToInstallPosition(){
+  servoSwitchPusher.write(125 + servoAlignmentOffset);
+}

+ 25 - 3
firmware/cute_useless_robot/tasks.ino

@@ -34,6 +34,14 @@ char getAnimationCode() {
   return anicode;
 }
 
+//Get the current state of the switch
+// true = After human pushed
+// false = After robot pushed
+bool getSwitchState(){
+    int switchState = digitalRead(TOGGLE_SWITCH);
+    return (switchState == 1);
+}
+
 
 /* Multi-core process definations */
 void startCoreTasks() {
@@ -64,12 +72,14 @@ void startCoreTasks() {
 //Core 0 code, for movement and primary logics
 void PrimaryController( void * pvParameters ) {
   Serial.println("Primary logic process started on core " + String(xPortGetCoreID()));
+  clearFrame();
   for (;;) {
     //Loop of primary logics
     //setAnimationCode('a');
     //delay(10000);
     
     //Stepper test
+    /*
     forward(50);
     delay(3000);
     backward(50);
@@ -91,13 +101,25 @@ void PrimaryController( void * pvParameters ) {
     servoCoverPusher.write(0);
     servoSwitchPusher.write(0);
     delay(3000);
-    
+    */
+    bool switchPushed = getSwitchState();
+    if (switchPushed){
+      //Human pushed
+      setAnimationCode('a');
+      delay(4000);
+      Serial.println("ON");
+      pushSwitchNow();
+      clearFrame();
+      delay(3000);
+    }else{
+      delay(100);
+    }
 
     //Servo install position = 125 (vertical)
     //servoSwitchPusher.write(125);
     
-    setAnimationCode('j');
-    delay(10000);
+    //setAnimationCode('j');
+    //delay(10000);
   }
 }
 

BIN
sd_card/anime/z.bin