互动机器人项目 — ESP32 simulation

一个基于ESP32的互动机器人,具有舵机耳朵、传感器和OLED显示屏。

An interactive ESP32 circuit simulation you can run free in your browser on Velxio, by zhouzhi0713.

Components

Sketch code

// ---- In Velxio -------------------------------------------------------------------
// This is the firmware of a real desk robot, as its author wrote it:
//   https://github.com/davidmonterocrespo24/robot_desktop  (GPL-3.0)
// Five things differ from the repository:
//   - the files sit side by side (a Velxio project has no folders);
//   - Config.h says DHT22 and a light sensor whose output rises with light,
//     which is what the parts on this canvas are;
//   - loop() rests one millisecond between frames, so the emulated CPU idles;
//   - the GIF clips are left out (ClipData.cpp), 776 KB of frames;
//   - Buzzer.cpp asks for LEDC channel 6 on Arduino-ESP32 3.x as it does on
//     2.x: left to the core, the buzzer got channel 0, the left ear's.
// Open the 3D button: the model is the robot's own CAD, its screen is the
// OLED of this circuit, its ears are the two servos.
// Desktop robot: OLED face, two servo ears and a few senses.
//
//   sensors -> Brain (mood, reactions) -> FaceEngine (emotion graph, animation)
//                                      -> Ears / Buzzer
//
// The same sketch runs on Windows through emulator/ (see README.md).
#include <Wire.h>
#include <U8g2lib.h>
#include <ESP32Servo.h>
#include <DHT.h>

#include "Config.h"
#include "Canvas.h"
#include "FaceEngine.h"
#include "Ears.h"
#include "Buzzer.h"
#include "Sensors.h"
#include "Brain.h"
#include "InfoScreen.h"

U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);

Canvas canvas;
FaceEngine face;
Ears ears;
Buzzer buzzer;
Sensors sensors;
Brain brain;

static uint32_t nextFrame = 0;
static uint8_t shown[CANVAS_BYTES];  // what the panel is showing (page layout)
static uint8_t pages[CANVAS_BYTES];
static bool firstFrame = true;

// Sends to the panel only the 8x8 tiles that changed. Moving two eyes touches
// a small part of the screen, so this is several times faster than a full
// refresh over I2C.
static void pushDisplay() {
  canvas.toPages(pages);
  int tx0 = 16, ty0 = 8, tx1 = -1, ty1 = -1;
  for (int ty = 0; ty < 8; ty++) {
    for (int tx = 0; tx < 16; tx++) {
      if (firstFrame || memcmp(pages + ty * 128 + tx * 8, shown + ty * 128 + tx * 8, 8) != 0) {
        if (tx < tx0) tx0 = tx;
        if (tx > tx1) tx1 = tx;
        if (ty < ty0) ty0 = ty;
        if (ty > ty1) ty1 = ty;
      }
    }
  }
  if (tx1 < 0) return;  // nothing changed
  memcpy(u8g2.getBufferPtr(), pages, CANVAS_BYTES);
  u8g2.updateDisplayArea(tx0, ty0, tx1 - tx0 + 1, ty1 - ty0 + 1);
  memcpy(shown, pages, CANVAS_BYTES);
  firstFrame = false;
}

// ---- serial console -----------------------------------------------------------------
//   <expression>      go to that face (try: happy, love, furious, asleep ...)
//   act <name>        play an act (try: pacman, pong, sneeze, fly, yes, no ...)
//   say <text>        speech balloon
//   clip <name>       play a GIF clip          list    names of faces, acts and clips
//   auto on|off       autonomous behaviour     info

More projects by zhouzhi0713