Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EOL refactor prototype #183

Draft
wants to merge 14 commits into
base: ayab-esp32
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions .gitmodules

This file was deleted.

57 changes: 57 additions & 0 deletions boards/ayab-esp32.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"build": {
"arduino":{
"ldscript": "esp32s3_out.ld",
"partitions": "default.csv"
},
"core": "esp32",
"extra_flags": [
"-DARDUINO_USB_CDC_ON_BOOT=1",
"-DARDUINO_RUNNING_CORE=1",
"-DARDUINO_EVENT_RUNNING_CORE=1",
"-DBOARD_HAS_PSRAM"
],
"f_cpu": "240000000L",
"f_flash": "80000000L",
"flash_mode": "qio",
"hwids": [
[
"0x239A",
"0x811B"
],
[
"0x239A",
"0x011B"
],
[
"0x239A",
"0x811C"
]
],
"mcu": "esp32s3",
"variant": "esp32s3"
},
"connectivity": [
"bluetooth",
"wifi"
],
"debug": {
"openocd_target": "esp32s3.cfg"
},
"frameworks": [
"arduino",
"espidf"
],
"name": "AYAB-ESP32",
"upload": {
"flash_size": "4MB",
"maximum_ram_size": 327680,
"maximum_size": 4194304,
"use_1200bps_touch": true,
"wait_for_upload_port": true,
"require_upload_port": true,
"speed": 460800
},
"url": "https://ayab-knitting.com",
"vendor": "AllYarnsAreBeautiful"
}
1 change: 0 additions & 1 deletion lib/Adafruit_MCP23008
Submodule Adafruit_MCP23008 deleted from 05fc9b
1 change: 0 additions & 1 deletion lib/PacketSerial
Submodule PacketSerial deleted from 44c5b0
11 changes: 9 additions & 2 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,20 @@
; https://docs.platformio.org/page/projectconf.html

[platformio]
default_envs = uno
default_envs = uno, ayab-esp32

[env]
framework = arduino
extra_scripts =
pre:scripts/preBuild.py
pre:scripts/preBuild.py
lib_deps =
adafruit/Adafruit MCP23017 Arduino Library@^2.3.2
bakercp/PacketSerial@^1.4.0

[env:uno]
platform = atmelavr
board = uno

[env:ayab-esp32]
platform = espressif32
board = ayab-esp32
77 changes: 67 additions & 10 deletions src/ayab/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@

#include <Arduino.h>

#ifdef DBG_NOMACHINE // Turn on to use DBG_BTN_PIN as EOL Trigger
constexpr uint8_t DBG_BTN_PIN = 7; // DEBUG BUTTON
#endif

// TODO(Who?): Optimize Delay for various Arduino Models (does ESP32 need this? Probably no?)
constexpr uint16_t START_KNITTING_DELAY = 2000U; // ms

// Determine board type
#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
// Arduino Uno

#define HAS_MCP23008
#define EOL_ANALOG

constexpr uint8_t I2Caddr_sol1_8 = 0x0U; ///< I2C Address of solenoids 1 - 8
constexpr uint8_t I2Caddr_sol9_16 = 0x1U; ///< I2C Address of solenoids 9 - 16

// Pin Assignments
constexpr uint8_t EOL_PIN_R = A0; // Analog
constexpr uint8_t EOL_PIN_L = A1; // Analog
Expand All @@ -39,20 +56,60 @@ constexpr uint8_t LED_PIN_A = 5; // green LED
constexpr uint8_t LED_PIN_B = 6; // yellow LED

constexpr uint8_t PIEZO_PIN = 9;
#elif defined(CONFIG_IDF_TARGET_ESP32S3)// ESP32-S3 (AYAB-ESP32)

#ifdef DBG_NOMACHINE // Turn on to use DBG_BTN_PIN as EOL Trigger
constexpr uint8_t DBG_BTN_PIN = 7; // DEBUG BUTTON
#endif
#define HAS_MCP23017
#define EOL_COMPARATOR

constexpr uint8_t I2Caddr_sol1_8 = 0x0U; ///< I2C Address of solenoids 1 - 8
constexpr uint8_t I2Caddr_sol9_16 = 0x1U; ///< I2C Address of solenoids 9 - 16
constexpr uint8_t MCP23017_ADDR_0 = 0x0U; // I2C address of expander on ayab-esp32 (16-wide)

// TODO(Who?): Optimize Delay for various Arduino Models
constexpr uint16_t START_KNITTING_DELAY = 2000U; // ms
// Knitting machine control
constexpr uint8_t EOL_PIN_R_N = 3; // Right EOL marker, each polarity.
constexpr uint8_t EOL_PIN_R_S = 4;

constexpr uint8_t EOL_PIN_L_N = 1; // Left EOL marker, each polarity.
constexpr uint8_t EOL_PIN_L_S = 2;

constexpr uint8_t ENC_PIN_A = 5; // Carriage movement A
constexpr uint8_t ENC_PIN_B = 6; // Carriage movement B
constexpr uint8_t ENC_PIN_C = 7; // Carriage belt phase alignment.

constexpr uint8_t MCP_SDA_PIN = 8; // Internal I2C bus with MCP GPIO expander.
constexpr uint8_t MCP_SCL_PIN = 9; // I2C0

// Communication busses
constexpr uint8_t SPI_PIN_COPI = 12; // Internal SPI bus for future display.
constexpr uint8_t SPI_PIN_CIPO = 11; // SPI0
constexpr uint8_t SPI_PIN_SCK = 13;
constexpr uint8_t SPI_PIN_CS = 10;

constexpr uint8_t UART_PIN_TX = 43; // External bus for debugging and/or user.
constexpr uint8_t UART_PIN_RX = 44; // UART0

constexpr uint8_t I2C_PIN_SDA = 15; // External bus for user applications.
constexpr uint8_t I2C_PIN_SCL = 16; // I2C1

// Misc I/O
constexpr uint8_t LED_PIN_R = 33;
constexpr uint8_t LED_PIN_A = 33;
constexpr uint8_t LED_PIN_G = 34;
constexpr uint8_t LED_PIN_B = 35;

constexpr uint8_t PIEZO_PIN = 38;

// User I/O
constexpr uint8_t USER_BUTTON = 36;

constexpr uint8_t USER_PIN_14 = 14; // Should these actually be like USER_0... etc?
constexpr uint8_t USER_PIN_17 = 17; // And then on the silk/ enclosure we put friendly numbers (0..8) instead of GPIO name?
constexpr uint8_t USER_PIN_18 = 18;
constexpr uint8_t USER_PIN_21 = 21;

constexpr uint8_t USER_PIN_39 = 39;
constexpr uint8_t USER_PIN_40 = 40;
constexpr uint8_t USER_PIN_41 = 41;
constexpr uint8_t USER_PIN_42 = 42;

// Determine board type
#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
// Arduino Uno
#else
#error "untested board"
#endif
Expand Down
25 changes: 17 additions & 8 deletions src/ayab/com.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,28 @@ void Com::send_reqLine(const uint8_t lineNumber, Err_t error) const {
* \param position Position of knitting carriage in needles from left hand side.
* \param initState State of readiness (0 = ready, other values = not ready).
*/
void Com::send_indState(Carriage_t carriage, uint8_t position,
Err_t error) const {
uint16_t leftHallValue = GlobalEncoders::getHallValue(Direction_t::Left);
uint16_t rightHallValue = GlobalEncoders::getHallValue(Direction_t::Right);
void Com::send_indState(Carriage_t carriage, uint8_t position, Err_t error) const {
#if defined(EOL_ANALOG)
uint16_t leftHallValueAnalog = analogRead(EOL_PIN_L);
uint16_t rightHallValueAnalog = analogRead(EOL_PIN_R);

#elif defined(EOL_COMPARATOR)
// There are no analog values for the comparator type board.
uint16_t leftHallValueAnalog = 0xFFFF;
uint16_t rightHallValueAnalog = 0xFFFF;

#endif
// `payload` will be allocated on stack since length is compile-time constant
uint8_t payload[INDSTATE_LEN] = {
static_cast<uint8_t>(AYAB_API::indState),
static_cast<uint8_t>(error),
static_cast<uint8_t>(GlobalFsm::getState()),
highByte(leftHallValue),
lowByte(leftHallValue),
highByte(rightHallValue),
lowByte(rightHallValue),
highByte(leftHallValueAnalog),
lowByte(leftHallValueAnalog),
highByte(rightHallValueAnalog),
lowByte(rightHallValueAnalog),
static_cast<uint8_t>(GlobalEncoders::getHallValue(Direction_t::Left)),
static_cast<uint8_t>(GlobalEncoders::getHallValue(Direction_t::Right)),
static_cast<uint8_t>(carriage),
position,
static_cast<uint8_t>(GlobalEncoders::getDirection()),
Expand Down
2 changes: 1 addition & 1 deletion src/ayab/com.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ enum class AYAB_API : unsigned char {
using AYAB_API_t = enum AYAB_API;

// API constants
constexpr uint8_t INDSTATE_LEN = 10U;
constexpr uint8_t INDSTATE_LEN = 12U;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Please update the API description in ayab-manual accordingly
  • Is there a change on the desktop side required as well to be compatible to this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes are also required in the documentation of the API https://github.com/AllYarnsAreBeautiful/ayab-manual/blob/1.0.0-dev/docs/API.md

constexpr uint8_t REQLINE_LEN = 3U;

class ComInterface {
Expand Down
87 changes: 68 additions & 19 deletions src/ayab/encoders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@
* http://ayab-knitting.com
*/

#include "board.h"
#include <Arduino.h>

#include "board.h"
#include "encoders.h"


Expand All @@ -52,15 +51,72 @@ void Encoders::encA_interrupt() {
* \brief Read hall sensor on left and right.
* \param pSensor Which sensor to read (left or right).
*/
uint16_t Encoders::getHallValue(Direction_t pSensor) {
HallState_t Encoders::getHallValue(Direction_t pSensor) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a HallState is return from now on, we could rename the method to getHallState instead of getHallValue.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I think that makes sense.

#if defined(EOL_ANALOG)
// Need to check if the polarity matches with the correct voltage swing.
uint16_t sensorValue;

switch (pSensor) {
case Direction_t::Left:

sensorValue = analogRead(EOL_PIN_L);
if(sensorValue < FILTER_L_MIN[static_cast<uint8_t>(m_machineType)]) {
return HallState_t::North;
}
else if (sensorValue > FILTER_L_MAX[static_cast<uint8_t>(m_machineType)]) {
return HallState_t::South;
}
else {
return HallState_t::None;
}

case Direction_t::Right:

sensorValue = analogRead(EOL_PIN_R);
if(sensorValue < FILTER_R_MIN[static_cast<uint8_t>(m_machineType)]) {
return HallState_t::North;
}
else if (sensorValue > FILTER_R_MAX[static_cast<uint8_t>(m_machineType)]) {
return HallState_t::South;
}
else {
return HallState_t::None;
}

default:
return HallState_t::Error;
}

#elif defined(EOL_COMPARATOR)

switch (pSensor) {
case Direction_t::Left:
return analogRead(EOL_PIN_L);
if(digitalRead(EOL_PIN_L_N)) {
return HallState_t::North;
}
else if(digitalRead(EOL_PIN_L_S)){
return HallState_t::South;
}
else {
return HallState::None;
}

case Direction_t::Right:
return analogRead(EOL_PIN_R);
if(digitalRead(EOL_PIN_R_N)) {
return HallState_t::North;
}
else if(digitalRead(EOL_PIN_R_S)){
return HallState_t::South;
}
else {
return HallState::None;
}

default:
return 0;
return HallState_t::Error;
}

#endif
}

/*!
Expand Down Expand Up @@ -150,15 +206,14 @@ void Encoders::encA_rising() {
}

// In front of Left Hall Sensor?
uint16_t hallValue = analogRead(EOL_PIN_L);
if ((hallValue < FILTER_L_MIN[static_cast<uint8_t>(m_machineType)]) ||
(hallValue > FILTER_L_MAX[static_cast<uint8_t>(m_machineType)])) {
HallState_t hallValue = getHallValue(Direction_t::Left);
if (hallValue == HallState_t::North || hallValue == HallState_t::South) {
m_hallActive = Direction_t::Left;

Carriage detected_carriage = Carriage_t::NoCarriage;
uint8_t start_position = END_LEFT_PLUS_OFFSET[static_cast<uint8_t>(m_machineType)];

if (hallValue >= FILTER_L_MIN[static_cast<uint8_t>(m_machineType)]) {
if (hallValue == HallState_t::North) {
detected_carriage = Carriage_t::Knit;
} else {
detected_carriage = Carriage_t::Lace;
Expand Down Expand Up @@ -208,21 +263,15 @@ void Encoders::encA_falling() {
}

// In front of Right Hall Sensor?
uint16_t hallValue = analogRead(EOL_PIN_R);

// Avoid 'comparison of unsigned expression < 0 is always false'
// by being explicit about that behaviour being expected.
bool hallValueSmall = false;

hallValueSmall = (hallValue < FILTER_R_MIN[static_cast<uint8_t>(m_machineType)]);
HallState_t hallValue = getHallValue(Direction_t::Right);

if (hallValueSmall || hallValue > FILTER_R_MAX[static_cast<uint8_t>(m_machineType)]) {
if (hallValue == HallState_t::North || hallValue == HallState_t::South) {
m_hallActive = Direction_t::Right;

// The garter carriage has a second set of magnets that are going to
// pass the sensor and will reset state incorrectly if allowed to
// continue.
if (hallValueSmall && (m_carriage != Carriage_t::Garter)) {
if ((hallValue == HallState_t::North) && (m_carriage != Carriage_t::Garter)) {
m_carriage = Carriage_t::Knit;
}

Expand Down
Loading
Loading