Compare commits

..

No commits in common. "main" and "stepper_custom-driver" have entirely different histories.

52 changed files with 574 additions and 2412 deletions

14
.gitignore vendored
View File

@ -1,29 +1,15 @@
# ESP-IDF default build directory
build
sdkconfig.old
dependencies.lock
# VIM files
*.swp
*.swo
**/.cache
# VS-code
settings.json
# drawio files
*.bkp
# freecad backup files
*.FCStd1
*.FCBak
# stl files are usually temporary
*.stl
# kicad backup files
pcb/*/*backups/
# other
octave-workspace
del
screenshots

View File

@ -6,4 +6,4 @@ cmake_minimum_required(VERSION 3.5)
#set(EXTRA_COMPONENT_DIRS ../esp-idf-lib/components)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(cable-length-cutter)
project(cable-length-cutting)

104
README.md
View File

@ -1,94 +1,18 @@
# Overview
Firmware and documentation of a custom built machine that winds and cuts cable to certain lengths.
Extensive details about this project can be found on the website: [pfusch.zone/cable-length-cutter](https://pfusch.zone/cable-length-cutter)
<img src="img/cable-length-cutter.jpg" alt="Photo machine" style="width:60%;">
Photo of the built machine
## Current Features
- Measure length using rotary encoder
- Wind to set length while start-button is pressed
- Automatic wire cutter
- Control interface:
- 2x 7-Segment display showing lengths and notifications
- Buzzer for acoustic notifications
- 4 Buttons and Potentiometer for setting the target length
- Reset and Cut Button
- Stepper motor controlling a linear axis guiding the cable while winding
- Store last axis position at shutdown
# Usage
![Panel Layout](img/panel-layout.svg)
# Electrical Details
For detailed documentation of all electrical aspects, see [connection-plan.pdf](connection-plan.pdf).
# Printed Parts
All designed and 3D-printed parts can be found in the [cad/](cad/) folder. These parts were designed using FreeCAD.
# Hardware
## connection plan
See [connection-plan.pdf](connection-plan.pdf)
# Components
### Custom pcb with ESP-32 microcontroller
See [connection-plan.pdf](connection-plan.pdf)
## rotary encoder LPD3806-600BM-G5-24C
- Pulses: 600 p/r (Single-phase 600 pulses /R,Two phase 4 frequency doubling to 2400 pulses)
- Power source: DC5-24V
- Shaft: 6*13mm/0.23*0.51"
- Size: 38*35.5mm/1.49*1.39"
- Output :AB 2phase output rectangular orthogonal pulse circuit, the output for the NPN open collector output type
- Maximum mechanical speed: 5000 R / min
- Response frequency: 0-20KHz
- Cable length: 1.5 meter
- size: http://domoticx.com/wp-content/uploads/2020/05/LPD3806-afmetingen.jpg
- Wires: Green = A phase, white = B phase, red = Vcc power +, black = V0
### Rotary encoder LPD3806-600BM-G5-24C
```
- Pulses: 600 p/r (Single-phase 600 pulses /R,Two phase 4 frequency doubling to 2400 pulses)
- Power source: DC5-24V
- Shaft: 6*13mm/0.23*0.51"
- Size: 38*35.5mm/1.49*1.39"
- Output :AB 2phase output rectangular orthogonal pulse circuit, the output for the NPN open collector output type
- Maximum mechanical speed: 5000 R / min
- Response frequency: 0-20KHz
- Cable length: 1.5 meter
- size: http://domoticx.com/wp-content/uploads/2020/05/LPD3806-afmetingen.jpg
- Wires: Green = A phase, white = B phase, red = Vcc power +, black = V0
```
### Variable Frequency Drive T13-400W-12-H
See [docs/vfd/](docs/vfd/)
### Stepper-driver TB6600
See [docs/stepper-driver_TB6600-Manual.pdf](docs/stepper-driver_TB6600-Manual.pdf)
### Linear Axis with Nema-12 28HB40 Stepper
See [connection-plan.pdf](connection-plan.pdf)
# Installation
For this project **ESP-IDF v4.4.4** is required (with other versions it might not compile)
```bash
#download esp-idf
yay -S esp-idf
#alternatively clone the esp-idf repository from github and do `git checkout release/4.4`
#run installation script in installed folder
/opt/esp-idf/install.sh
```
# Build
### Set up environment
```bash
source /opt/esp-idf/export.sh
```
(run once per terminal)
### Compile
```bash
cd board_single
idf.py build
```
# Flash
- connect FTDI programmer to board (VCC to VCC; TX to RX; RX to TX)
- press REST and BOOT button
- release RESET button (keep pressing boot)
- run flash command:
```bash
idf.py flash
```

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -116,35 +116,21 @@ timer_avail:
esp_err_t DendoStepper::runPos(int32_t relative)
{
//TODO only enable when actually moving
if (ctrl.status == DISABLED) // if motor is disabled, enable it
enableMotor();
if (!relative) // why would u call it with 0 wtf
return ESP_ERR_NOT_SUPPORTED;
if (ctrl.status > IDLE) { //currently moving
bool newDir = (relative < 0); // CCW if <0, else set CW
if (ctrl.dir == newDir){ //current direction is the same
ctrl.statusPrev = ctrl.status; //update previous status
ctrl.status = ctrl.status==COAST ? COAST : ACC; //stay at coast otherwise switch to ACC
calc(abs(relative + ctrl.stepsRemaining)); //calculate new velolcity profile for new+remaining steps
} else { //direction has changed
//direction change not supported TODO wait for decel finish / queue?
STEP_LOGW("DendoStepper", "DIRECTION HOT-CHANGE NOT SUPPORTED - Finising previous move, this command will be ignored");
if (ctrl.status > IDLE)
{ // we are running, we need to adjust steps accordingly, for now just stop the movement
STEP_LOGW("DendoStepper", "Finising previous move, this command will be ignored");
return ESP_ERR_NOT_SUPPORTED;
}
}
else { //current state is IDLE
ctrl.statusPrev = ctrl.status; //update previous status
if (ctrl.status == DISABLED) // if motor is disabled, enable it
enableMotor();
ctrl.status = ACC;
setDir(relative < 0); // set CCW if <0, else set CW
currentPos += relative;
calc(abs(relative)); // calculate velocity profile
}
currentPos += relative; //(target position / not actual)
ESP_ERROR_CHECK(timer_set_alarm_value(conf.timer_group, conf.timer_idx, ctrl.stepInterval)); // set HW timer alarm to stepinterval
//TODO timer has to be stopped before update if already running?
ESP_ERROR_CHECK(timer_start(conf.timer_group, conf.timer_idx)); // start the timer
return ESP_OK;
@ -209,37 +195,6 @@ void DendoStepper::setSpeedMm(uint32_t speed, uint16_t accT, uint16_t decT)
STEP_LOGI("DendoStepper", "Speed set: v=%d mm/s t+=%d s t-=%d s", speed, accT, decT);
}
//CUSTOM - change speed while running
//FIXME: this approach does not work, since calc function would have to be run after change, this will mess up target steps...
//void DendoStepper::changeSpeed(uint32_t speed)
//{
// //TODO reduce duplicate code (e.g. call setSpeed function)
// //change speed
// ctrl.speed = speed;
// //change status to ACC/DEC
// STEP_LOGI("DendoStepper", "Speed changed: from v=%.2f rad/s to v=%.2f rad/s", ctrl.speed, speed);
// if (speed > ctrl.speed) ctrl.status = ACC;
// if (speed < ctrl.speed) ctrl.status = DEC;
//}
//
//void DendoStepper::changeSpeedMm(uint32_t speed)
//{
// //TODO reduce duplicate code (e.g. call setSpeedMm function)
// if (ctrl.stepsPerMm == 0)
// {
// STEP_LOGE("DendoStepper", "Steps per millimeter not set, cannot set the speed!");
// }
// //calc new speed
// float speedNew = speed * ctrl.stepsPerMm;
// //change status to ACC/DEC
// if (speedNew > ctl.speed) ctrl.status = ACC;
// if (speedNew < ctl.speed) ctrl.status = DEC;
// //update speed, log output
// ctrl.speed = speedNew;
// STEP_LOGI("DendoStepper", "Speed changed: from v=%.2f rad/s to v=%.2f rad/s", ctrl.speed, speedNew);
//}
void DendoStepper::setStepsPerMm(uint16_t steps)
{
ctrl.stepsPerMm = steps;
@ -306,7 +261,6 @@ void DendoStepper::stop()
}
ctrl.runInfinite = false;
timer_pause(conf.timer_group, conf.timer_idx); // stop the timer
ctrl.statusPrev = ctrl.status; //update previous status
ctrl.status = IDLE;
ctrl.stepCnt = 0;
gpio_set_level((gpio_num_t)conf.stepPin, 0);
@ -333,20 +287,10 @@ bool DendoStepper::xISR()
ctrl.stepCnt++;
//CUSTOM: track actual precice current position
if (ctrl.dir) {
ctrl.posActual ++;
} else {
ctrl.posActual --;
}
//CUSTOM: track remaining steps for eventually resuming
ctrl.stepsRemaining = ctrl.stepCnt - ctrl.stepCnt;
// we are done
if (ctrl.stepsToGo == ctrl.stepCnt && !ctrl.runInfinite)
{
timer_pause(conf.timer_group, conf.timer_idx); // stop the timer
ctrl.statusPrev = ctrl.status; //update previous status
ctrl.status = IDLE;
ctrl.stepCnt = 0;
return 0;
@ -355,19 +299,16 @@ bool DendoStepper::xISR()
if (ctrl.stepCnt > 0 && ctrl.stepCnt < ctrl.accEnd)
{ // we are accelerating
ctrl.currentSpeed += ctrl.accInc;
ctrl.statusPrev = ctrl.status; //update previous status
ctrl.status = ACC; // we are accelerating, note that*/
}
else if (ctrl.stepCnt > ctrl.coastEnd && !ctrl.runInfinite)
{ // we must be deccelerating then
ctrl.currentSpeed -= ctrl.decInc;
ctrl.statusPrev = ctrl.status; //update previous status
ctrl.status = DEC; // we are deccelerating
}
else
{
ctrl.currentSpeed = ctrl.targetSpeed;
ctrl.statusPrev = ctrl.status; //update previous status
ctrl.status = COAST; // we are coasting
}
@ -375,18 +316,15 @@ bool DendoStepper::xISR()
// set alarm to calculated interval and disable pin
GPIO.out_w1tc = (1ULL << conf.stepPin);
timer_set_alarm_value(conf.timer_group, conf.timer_idx, ctrl.stepInterval);
ctrl.stepCnt++;
return 1;
}
void DendoStepper::calc(uint32_t targetSteps)
{
//steps from ctrl.speed -> 0:
ctrl.accSteps = 0.5 * ctrl.acc * (ctrl.speed / ctrl.acc) * (ctrl.speed / ctrl.acc);
ctrl.decSteps = 0.5 * ctrl.dec * (ctrl.speed / ctrl.dec) * (ctrl.speed / ctrl.dec);
//steps from 0 -> ctrl.speed:
//ctrl.accSteps = 0.5 * ctrl.acc * (ctrl.speed / ctrl.acc) * (ctrl.speed / ctrl.acc);
//steps from ctrl.currentSpeed -> ctrl.speed:
ctrl.accSteps = 0.5 * ctrl.acc * (ctrl.speed / ctrl.acc) * (ctrl.speed / ctrl.acc) * (ctrl.speed - ctrl.currentSpeed) / ctrl.speed;
if (targetSteps < (ctrl.decSteps + ctrl.accSteps))
{

View File

@ -94,9 +94,6 @@ typedef struct
float dec = 100; // decceleration in rad*second^-2
uint32_t accSteps = 0;
uint32_t decSteps = 0;
int32_t stepsRemaining = 0;
uint64_t posActual = 0; //actual current pos incremented at every step
uint8_t statusPrev = DISABLED; //FIXME currently unused
uint8_t status = DISABLED;
bool dir = CW;
bool runInfinite = false;
@ -211,9 +208,6 @@ public:
*/
void setSpeedMm(uint32_t speed, uint16_t accT, uint16_t decT);
//CUSTOM: change speed while running
void changeSpeedMm(uint32_t speed);
/**
* @brief Set steps per 1 mm of linear movement
*

File diff suppressed because one or more lines are too long

Binary file not shown.

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 144 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 128 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 144 KiB

View File

@ -1,6 +1,6 @@
<mxfile host="Electron" modified="2024-03-14T10:08:17.453Z" agent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) draw.io/23.1.5 Chrome/120.0.6099.109 Electron/28.2.5 Safari/537.36" etag="IGTPBlitl9d9z0sPWQV8" version="23.1.5" type="device" pages="2">
<mxfile host="Electron" modified="2023-04-24T10:25:15.632Z" agent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) draw.io/21.1.2 Chrome/106.0.5249.199 Electron/21.4.3 Safari/537.36" etag="MGrY-6I6opffbVIVdFHA" version="21.1.2" type="device" pages="2">
<diagram id="pYACIs8yIpksGCPkkI0E" name="control, cutter, buttons, encoder">
<mxGraphModel dx="1194" dy="741" grid="1" gridSize="5" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="1169" pageHeight="1654" math="0" shadow="0">
<mxGraphModel dx="1240" dy="730" grid="1" gridSize="5" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="1169" pageHeight="1654" math="0" shadow="0">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
@ -25,7 +25,7 @@
<mxCell id="Jo9Ue6hLqtK935TEgpBK-28" value="SW_RESET.state" style="rhombus;whiteSpace=wrap;html=1;" parent="E9gbP4vXqY4O4zoDVCtS-22" vertex="1">
<mxGeometry x="120" y="110" width="120" height="70" as="geometry" />
</mxCell>
<mxCell id="Jo9Ue6hLqtK935TEgpBK-29" value="// lengthNow = 0&lt;br&gt;encoder_reset()&lt;br&gt;guide_moveToZero()" style="rounded=1;whiteSpace=wrap;html=1;" parent="E9gbP4vXqY4O4zoDVCtS-22" vertex="1">
<mxCell id="Jo9Ue6hLqtK935TEgpBK-29" value="// lengthNow = 0&lt;br&gt;encoder_reset()" style="rounded=1;whiteSpace=wrap;html=1;" parent="E9gbP4vXqY4O4zoDVCtS-22" vertex="1">
<mxGeometry x="120" y="240" width="120" height="60" as="geometry" />
</mxCell>
<mxCell id="Jo9Ue6hLqtK935TEgpBK-30" value="true" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;" parent="E9gbP4vXqY4O4zoDVCtS-22" source="Jo9Ue6hLqtK935TEgpBK-28" target="Jo9Ue6hLqtK935TEgpBK-29" edge="1">
@ -205,7 +205,7 @@
</mxGeometry>
</mxCell>
<mxCell id="1-PaxUM0a53oYDua-TxR-2" value="autoCutEnabled == true" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.75;exitY=1;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" parent="E9gbP4vXqY4O4zoDVCtS-33" source="Jo9Ue6hLqtK935TEgpBK-20" target="1-PaxUM0a53oYDua-TxR-1" edge="1">
<mxGeometry x="-0.6954" y="6" relative="1" as="geometry">
<mxGeometry x="-0.4778" y="6" relative="1" as="geometry">
<Array as="points">
<mxPoint x="299" y="1100" />
<mxPoint x="267" y="1100" />
@ -230,11 +230,11 @@
<mxCell id="8zFeNdrfwlNlW0LlzPxK-13" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=1;exitY=0.5;exitDx=0;exitDy=0;entryX=0;entryY=0.5;entryDx=0;entryDy=0;startArrow=none;startFill=0;" parent="E9gbP4vXqY4O4zoDVCtS-33" source="Jo9Ue6hLqtK935TEgpBK-19" target="Avus2w3zFXZjXSa-NAtZ-22" edge="1">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="8zFeNdrfwlNlW0LlzPxK-14" value="wind at dynamic speed&lt;br&gt;(slower speed when closer to target)" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontColor=#0000FF;labelBackgroundColor=none;" parent="E9gbP4vXqY4O4zoDVCtS-33" vertex="1">
<mxGeometry x="67" y="824" width="125" height="50" as="geometry" />
<mxCell id="8zFeNdrfwlNlW0LlzPxK-14" value="wind at dynamic speed&lt;br&gt;(slower speed when closer to target)" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontColor=#0000FF;labelBackgroundColor=#FFFFFF;" parent="E9gbP4vXqY4O4zoDVCtS-33" vertex="1">
<mxGeometry x="67" y="839" width="125" height="50" as="geometry" />
</mxCell>
<mxCell id="E9gbP4vXqY4O4zoDVCtS-5" value="&lt;font color=&quot;#990000&quot;&gt;todo:&amp;nbsp;&lt;/font&gt;&lt;span style=&quot;color: rgb(153, 0, 0);&quot;&gt;reverse when target exceeded?&lt;/span&gt;&lt;font color=&quot;#990000&quot;&gt;&lt;br&gt;&lt;br&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontColor=#0000FF;labelBackgroundColor=none;" parent="E9gbP4vXqY4O4zoDVCtS-33" vertex="1">
<mxGeometry x="373.75" y="976.5" width="206.25" height="45" as="geometry" />
<mxCell id="E9gbP4vXqY4O4zoDVCtS-5" value="&lt;font color=&quot;#990000&quot;&gt;todo: &lt;br&gt;&lt;div style=&quot;&quot;&gt;- reverse when target exceeded?&lt;/div&gt;&lt;br&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontColor=#0000FF;labelBackgroundColor=#FFFFFF;" parent="E9gbP4vXqY4O4zoDVCtS-33" vertex="1">
<mxGeometry x="352.5" y="976.5" width="182.5" height="45" as="geometry" />
</mxCell>
<mxCell id="E9gbP4vXqY4O4zoDVCtS-9" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;fontColor=#FFFFFF;" parent="E9gbP4vXqY4O4zoDVCtS-33" source="E9gbP4vXqY4O4zoDVCtS-8" target="Jo9Ue6hLqtK935TEgpBK-18" edge="1">
<mxGeometry relative="1" as="geometry" />
@ -348,13 +348,13 @@
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="Avus2w3zFXZjXSa-NAtZ-22" value="checkStopCondition()" style="rounded=1;whiteSpace=wrap;html=1;" parent="E9gbP4vXqY4O4zoDVCtS-33" vertex="1">
<mxGeometry x="401.25" y="836.5" width="130" height="25" as="geometry" />
<mxGeometry x="405" y="836.5" width="130" height="25" as="geometry" />
</mxCell>
<mxCell id="Jo9Ue6hLqtK935TEgpBK-25" value="&lt;font color=&quot;#0000ff&quot;&gt;start counting (no motor)&lt;br&gt;(pull and attach wire manually to spool)&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;labelBackgroundColor=none;" parent="E9gbP4vXqY4O4zoDVCtS-33" vertex="1">
<mxCell id="Jo9Ue6hLqtK935TEgpBK-25" value="&lt;font color=&quot;#0000ff&quot;&gt;start counting (no motor)&lt;br&gt;(pull and attach wire manually to spool)&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;labelBackgroundColor=#FFFFFF;" parent="E9gbP4vXqY4O4zoDVCtS-33" vertex="1">
<mxGeometry x="53.5" y="390" width="138.5" height="78.5" as="geometry" />
</mxCell>
<mxCell id="8zFeNdrfwlNlW0LlzPxK-15" value="start or resume slow for some time&lt;br&gt;(time for verifying it winds correctly)" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontColor=#0000FF;labelBackgroundColor=none;" parent="E9gbP4vXqY4O4zoDVCtS-33" vertex="1">
<mxGeometry x="75" y="644" width="109.5" height="56" as="geometry" />
<mxCell id="8zFeNdrfwlNlW0LlzPxK-15" value="start or resume slow for some time&lt;br&gt;(time for verifying it spools correctly)" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontColor=#0000FF;labelBackgroundColor=#FFFFFF;" parent="E9gbP4vXqY4O4zoDVCtS-33" vertex="1">
<mxGeometry x="63.75" y="649" width="109.5" height="50" as="geometry" />
</mxCell>
<mxCell id="Avus2w3zFXZjXSa-NAtZ-34" value="&lt;font color=&quot;#0000ff&quot;&gt;manually control the motor via preset buttons and potentiometer&lt;br&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;labelBackgroundColor=#FFFFFF;" parent="E9gbP4vXqY4O4zoDVCtS-33" vertex="1">
<mxGeometry x="817.75" y="648.25" width="138.5" height="78.5" as="geometry" />
@ -362,6 +362,24 @@
<mxCell id="aTO7ynV6Y0lAHxoPAONt-5" value="main loop" style="shape=hexagon;perimeter=hexagonPerimeter2;whiteSpace=wrap;html=1;fixedSize=1;fillColor=#f8cecc;strokeColor=#b85450;" parent="E9gbP4vXqY4O4zoDVCtS-33" vertex="1">
<mxGeometry x="696.75" y="535" width="79" height="28.5" as="geometry" />
</mxCell>
<mxCell id="aTO7ynV6Y0lAHxoPAONt-12" value="WINDING" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" parent="E9gbP4vXqY4O4zoDVCtS-33" source="aTO7ynV6Y0lAHxoPAONt-11" target="Avus2w3zFXZjXSa-NAtZ-22" edge="1">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="aTO7ynV6Y0lAHxoPAONt-11" value="main loop" style="shape=hexagon;perimeter=hexagonPerimeter2;whiteSpace=wrap;html=1;fixedSize=1;fillColor=#f8cecc;strokeColor=#b85450;" parent="E9gbP4vXqY4O4zoDVCtS-33" vertex="1">
<mxGeometry x="430.5" y="774" width="79" height="28.5" as="geometry" />
</mxCell>
<mxCell id="aTO7ynV6Y0lAHxoPAONt-14" value="WINDING_START" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" parent="E9gbP4vXqY4O4zoDVCtS-33" source="aTO7ynV6Y0lAHxoPAONt-13" target="Avus2w3zFXZjXSa-NAtZ-16" edge="1">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="aTO7ynV6Y0lAHxoPAONt-13" value="main loop" style="shape=hexagon;perimeter=hexagonPerimeter2;whiteSpace=wrap;html=1;fixedSize=1;fillColor=#f8cecc;strokeColor=#b85450;" parent="E9gbP4vXqY4O4zoDVCtS-33" vertex="1">
<mxGeometry x="430.5" y="595" width="79" height="28.5" as="geometry" />
</mxCell>
<mxCell id="aTO7ynV6Y0lAHxoPAONt-16" value="COUNTING" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;" parent="E9gbP4vXqY4O4zoDVCtS-33" source="aTO7ynV6Y0lAHxoPAONt-15" target="Avus2w3zFXZjXSa-NAtZ-4" edge="1">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="aTO7ynV6Y0lAHxoPAONt-15" value="main loop" style="shape=hexagon;perimeter=hexagonPerimeter2;whiteSpace=wrap;html=1;fixedSize=1;fillColor=#f8cecc;strokeColor=#b85450;" parent="E9gbP4vXqY4O4zoDVCtS-33" vertex="1">
<mxGeometry x="429" y="390" width="79" height="28.5" as="geometry" />
</mxCell>
<mxCell id="aTO7ynV6Y0lAHxoPAONt-17" value="main loop" style="shape=hexagon;perimeter=hexagonPerimeter2;whiteSpace=wrap;html=1;fixedSize=1;fillColor=#f8cecc;strokeColor=#b85450;" parent="E9gbP4vXqY4O4zoDVCtS-33" vertex="1">
<mxGeometry x="945.37" y="430" width="79" height="28.5" as="geometry" />
</mxCell>
@ -399,15 +417,15 @@
<mxPoint x="265" y="960" as="sourcePoint" />
<mxPoint x="315" y="910" as="targetPoint" />
<Array as="points">
<mxPoint x="434" y="875" />
<mxPoint x="438" y="875" />
<mxPoint x="360" y="875" />
<mxPoint x="360" y="915" />
<mxPoint x="263" y="915" />
<mxPoint x="360" y="900" />
<mxPoint x="263" y="900" />
</Array>
</mxGeometry>
</mxCell>
<mxCell id="aTO7ynV6Y0lAHxoPAONt-55" value="setDynSpeedLvl()" style="rounded=1;whiteSpace=wrap;html=1;" parent="E9gbP4vXqY4O4zoDVCtS-33" vertex="1">
<mxGeometry x="401.25" y="882.5" width="130" height="25" as="geometry" />
<mxGeometry x="403.5" y="893" width="130" height="25" as="geometry" />
</mxCell>
<mxCell id="8zFeNdrfwlNlW0LlzPxK-2" value="lengthRemaining" style="rhombus;whiteSpace=wrap;html=1;" parent="E9gbP4vXqY4O4zoDVCtS-33" vertex="1">
<mxGeometry x="736.88" y="105" width="130" height="60" as="geometry" />
@ -483,8 +501,8 @@
<mxCell id="aTO7ynV6Y0lAHxoPAONt-63" value="setDynSpeedLvl(1)" style="rounded=1;whiteSpace=wrap;html=1;" parent="E9gbP4vXqY4O4zoDVCtS-33" vertex="1">
<mxGeometry x="403.5" y="704" width="130" height="25" as="geometry" />
</mxCell>
<mxCell id="aTO7ynV6Y0lAHxoPAONt-66" value="limit speed lvl to 1&lt;br style=&quot;font-size: 8px;&quot;&gt;can be slower if close though" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontColor=#0000FF;labelBackgroundColor=none;fontSize=8;" parent="E9gbP4vXqY4O4zoDVCtS-33" vertex="1">
<mxGeometry x="334.5" y="704" width="66.75" height="25" as="geometry" />
<mxCell id="aTO7ynV6Y0lAHxoPAONt-66" value="limit speed lvl to 1&lt;br style=&quot;font-size: 9px;&quot;&gt;can be slower if close though" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontColor=#0000FF;labelBackgroundColor=#FFFFFF;fontSize=9;" parent="E9gbP4vXqY4O4zoDVCtS-33" vertex="1">
<mxGeometry x="533.5" y="704" width="55" height="20" as="geometry" />
</mxCell>
<mxCell id="mQTKtX7dmJeWt0bXEoog-30" value="&lt;span style=&quot;color: rgb(0, 0, 0); font-family: Helvetica; font-size: 11px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: center; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; float: none; display: inline !important;&quot;&gt;cutter_isRunning() == false&lt;/span&gt;" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;endArrow=classic;endFill=1;" parent="E9gbP4vXqY4O4zoDVCtS-33" source="mQTKtX7dmJeWt0bXEoog-1" target="mQTKtX7dmJeWt0bXEoog-29" edge="1">
<mxGeometry relative="1" as="geometry" />
@ -498,8 +516,14 @@
<mxCell id="mQTKtX7dmJeWt0bXEoog-4" value="cutter_start()" style="rounded=1;whiteSpace=wrap;html=1;" parent="E9gbP4vXqY4O4zoDVCtS-33" vertex="1">
<mxGeometry x="199.5" y="1255" width="135" height="30" as="geometry" />
</mxCell>
<mxCell id="mQTKtX7dmJeWt0bXEoog-29" value="lengthNow = 0&lt;br&gt;guide_moveToZero()" style="rounded=1;whiteSpace=wrap;html=1;" parent="E9gbP4vXqY4O4zoDVCtS-33" vertex="1">
<mxGeometry x="206.75" y="1435" width="120.5" height="40" as="geometry" />
<mxCell id="mQTKtX7dmJeWt0bXEoog-9" value="&lt;font color=&quot;#990000&quot;&gt;todo: &lt;br&gt;&lt;/font&gt;&lt;span style=&quot;color: rgb(153, 0, 0);&quot;&gt;- ability to CANCEL cutting&lt;/span&gt;&lt;font color=&quot;#990000&quot;&gt;&lt;br&gt;- e.g.&amp;nbsp;&lt;/font&gt;&lt;span style=&quot;color: rgb(153, 0, 0);&quot;&gt;cancel when buttons released?&lt;/span&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontColor=#0000FF;labelBackgroundColor=#FFFFFF;" parent="E9gbP4vXqY4O4zoDVCtS-33" vertex="1">
<mxGeometry x="349.75" y="1320" width="229" height="45" as="geometry" />
</mxCell>
<mxCell id="mQTKtX7dmJeWt0bXEoog-28" value="&lt;font color=&quot;#990000&quot;&gt;todo:require reset switch to be able to start again?&lt;br&gt;&lt;/font&gt;" style="text;html=1;strokeColor=none;fillColor=none;align=left;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontColor=#0000FF;labelBackgroundColor=#FFFFFF;" parent="E9gbP4vXqY4O4zoDVCtS-33" vertex="1">
<mxGeometry x="77" y="1115" width="91.5" height="45" as="geometry" />
</mxCell>
<mxCell id="mQTKtX7dmJeWt0bXEoog-29" value="lengthNow = 0" style="rounded=1;whiteSpace=wrap;html=1;" parent="E9gbP4vXqY4O4zoDVCtS-33" vertex="1">
<mxGeometry x="224.5" y="1430" width="85" height="30" as="geometry" />
</mxCell>
<mxCell id="1-PaxUM0a53oYDua-TxR-4" value="delay passed" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" parent="E9gbP4vXqY4O4zoDVCtS-33" source="1-PaxUM0a53oYDua-TxR-1" target="mQTKtX7dmJeWt0bXEoog-4" edge="1">
<mxGeometry relative="1" as="geometry" />
@ -510,62 +534,14 @@
<mxCell id="1-PaxUM0a53oYDua-TxR-1" value="state:&lt;br&gt;AUTO_CUT_WAITING" style="rounded=0;whiteSpace=wrap;html=1;fontStyle=1" parent="E9gbP4vXqY4O4zoDVCtS-33" vertex="1">
<mxGeometry x="195.75" y="1135" width="142.5" height="60" as="geometry" />
</mxCell>
<mxCell id="1-PaxUM0a53oYDua-TxR-3" value="indicate cut pending on display&lt;br&gt;beep countdown&lt;br&gt;blink lamp" style="rounded=1;whiteSpace=wrap;html=1;align=center;" parent="E9gbP4vXqY4O4zoDVCtS-33" vertex="1">
<mxCell id="1-PaxUM0a53oYDua-TxR-3" value="indicate cut pending on display&lt;br&gt;beep countdown&lt;br&gt;blink lamp" style="rounded=1;whiteSpace=wrap;html=1;" parent="E9gbP4vXqY4O4zoDVCtS-33" vertex="1">
<mxGeometry x="395" y="1142.5" width="185" height="45" as="geometry" />
</mxCell>
<mxCell id="u4D9dyohbDwP2OhtFbo0-1" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=1.008;entryY=0.119;entryDx=0;entryDy=0;entryPerimeter=0;strokeColor=#999999;" edge="1" parent="E9gbP4vXqY4O4zoDVCtS-33" source="aTO7ynV6Y0lAHxoPAONt-63" target="Jo9Ue6hLqtK935TEgpBK-18">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="469" y="745" />
<mxPoint x="555" y="745" />
<mxPoint x="555" y="651" />
</Array>
</mxGeometry>
<mxCell id="1-PaxUM0a53oYDua-TxR-7" value="AUTO_CUT_WAITING" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;" parent="E9gbP4vXqY4O4zoDVCtS-33" source="1-PaxUM0a53oYDua-TxR-6" target="1-PaxUM0a53oYDua-TxR-3" edge="1">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="u4D9dyohbDwP2OhtFbo0-2" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=1.004;entryY=0.1;entryDx=0;entryDy=0;entryPerimeter=0;strokeColor=#999999;" edge="1" parent="E9gbP4vXqY4O4zoDVCtS-33" source="Avus2w3zFXZjXSa-NAtZ-4" target="Jo9Ue6hLqtK935TEgpBK-21">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="469" y="497" />
<mxPoint x="545" y="497" />
<mxPoint x="545" y="440" />
</Array>
</mxGeometry>
</mxCell>
<mxCell id="u4D9dyohbDwP2OhtFbo0-3" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=1.002;entryY=0.144;entryDx=0;entryDy=0;entryPerimeter=0;strokeColor=#999999;" edge="1" parent="E9gbP4vXqY4O4zoDVCtS-33" source="aTO7ynV6Y0lAHxoPAONt-55" target="Jo9Ue6hLqtK935TEgpBK-19">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="469" y="908" />
<mxPoint x="469" y="925" />
<mxPoint x="550" y="925" />
<mxPoint x="550" y="828" />
</Array>
</mxGeometry>
</mxCell>
<mxCell id="u4D9dyohbDwP2OhtFbo0-4" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;entryX=0.91;entryY=0.003;entryDx=0;entryDy=0;entryPerimeter=0;strokeColor=#999999;" edge="1" parent="E9gbP4vXqY4O4zoDVCtS-33" source="1-PaxUM0a53oYDua-TxR-3" target="1-PaxUM0a53oYDua-TxR-1">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="487" y="1208" />
<mxPoint x="600" y="1208" />
<mxPoint x="600" y="1115" />
<mxPoint x="325" y="1115" />
</Array>
</mxGeometry>
</mxCell>
<mxCell id="u4D9dyohbDwP2OhtFbo0-5" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=1;exitY=0.75;exitDx=0;exitDy=0;entryX=1;entryY=0.25;entryDx=0;entryDy=0;strokeColor=#999999;" edge="1" parent="E9gbP4vXqY4O4zoDVCtS-33" source="mQTKtX7dmJeWt0bXEoog-1" target="mQTKtX7dmJeWt0bXEoog-1">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="365" y="1370" />
<mxPoint x="365" y="1340" />
</Array>
</mxGeometry>
</mxCell>
<mxCell id="u4D9dyohbDwP2OhtFbo0-6" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=1;exitY=0.75;exitDx=0;exitDy=0;entryX=1;entryY=0.25;entryDx=0;entryDy=0;fontColor=#999999;strokeColor=#999999;" edge="1" parent="E9gbP4vXqY4O4zoDVCtS-33" source="Jo9Ue6hLqtK935TEgpBK-20" target="Jo9Ue6hLqtK935TEgpBK-20">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="365" y="1014" />
<mxPoint x="365" y="984" />
</Array>
</mxGeometry>
<mxCell id="1-PaxUM0a53oYDua-TxR-6" value="main loop" style="shape=hexagon;perimeter=hexagonPerimeter2;whiteSpace=wrap;html=1;fixedSize=1;fillColor=#f8cecc;strokeColor=#b85450;" parent="E9gbP4vXqY4O4zoDVCtS-33" vertex="1">
<mxGeometry x="448" y="1080" width="79" height="28.5" as="geometry" />
</mxCell>
<mxCell id="E9gbP4vXqY4O4zoDVCtS-37" value="rotary encoder  (control.cpp)" style="swimlane;labelBackgroundColor=none;fontColor=#000000;" parent="1" vertex="1">
<mxGeometry x="1245" y="1415" width="590" height="215" as="geometry" />
@ -811,10 +787,9 @@
<mxGeometry x="-0.5375" relative="1" as="geometry">
<mxPoint x="85" y="1120" as="targetPoint" />
<Array as="points">
<mxPoint x="85" y="1480" />
<mxPoint x="85" y="1470" />
</Array>
<mxPoint x="1" as="offset" />
<mxPoint x="231.5" y="1520" as="sourcePoint" />
</mxGeometry>
</mxCell>
<mxCell id="1-PaxUM0a53oYDua-TxR-8" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.25;exitY=1;exitDx=0;exitDy=0;endArrow=oval;endFill=1;" parent="1" source="1-PaxUM0a53oYDua-TxR-1" edge="1">
@ -826,7 +801,7 @@
</Array>
</mxGeometry>
</mxCell>
<mxCell id="1-PaxUM0a53oYDua-TxR-9" value="any button&lt;br&gt;event" style="edgeLabel;html=1;align=center;verticalAlign=middle;resizable=0;points=[];" parent="1-PaxUM0a53oYDua-TxR-8" vertex="1" connectable="0">
<mxCell id="1-PaxUM0a53oYDua-TxR-9" value="SW_RESET&lt;br&gt;SW_CUT&lt;br&gt;!SW_AUTO_CUT" style="edgeLabel;html=1;align=center;verticalAlign=middle;resizable=0;points=[];" parent="1-PaxUM0a53oYDua-TxR-8" vertex="1" connectable="0">
<mxGeometry x="-0.1235" y="2" relative="1" as="geometry">
<mxPoint x="-22" y="-2" as="offset" />
</mxGeometry>

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 546 KiB

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 348 KiB

View File

@ -1,7 +1,7 @@
idf_component_register(
SRCS
"main.cpp"
"global.cpp"
"config.cpp"
"control.cpp"
"buzzer.cpp"
"vfd.cpp"
@ -11,10 +11,6 @@ idf_component_register(
"stepper.cpp"
"guide-stepper.cpp"
"encoder.cpp"
"shutdown.cpp"
INCLUDE_DIRS
"."
)
# colored build output (errors, warnings...)
idf_build_set_property(COMPILE_OPTIONS "-fdiagnostics-color=always" APPEND)

View File

@ -1,5 +1,5 @@
#include "buzzer.hpp"
#include "config.h"
#include "config.hpp"
static const char *TAG_BUZZER = "buzzer";

View File

@ -1,5 +1,4 @@
#include "global.hpp"
#include "config.h"
#include "config.hpp"
//--- inputs ---

View File

@ -1,151 +0,0 @@
#pragma once
#include "esp_idf_version.h"
//note: global variables and objects were moved to global.hpp
//===================================
//===== define output gpio pins =====
//===================================
//4x stepper mosfet outputs for VFD
#define GPIO_VFD_FWD GPIO_NUM_4 //ST4
#define GPIO_VFD_REV GPIO_NUM_5 //mos2
#define GPIO_VFD_D0 GPIO_NUM_2 //ST2
#define GPIO_VFD_D1 GPIO_NUM_15 //ST1
//#define GPIO_VFD_D2 GPIO_NUM_15 //ST1 (D2 only used with 7.5kw vfd)
#define GPIO_MOS1 GPIO_NUM_18 //mos1 (free) 2022.02.28: pin used for stepper
#define GPIO_LAMP GPIO_NUM_0 //mos2 (5) 2022.02.28: lamp disabled, pin used for stepper
#define GPIO_RELAY GPIO_NUM_13
#define GPIO_BUZZER GPIO_NUM_12
//==================================
//==== define analog input pins ====
//==================================
#define GPIO_POTI GPIO_NUM_36
#define ADC_CHANNEL_POTI ADC1_CHANNEL_0
#define GPIO_4SW_TO_ANALOG GPIO_NUM_39
#define ADC_CHANNEL_4SW_TO_ANALOG ADC1_CHANNEL_3 //gpio 39
#define ADC_CHANNEL ADC_CHANNEL_0
//#define ADC_LOW_VOLTAGE_THRESHOLD 1000 //adc value where shut down is detected (store certain values before power loss)
#define GPIO_PIN GPIO_NUM_2
#define ADC_CHANNEL_SUPPLY_VOLTAGE ADC1_CHANNEL_7//gpio35 onboard supply voltage
//ADC1_CHANNEL_0 gpio36
//ADC1_CHANNEL_6 gpio_34
//ADC1_CHANNEL_3 gpio_39
//=====================================
//==== assign switches to objects =====
//=====================================
//see config.cpp for available evaluated switch objects
#define SW_START sw_gpio_26
#define SW_RESET sw_gpio_25
#define SW_CUTTER_POS sw_gpio_14
#define SW_SET sw_gpio_analog_0
#define SW_PRESET1 sw_gpio_analog_1
#define SW_PRESET2 sw_gpio_analog_2
#define SW_PRESET3 sw_gpio_analog_3
#define SW_CUT sw_gpio_33
#define SW_AUTO_CUT sw_gpio_32
//#define ? sw_gpio_34
//note: actual objects are created in global.cpp
//=============================
//======= configuration =======
//=============================
//--------------------------
//----- display config -----
//--------------------------
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 0, 0)
#define HOST HSPI_HOST
#else
#define HOST SPI2_HOST
#endif
#define DISPLAY_PIN_NUM_MOSI GPIO_NUM_23
#define DISPLAY_PIN_NUM_CLK GPIO_NUM_22
#define DISPLAY_PIN_NUM_CS GPIO_NUM_27
#define DISPLAY_DELAY 2000
#define DISPLAY_BRIGHTNESS 8
//--------------------------
//----- encoder config -----
//--------------------------
#define ROT_ENC_A_GPIO GPIO_NUM_19
#define ROT_ENC_B_GPIO GPIO_NUM_21
#define ENABLE_HALF_STEPS false // Set to true to enable tracking of rotary encoder at half step resolution
#define FLIP_DIRECTION false // Set to true to reverse the clockwise/counterclockwise sense
//--------------------------
//----- stepper config -----
//--------------------------
//enable stepper test mode (dont start control and encoder task)
//#define STEPPER_TEST
//pins
#define STEPPER_STEP_PIN GPIO_NUM_18 //mos1
#define STEPPER_DIR_PIN GPIO_NUM_16 //ST3
//driver settings
#define STEPPER_STEPS_PER_MM (200/2) //steps/mm (steps-per-rot / spindle-slope)
#define STEPPER_SPEED_DEFAULT 25 //mm/s
#define STEPPER_SPEED_MIN 4 //mm/s - speed threshold at which stepper immediately starts/stops
#define STEPPER_ACCEL_INC 3 //steps/s increment per cycle
#define STEPPER_DECEL_INC 7 //steps/s decrement per cycle
//options affecting movement are currently defined in guide-stepper.cpp
//---------------------------
//------- cable guide -------
//---------------------------
// default axis coordinates the guide changes direction (winding width)
#define GUIDE_MIN_MM 0 // TODO add feature so guide stays at zero for some steps (negative MIN_MM?), currently seems appropriate for even winding
#define GUIDE_MAX_MM 90 // 95 still to long at max pos - actual reel is 110, but currently guide turned out to stay at max position for too long, due to cable running diagonal from guide to reel
// tolerance added to last stored position at previous shutdown.
// When calibrating at startup the stepper moves for that sum to get track of zero position (ensure crashes into hardware limit for at least some time)
#define AUTO_HOME_TRAVEL_ADD_TO_LAST_POS_MM 20
#define MAX_TOTAL_AXIS_TRAVEL_MM 103 // max possible travel distance, needed as fallback for auto-home
#define LAYER_THICKNESS_MM 5 // height of one cable layer on reel -> increase in radius every layer
#define D_CABLE 6 // determines winds per layer / guide speed
#define D_REEL 160 // start diameter of empty reel
// max winding width that can be set using potentiometer (SET+PRESET1 buttons)
#define MAX_SELECTABLE_WINDING_WIDTH_MM 100;
// max target length that can be selected using potentiometer (SET button)
#define MAX_SELECTABLE_LENGTH_POTI_MM 100000
// calculate new winding width each time target length changes, according to custom thresholds defined in guide-stepper.cpp
// if not defined, winding width is always GUIDE_MAX_MM even for short lengths
#define DYNAMIC_WINDING_WIDTH_ENABLED
//--------------------------
//------ calibration -------
//--------------------------
//enable mode encoder test for calibration (determine ENCODER_STEPS_PER_METER)
//if defined, displays always show length and steps instead of the normal messages
//#define ENCODER_TEST
//TODO: add way to calibrate without flashing -> enter calibration mode with certain button sequence, enter steps-per-meter with poti, store in nvs
//steps per meter
//this value is determined experimentally while ENCODER_TEST is enabled
//#define ENCODER_STEPS_PER_METER 2127 //until 2024.03.13 roll-v3-gummi-86.6mm - d=89.8mm
#define ENCODER_STEPS_PER_METER 2118 //2024.03.13 roll-v3-gummi measured 86.5mm
//millimeters added to target length
//to ensure that length does not fall short when spool slightly rotates back after stop
#define TARGET_LENGTH_OFFSET 0
//millimeters lengthNow can be below lengthTarget to still stay in target_reached state
#define TARGET_REACHED_TOLERANCE 5

138
main/config.hpp Normal file
View File

@ -0,0 +1,138 @@
#pragma once
extern "C" {
#include "driver/adc.h"
}
#include "gpio_evaluateSwitch.hpp"
#include "buzzer.hpp"
#include "switchesAnalog.hpp"
//===================================
//===== define output gpio pins =====
//===================================
//4x stepper mosfet outputs for VFD
#define GPIO_VFD_FWD GPIO_NUM_4 //ST4
#define GPIO_VFD_REV GPIO_NUM_5 //mos2
#define GPIO_VFD_D0 GPIO_NUM_2 //ST2
#define GPIO_VFD_D1 GPIO_NUM_15 //ST1
//#define GPIO_VFD_D2 GPIO_NUM_15 //ST1 (D2 only used with 7.5kw vfd)
#define GPIO_MOS1 GPIO_NUM_18 //mos1 (free) 2022.02.28: pin used for stepper
#define GPIO_LAMP GPIO_NUM_0 //mos2 (5) 2022.02.28: lamp disabled, pin used for stepper
#define GPIO_RELAY GPIO_NUM_13
#define GPIO_BUZZER GPIO_NUM_12
//==================================
//==== define analog input pins ====
//==================================
#define GPIO_POTI GPIO_NUM_36
#define ADC_CHANNEL_POTI ADC1_CHANNEL_0
#define GPIO_4SW_TO_ANALOG GPIO_NUM_39
#define ADC_CHANNEL_4SW_TO_ANALOG ADC1_CHANNEL_3 //gpio 39
//ADC1_CHANNEL_0 gpio36
//ADC1_CHANNEL_6 gpio_34
//ADC1_CHANNEL_3 gpio_39
//=====================================
//==== assign switches to objects =====
//=====================================
//see config.cpp for available evaluated switch objects
#define SW_START sw_gpio_26
#define SW_RESET sw_gpio_25
#define SW_CUTTER_POS sw_gpio_14
#define SW_SET sw_gpio_analog_0
#define SW_PRESET1 sw_gpio_analog_1
#define SW_PRESET2 sw_gpio_analog_2
#define SW_PRESET3 sw_gpio_analog_3
#define SW_CUT sw_gpio_33
#define SW_AUTO_CUT sw_gpio_32
//unused but already available evaluated inputs
//#define ? sw_gpio_34
//=============================
//======= configuration =======
//=============================
//--------------------------
//----- display config -----
//--------------------------
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 0, 0)
#define HOST HSPI_HOST
#else
#define HOST SPI2_HOST
#endif
#define DISPLAY_PIN_NUM_MOSI GPIO_NUM_23
#define DISPLAY_PIN_NUM_CLK GPIO_NUM_22
#define DISPLAY_PIN_NUM_CS GPIO_NUM_27
#define DISPLAY_DELAY 2000
//--------------------------
//----- encoder config -----
//--------------------------
#define ROT_ENC_A_GPIO GPIO_NUM_19
#define ROT_ENC_B_GPIO GPIO_NUM_21
#define ENABLE_HALF_STEPS false // Set to true to enable tracking of rotary encoder at half step resolution
#define FLIP_DIRECTION false // Set to true to reverse the clockwise/counterclockwise sense
//--------------------------
//----- stepper config -----
//--------------------------
//enable stepper test mode (dont start control and encoder task)
#define STEPPER_TEST
#define STEPPER_STEP_PIN GPIO_NUM_18 //mos1
#define STEPPER_DIR_PIN GPIO_NUM_16 //ST3
#define STEPPER_EN_PIN GPIO_NUM_0 //not connected (-> stepper always on)
//more detailed options for testing are currently defined in guide-stepper.cpp
//--------------------------
//------ calibration -------
//--------------------------
//enable mode encoder test for calibration
//if defined, displays always show length and steps instead of the normal messages
//#define ENCODER_TEST
//steps per meter
#define ENCODER_STEPS_PER_METER 2127 //roll-v3-gummi-86.6mm - d=89.8mm
//millimetres added to target length
//to ensure that length does not fall short when spool slightly rotates back after stop
#define TARGET_LENGTH_OFFSET 0
//millimetres lengthNow can be below lengthTarget to still stay in target_reached state
#define TARGET_REACHED_TOLERANCE 5
//============================
//===== global variables =====
//============================
//create global evaluated switch objects
//--- switches on digital gpio pins ---
//extern gpio_evaluatedSwitch sw_gpio_39;
extern gpio_evaluatedSwitch sw_gpio_34;
extern gpio_evaluatedSwitch sw_gpio_32;
extern gpio_evaluatedSwitch sw_gpio_33;
extern gpio_evaluatedSwitch sw_gpio_25;
extern gpio_evaluatedSwitch sw_gpio_26;
extern gpio_evaluatedSwitch sw_gpio_14;
//--- switches connected to 4-sw-to-analog stripboard ---
extern gpio_evaluatedSwitch sw_gpio_analog_0;
extern gpio_evaluatedSwitch sw_gpio_analog_1;
extern gpio_evaluatedSwitch sw_gpio_analog_2;
extern gpio_evaluatedSwitch sw_gpio_analog_3;
//create global buzzer object
extern buzzer_t buzzer;

View File

@ -1,47 +1,20 @@
extern "C"
{
#include <stdio.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <esp_idf_version.h>
#include "freertos/queue.h"
#include "esp_system.h"
#include "esp_log.h"
#include "driver/adc.h"
#include "max7219.h"
}
#include <cmath>
#include "config.h"
#include "gpio_evaluateSwitch.hpp"
#include "gpio_adc.hpp"
#include "buzzer.hpp"
#include "vfd.hpp"
#include "display.hpp"
#include "cutter.hpp"
#include "encoder.hpp"
#include "guide-stepper.hpp"
#include "global.hpp"
#include "control.hpp"
#include "encoder.hpp"
//-----------------------------------------
//--------------- variables ---------------
//-----------------------------------------
//====================
//==== variables =====
//====================
static const char *TAG = "control"; //tag for logging
//control
const char* systemStateStr[7] = {"COUNTING", "WINDING_START", "WINDING", "TARGET_REACHED", "AUTO_CUT_WAITING", "CUTTING", "MANUAL"};
systemState_t controlState = systemState_t::COUNTING;
static uint32_t timestamp_lastStateChange = 0;
static uint32_t timestamp_changedState = 0;
//display
static char buf_disp1[10];// 8 digits + decimal point + \0
static char buf_disp2[10];// 8 digits + decimal point + \0
static char buf_tmp[15];
//track length
static int lengthNow = 0; //length measured in mm
static int lengthTarget = 5000; //default target length in mm
static int lengthRemaining = 0; //(target - now) length needed for reaching the target
@ -54,19 +27,8 @@ static uint32_t timestamp_cut_lastBeep = 0;
static uint32_t autoCut_delayMs = 2500; //TODO add this to config
static bool autoCutEnabled = false; //store state of toggle switch (no hotswitch)
//user interface
static uint32_t timestamp_lastWidthSelect = 0;
//ignore new set events for that time after last value set using poti
#define DEAD_TIME_POTI_SET_VALUE 1000
//-----------------------------------------
//--------------- functions ---------------
//-----------------------------------------
//========================
//===== change State =====
//========================
//function for changing the controlState with log output
void changeState (systemState_t stateNew) {
//only proceed when state actually changed
@ -78,21 +40,18 @@ void changeState (systemState_t stateNew) {
//change state
controlState = stateNew;
//update timestamp
timestamp_lastStateChange = esp_log_timestamp();
timestamp_changedState = esp_log_timestamp();
}
//=================================
//===== handle Stop Condition =====
//=================================
//function that checks whether start button is released or target is reached
//and takes according action if so (used in multiple states)
//returns true when stop condition was met, false when no action required
//function that checks whether start button is released or target is reached (used in multiple states)
//returns true when stopped, false when no action
bool handleStopCondition(handledDisplay * displayTop, handledDisplay * displayBot){
//--- stop conditions ---
//stop conditions that are checked in any mode
//target reached -> reached state, stop motor, display message
//target reached
if (lengthRemaining <= 0 ) {
changeState(systemState_t::TARGET_REACHED);
vfd_setState(false);
@ -101,7 +60,7 @@ bool handleStopCondition(handledDisplay * displayTop, handledDisplay * displayBo
buzzer.beep(2, 100, 100);
return true;
}
//start button released -> idle state, stop motor, display message
//start button released
else if (SW_START.state == false) {
changeState(systemState_t::COUNTING);
vfd_setState(false);
@ -115,11 +74,8 @@ bool handleStopCondition(handledDisplay * displayTop, handledDisplay * displayBo
}
//===================================
//===== set dynamic speed level =====
//===================================
//function that sets the vfd speed level dynamically depending on the remaining distance
//function that sets the vfd speed level dynamicly depending on the remaining distance
//closer to target -> slower
void setDynSpeedLvl(uint8_t lvlMax = 3){
uint8_t lvl;
@ -147,33 +103,31 @@ void setDynSpeedLvl(uint8_t lvlMax = 3){
//========================
//===== control task =====
//========================
//task that controls the entire machine
void task_control(void *pvParameter)
{
//-- initialize display --
//initialize display
max7219_t two7SegDisplays = display_init();
//create two separate custom handled display instances
//create two separate handled display instances
handledDisplay displayTop(two7SegDisplays, 0);
handledDisplay displayBot(two7SegDisplays, 8);
//-- display welcome msg --
//--- display welcome msg ---
//display welcome message on two 7 segment displays
//currently show name and date and scrolling 'hello'
display_ShowWelcomeMsg(two7SegDisplays);
//-- set initial winding width for default length --
guide_setWindingWidth(guide_targetLength2WindingWidth(lengthTarget));
// ##############################
// ######## control loop ########
// ##############################
// repeatedly handle the machine
//================
//===== loop =====
//================
while(1){
vTaskDelay(10 / portTICK_PERIOD_MS);
//-----------------------------
//------ handle switches ------
//run handle functions for all switches used here
//-----------------------------
//run handle functions for all switches
SW_START.handle();
SW_RESET.handle();
SW_SET.handle();
@ -184,23 +138,27 @@ void task_control(void *pvParameter)
SW_AUTO_CUT.handle();
//---------------------------
//------ handle cutter ------
//TODO: separate task for cutter?
//---------------------------
cutter_handle();
//----------------------------
//------ rotary encoder ------
//get current length since last reset
//----------------------------
//--- get current length since last reset ---
lengthNow = encoder_getLenMm();
//---------------------------
//--------- buttons ---------
//#### RESET switch ####
//---------------------------
//--- RESET switch ---
if (SW_RESET.risingEdge) {
//dont reset when press used for stopping pending auto-cut
if (controlState != systemState_t::AUTO_CUT_WAITING) {
guide_moveToZero(); //move axis guiding the cable to start position
encoder_reset(); //reset length measurement
encoder_reset();
lengthNow = 0;
buzzer.beep(1, 700, 100);
displayTop.blink(2, 100, 100, "1ST ");
@ -209,7 +167,7 @@ void task_control(void *pvParameter)
}
}
//### CUT switch ####
//--- CUT switch ---
//start cut cycle immediately
if (SW_CUT.risingEdge) {
//stop cutter if already running
@ -232,7 +190,7 @@ void task_control(void *pvParameter)
}
}
//#### AUTO_CUT toggle sw ####
//--- AUTO_CUT toggle sw ---
//beep at change
if (SW_AUTO_CUT.risingEdge) {
buzzer.beep(2, 100, 50);
@ -251,7 +209,7 @@ void task_control(void *pvParameter)
autoCutEnabled = false;
}
//#### manual mode ####
//--- manual mode ---
//switch to manual motor control (2 buttons + poti)
if ( SW_PRESET2.state && (SW_PRESET1.state || SW_PRESET3.state) && controlState != systemState_t::MANUAL ) {
//enable manual control
@ -259,44 +217,13 @@ void task_control(void *pvParameter)
buzzer.beep(3, 100, 60);
}
//##### SET switch + Potentiometer #####
//## set winding-width (SET+PRESET1+POTI) ##
// set winding width (axis travel) with poti position
// when SET and PRESET1 button are pressed
if (SW_SET.state == true && SW_PRESET1.state == true) {
timestamp_lastWidthSelect = esp_log_timestamp();
//--- set custom target length ---
//set target length to poti position when SET switch is pressed
if (SW_SET.state == true) {
//read adc
potiRead = gpio_readAdc(ADC_CHANNEL_POTI); //0-4095
//scale to target length range
uint8_t windingWidthNew = (float)potiRead / 4095 * MAX_SELECTABLE_WINDING_WIDTH_MM;
//apply hysteresis and round to whole meters //TODO optimize this
if (windingWidthNew % 5 < 2) { //round down if remainder less than 2mm
ESP_LOGD(TAG, "Poti input = %d -> rounding down", windingWidthNew);
windingWidthNew = (windingWidthNew/5 ) * 5; //round down
} else if (windingWidthNew % 5 > 4 ) { //round up if remainder more than 4mm
ESP_LOGD(TAG, "Poti input = %d -> rounding up", windingWidthNew);
windingWidthNew = (windingWidthNew/5 + 1) * 5; //round up
} else {
ESP_LOGD(TAG, "Poti input = %d -> hysteresis", windingWidthNew);
windingWidthNew = guide_getWindingWidth();
}
//update target width and beep when effectively changed
if (windingWidthNew != guide_getWindingWidth()) {
//TODO update at button release only?
guide_setWindingWidth(windingWidthNew);
ESP_LOGW(TAG, "Changed winding width to %d mm", windingWidthNew);
buzzer.beep(1, 30, 10);
}
}
//## set target length (SET+POTI) ##
//set target length to poti position when only SET button is pressed and certain dead time passed after last setWindingWidth (SET and PRESET1 button) to prevent set target at release
// FIXME: when going to edit the winding width (SET+PRESET1) sometimes the target-length also updates when initially pressing SET -> update only at actual poti change (works sometimes)
else if (SW_SET.state == true && (esp_log_timestamp() - timestamp_lastWidthSelect > DEAD_TIME_POTI_SET_VALUE)) {
//read adc
potiRead = gpio_readAdc(ADC_CHANNEL_POTI); //0-4095
//scale to target length range
int lengthTargetNew = (float)potiRead / 4095 * MAX_SELECTABLE_LENGTH_POTI_MM;
int lengthTargetNew = (float)potiRead / 4095 * 50000;
//apply hysteresis and round to whole meters //TODO optimize this
if (lengthTargetNew % 1000 < 200) { //round down if less than .2 meter
ESP_LOGD(TAG, "Poti input = %d -> rounding down", lengthTargetNew);
@ -312,7 +239,6 @@ void task_control(void *pvParameter)
if (lengthTargetNew != lengthTarget) {
//TODO update lengthTarget only at button release?
lengthTarget = lengthTargetNew;
guide_setWindingWidth(guide_targetLength2WindingWidth(lengthTarget));
ESP_LOGI(TAG, "Changed target length to %d mm", lengthTarget);
buzzer.beep(1, 25, 10);
}
@ -327,40 +253,36 @@ void task_control(void *pvParameter)
}
//##### target length preset buttons #####
if (controlState != systemState_t::MANUAL && SW_SET.state == false) { //dont apply preset length while controlling motor with preset buttons
//--- target length presets ---
if (controlState != systemState_t::MANUAL) { //dont apply preset length while controlling motor with preset buttons
if (SW_PRESET1.risingEdge) {
lengthTarget = 5000;
guide_setWindingWidth(guide_targetLength2WindingWidth(lengthTarget));
buzzer.beep(lengthTarget/1000, 25, 30);
displayBot.blink(2, 100, 100, "S0LL ");
}
else if (SW_PRESET2.risingEdge) {
lengthTarget = 10000;
guide_setWindingWidth(guide_targetLength2WindingWidth(lengthTarget));
buzzer.beep(lengthTarget/1000, 25, 30);
displayBot.blink(2, 100, 100, "S0LL ");
}
else if (SW_PRESET3.risingEdge) {
lengthTarget = 15000;
guide_setWindingWidth(guide_targetLength2WindingWidth(lengthTarget));
buzzer.beep(lengthTarget/1000, 25, 30);
displayBot.blink(2, 100, 100, "S0LL ");
}
}
//---------------------------
//--------- control ---------
//---------------------------
//statemachine handling the sequential winding process
//calculate current length difference
//calculate length difference
lengthRemaining = lengthTarget - lengthNow + TARGET_LENGTH_OFFSET;
//--- statemachine ---
switch (controlState) {
case systemState_t::COUNTING: //no motor action, just show current length on display
case systemState_t::COUNTING: //no motor action
vfd_setState(false);
//TODO check stop condition before starting - prevents motor from starting 2 cycles when already at target
//--- start winding to length ---
@ -376,7 +298,6 @@ void task_control(void *pvParameter)
case systemState_t::WINDING_START: //wind slow for certain time
//set vfd speed depending on remaining distance
setDynSpeedLvl(1); //limit to speed lvl 1 (force slow start)
//switch to WINDING state (full speed) when 3s have passed
if (esp_log_timestamp() - timestamp_motorStarted > 3000) {
changeState(systemState_t::WINDING);
}
@ -386,23 +307,23 @@ void task_control(void *pvParameter)
case systemState_t::WINDING: //wind fast, slow down when close
//set vfd speed depending on remaining distance
setDynSpeedLvl(); //set motor speed, slow down when close to target
setDynSpeedLvl(); //slow down when close to target
handleStopCondition(&displayTop, &displayBot); //stops if button released or target reached
//TODO: cancel when there is no cable movement anymore e.g. empty / timeout?
break;
case systemState_t::TARGET_REACHED: //prevent further motor rotation and start auto-cut
case systemState_t::TARGET_REACHED:
vfd_setState(false);
//switch to counting state when no longer at or above target length
if ( lengthNow < lengthTarget - TARGET_REACHED_TOLERANCE ) {
changeState(systemState_t::COUNTING);
}
//initiate countdown to auto-cut if enabled
//switch initiate countdown to auto-cut
else if ( (autoCutEnabled)
&& (esp_log_timestamp() - timestamp_lastStateChange > 300) ) { //wait for dislay msg "reached" to finish
&& (esp_log_timestamp() - timestamp_changedState > 300) ) { //wait for dislay msg "reached" to finish
changeState(systemState_t::AUTO_CUT_WAITING);
}
//show msg when trying to start, but target is already reached (-> reset button has to be pressed)
//show msg when trying to start, but target is reached
if (SW_START.risingEdge) {
buzzer.beep(2, 50, 30);
displayTop.blink(2, 600, 500, " S0LL ");
@ -410,8 +331,9 @@ void task_control(void *pvParameter)
}
break;
case systemState_t::AUTO_CUT_WAITING: //handle delayed start of cut
cut_msRemaining = autoCut_delayMs - (esp_log_timestamp() - timestamp_lastStateChange);
case systemState_t::AUTO_CUT_WAITING:
//handle delayed start of cut
cut_msRemaining = autoCut_delayMs - (esp_log_timestamp() - timestamp_changedState);
//- countdown stop conditions -
//stop with any button
if (!autoCutEnabled
@ -435,14 +357,13 @@ void task_control(void *pvParameter)
}
break;
case systemState_t::CUTTING: //prevent any action while cutter is active
case systemState_t::CUTTING:
//exit when finished cutting
if (cutter_isRunning() == false) {
//TODO stop if start buttons released?
changeState(systemState_t::COUNTING);
//TODO reset automatically or wait for manual reset?
guide_moveToZero(); //move axis guiding the cable to start position
encoder_reset(); //reset length measurement
encoder_reset();
lengthNow = 0;
buzzer.beep(1, 700, 100);
}
@ -479,34 +400,30 @@ void task_control(void *pvParameter)
}
#ifdef ENCODER_TEST
//--------------------------
//------ encoder test ------
//--------------------------
//mode for calibrating the cable length measurement (determine ENCODER_STEPS_PER_METER in config.h)
#ifdef ENCODER_TEST
//run display handle functions
displayTop.handle();
displayBot.handle();
//-- show encoder steps on display1 ---
sprintf(buf_disp1, "EN %05d", encoder_getSteps()); //count
sprintf(buf_disp1, "EN %05d", encoder_getSteps); //count
displayTop.showString(buf_disp1);
//--- show converted distance on display2 ---
sprintf(buf_disp2, "Met %5.3f", (float)lengthNow/1000); //m
displayBot.showString(buf_disp2);
//--- beep every 0.5m ---
//note: only works precisely in forward/positive direction, in reverse it it beeps by tolerance too early
static int lengthBeeped = 0;
if (lengthNow % 500 < 50) { //with tolerance in case of missed exact value
if (fabs(lengthNow - lengthBeeped) >= 400) { //dont beep multiple times at same distance
//TODO: add case for reverse direction. currently beeps 50mm too early
if (lengthNow % 1000 < 50) // 1m beep
buzzer.beep(1, 400, 100);
else // 0.5m beep
buzzer.beep(1, 200, 100);
//--- beep every 1m ---
//note: only works precicely in forward/positive direction
if (lengthNow % 1000 < 50) { //with tolerance in case of missed exact value
if (fabs(lengthNow - lengthBeeped) >= 900) { //dont beep multiple times at same meter
//TODO: add case for reverse direction. currently beeps 0.1 too early
buzzer.beep(1, 400, 100 );
lengthBeeped = lengthNow;
}
}
#else //not in encoder calibration mode
#else
//--------------------------
//-------- display1 --------
@ -517,10 +434,6 @@ void task_control(void *pvParameter)
if (controlState == systemState_t::AUTO_CUT_WAITING) {
displayTop.blinkStrings(" CUT 1N ", " ", 70, 30);
}
//setting winding width: blink info message
else if (SW_SET.state && SW_PRESET1.state){
displayTop.blinkStrings("SET WIND", " WIDTH ", 900, 900);
}
//otherwise show current position
else {
sprintf(buf_tmp, "1ST %5.4f", (float)lengthNow/1000);
@ -530,13 +443,23 @@ void task_control(void *pvParameter)
displayTop.showString(buf_disp1);
}
//--------------------------
//-------- display2 --------
//--------------------------
//run handle function
displayBot.handle();
//setting target length: blink target length
if (SW_SET.state == true) {
sprintf(buf_tmp, "S0LL%5.3f", (float)lengthTarget/1000);
displayBot.blinkStrings(buf_tmp, "S0LL ", 300, 100);
}
//manual state: blink "manual"
else if (controlState == systemState_t::MANUAL) {
displayBot.blinkStrings(" MANUAL ", buf_disp2, 400, 800);
}
//notify that cutter is active
if (cutter_isRunning()) {
else if (cutter_isRunning()) {
displayBot.blinkStrings("CUTTING]", "CUTTING[", 100, 100);
}
//show ms countdown to cut when pending
@ -545,20 +468,6 @@ void task_control(void *pvParameter)
//displayBot.showString(buf_disp2); //TODO:blink "erreicht" overrides this. for now using blink as workaround
displayBot.blinkStrings(buf_disp2, buf_disp2, 100, 100);
}
//manual state: blink "manual"
else if (controlState == systemState_t::MANUAL) {
displayBot.blinkStrings(" MANUAL ", buf_disp2, 400, 800);
}
//setting winding width: blink currently set windingWidth
else if (SW_SET.state && SW_PRESET1.state){
sprintf(buf_tmp, " %03d mm", guide_getWindingWidth());
displayBot.blinkStrings(buf_tmp, " ", 300, 100);
}
//setting target length: blink target length
else if (SW_SET.state == true) {
sprintf(buf_tmp, "S0LL%5.3f", (float)lengthTarget/1000);
displayBot.blinkStrings(buf_tmp, "S0LL ", 300, 100);
}
//otherwise show target length
else {
//sprintf(buf_disp2, "%06.1f cm", (float)lengthTarget/10); //cm
@ -567,7 +476,6 @@ void task_control(void *pvParameter)
displayBot.showString(buf_tmp);
}
#endif // end else ifdef ENCODER_TEST
//----------------------------
//------- control lamp -------
@ -584,6 +492,10 @@ void task_control(void *pvParameter)
gpio_set_level(GPIO_LAMP, 0);
}
} //end while(1)
} //end task_control
#endif
}
}

View File

@ -1,12 +1,34 @@
#pragma once
extern "C"
{
#include <stdio.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <esp_idf_version.h>
#include "freertos/queue.h"
#include "esp_system.h"
#include "esp_log.h"
#include "driver/adc.h"
#include "max7219.h"
}
#include <cmath>
#include "config.hpp"
#include "gpio_evaluateSwitch.hpp"
#include "gpio_adc.hpp"
#include "buzzer.hpp"
#include "vfd.hpp"
#include "display.hpp"
#include "cutter.hpp"
//enum describing the state of the system
enum class systemState_t {COUNTING, WINDING_START, WINDING, TARGET_REACHED, AUTO_CUT_WAITING, CUTTING, MANUAL};
//array with enum as strings for logging states
extern const char* systemStateStr[7];
//task that controls the entire machine (has to be created as task in main function)
void task_control(void *pvParameter);

View File

@ -1,6 +1,4 @@
#include "cutter.hpp"
#include "config.h"
#include "global.hpp"
const char* cutter_stateStr[5] = {"IDLE", "START", "CUTTING", "CANCELED", "TIMEOUT"}; //define strings for logging the state
@ -41,6 +39,7 @@ void cutter_stop(){
if(cutter_state != cutter_state_t::IDLE){
setState(cutter_state_t::CANCELED);
}
//starts motor on state change
}
@ -132,6 +131,7 @@ void cutter_handle(){
//SW_CUTTER_POS.minOnMs = 10;
//SW_CUTTER_POS.minOffMs = 10;
switch(cutter_state){
case cutter_state_t::IDLE:
case cutter_state_t::TIMEOUT:
@ -142,13 +142,14 @@ void cutter_handle(){
case cutter_state_t::START:
//--- moved away from idle position ---
//if (gpio_get_level(GPIO_CUTTER_POS_SW) == 0){ //contact closed
if (SW_CUTTER_POS.state == true) { //contact closed -> not at idle pos anymore
if (SW_CUTTER_POS.state == true) { //contact closed -> not at idle pos
setState(cutter_state_t::CUTTING);
}
//--- timeout ---
else {
checkTimeout();
}
break;
case cutter_state_t::CUTTING:

View File

@ -1,6 +1,5 @@
#include "display.hpp"
#include "config.h"
//=== variables ===
static const char *TAG = "display"; //tag for logging
@ -35,7 +34,7 @@ max7219_t display_init(){
ESP_ERROR_CHECK(max7219_init_desc(&dev, HOST, MAX7219_MAX_CLOCK_SPEED_HZ, DISPLAY_PIN_NUM_CS));
ESP_ERROR_CHECK(max7219_init(&dev));
//0...15
ESP_ERROR_CHECK(max7219_set_brightness(&dev, DISPLAY_BRIGHTNESS));
ESP_ERROR_CHECK(max7219_set_brightness(&dev, 8)); //TODO add this to config
return dev;
//display = dev;
ESP_LOGI(TAG, "initializing display - done");
@ -51,7 +50,7 @@ void display_ShowWelcomeMsg(max7219_t dev){
//show name and date
ESP_LOGI(TAG, "showing startup message...");
max7219_clear(&dev);
max7219_draw_text_7seg(&dev, 0, "CUTTER 15.03.2024");
max7219_draw_text_7seg(&dev, 0, "CUTTER 29.09.2022");
// 1234567812 34 5678
vTaskDelay(pdMS_TO_TICKS(700));
//scroll "hello" over 2 displays
@ -65,7 +64,7 @@ void display_ShowWelcomeMsg(max7219_t dev){
//noticed rare bug that one display does not initialize / is not configured correctly after start
//initialize display again after the welcome message in case it did not work the first time
ESP_ERROR_CHECK(max7219_init(&dev));
ESP_ERROR_CHECK(max7219_set_brightness(&dev, DISPLAY_BRIGHTNESS));
ESP_ERROR_CHECK(max7219_set_brightness(&dev, 8)); //TODO add this to config
}
@ -84,9 +83,9 @@ handledDisplay::handledDisplay(max7219_t displayDevice, uint8_t posStart_f) {
//================================
//========== showString ==========
//================================
//--------------------------------
//---------- showString ----------
//--------------------------------
//function that displays a given string on the display
void handledDisplay::showString(const char * buf, uint8_t pos_f){
//calculate actual absolute position
@ -104,11 +103,11 @@ void handledDisplay::showString(const char * buf, uint8_t pos_f){
//TODO: blinkStrings() and blink() are very similar - can be optimized?
//only difficulty is the reset behaivor of blinkStrings through showString (blink does not reset)
//only difficulty currently is the reset behaivor of blinkStrings through showString (blink does not reset)
//==================================
//========== blinkStrings ==========
//==================================
//----------------------------------
//---------- blinkStrings ----------
//----------------------------------
//function switches between two strings in a given interval
void handledDisplay::blinkStrings(const char * strOn_f, const char * strOff_f, uint32_t msOn_f, uint32_t msOff_f){
//copy/update variables
@ -131,9 +130,9 @@ void handledDisplay::blinkStrings(const char * strOn_f, const char * strOff_f, u
//===============================
//============ blink ============
//===============================
//-------------------------------
//------------ blink ------------
//-------------------------------
//function triggers certain count and interval of off durations
void handledDisplay::blink(uint8_t count_f, uint32_t msOn_f, uint32_t msOff_f, const char * strOff_f) {
//copy/update parameters
@ -157,9 +156,9 @@ void handledDisplay::blink(uint8_t count_f, uint32_t msOn_f, uint32_t msOff_f, c
//================================
//============ handle ============
//================================
//--------------------------------
//------------ handle ------------
//--------------------------------
//function that handles time based modes
//writes text to the 7 segment display depending on the current mode
void handledDisplay::handle() {

View File

@ -16,8 +16,9 @@ extern "C"
#include <cstring>
#include "config.hpp"
//function for initializing the display using configuration from macros in config.h
//function for initializing the display using configuration from macros in config.hpp
max7219_t display_init();
//show welcome message on the entire display
@ -43,8 +44,8 @@ class handledDisplay {
//TODO: blinkStrings and blink are very similar - optimize?
//TODO: add 'scroll string' method
private:
//--- variables ---
//config
max7219_t dev;

View File

@ -8,8 +8,6 @@ extern "C" {
}
#include "encoder.hpp"
#include "config.h"
#include "global.hpp"
//----------------------------
@ -19,14 +17,10 @@ static rotary_encoder_info_t encoder; //encoder device/info
QueueHandle_t encoder_queue = NULL; //encoder event queue
//-------------------------
//------- functions -------
//-------------------------
//======================
//==== encoder_init ====
//======================
//--- encoder_init ---
//initialize encoder and return event queue
QueueHandle_t encoder_init(){
// esp32-rotary-encoder requires that the GPIO ISR service is installed before calling rotary_encoder_register()
@ -47,9 +41,7 @@ QueueHandle_t encoder_init(){
}
//========================
//=== encoder_getSteps ===
//========================
//--- encoder_getSteps ---
//get steps counted since last reset
int encoder_getSteps(){
// Poll current position and direction
@ -60,18 +52,14 @@ int encoder_getSteps(){
}
//========================
//=== encoder_getLenMm ===
//========================
//--- encoder_getLenMm ---
//get current length in Mm since last reset
int encoder_getLenMm(){
return (float)encoder_getSteps() * 1000 / ENCODER_STEPS_PER_METER;
}
//=======================
//==== encoder_reset ====
//=======================
//--- encoder_reset ---
//reset counted steps / length to 0
void encoder_reset(){
rotary_encoder_reset(&encoder);

View File

@ -6,6 +6,7 @@ extern "C" {
#include <freertos/task.h>
}
#include "config.hpp"
//----------------------------
@ -33,6 +34,7 @@ int encoder_getSteps();
int encoder_getLenMm();
//--- encoder_reset ---
//reset counted steps / length to 0
void encoder_reset();

View File

@ -1,33 +0,0 @@
#pragma once
extern "C" {
#include "driver/adc.h"
}
#include "gpio_evaluateSwitch.hpp"
#include "buzzer.hpp"
#include "switchesAnalog.hpp"
//note: in the actual code macro variables to these objects from config.h are used as the objects names
//============================
//===== global variables =====
//============================
//create global evaluated switch objects for all available pins
//--- switches on digital gpio pins ---
//extern gpio_evaluatedSwitch sw_gpio_39;
extern gpio_evaluatedSwitch sw_gpio_34;
extern gpio_evaluatedSwitch sw_gpio_32;
extern gpio_evaluatedSwitch sw_gpio_33;
extern gpio_evaluatedSwitch sw_gpio_25;
extern gpio_evaluatedSwitch sw_gpio_26;
extern gpio_evaluatedSwitch sw_gpio_14;
//--- switches connected to 4-sw-to-analog stripboard ---
extern gpio_evaluatedSwitch sw_gpio_analog_0;
extern gpio_evaluatedSwitch sw_gpio_analog_1;
extern gpio_evaluatedSwitch sw_gpio_analog_2;
extern gpio_evaluatedSwitch sw_gpio_analog_3;
//create global buzzer object
extern buzzer_t buzzer;

View File

@ -5,276 +5,170 @@ extern "C"
#include "freertos/task.h"
#include "esp_log.h"
#include "driver/adc.h"
#include "freertos/queue.h"
#include "freertos/semphr.h"
}
#include "stepper.hpp"
#include "config.h"
#include "global.hpp"
#include "config.hpp"
#include "guide-stepper.hpp"
#include "encoder.hpp"
#include "shutdown.hpp"
//macro to get smaller value out of two
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
//---------------------
//--- configuration ---
//---------------------
//also see config.h
//for pin definitions and guide parameters
//also see config.hpp
//for pin definition
// configure testing modes
#define STEPPER_TEST_TRAVEL 65 // mm
// speeds for testing with potentiometer (test task only)
#define SPEED_MIN 2.0 // mm/s
#define SPEED_MAX 70.0 // mm/s
//note: actual speed is currently defined in config.h with STEPPER_SPEED_DEFAULT
//simulate encoder with reset button to test stepper ctl task
//note STEPPER_TEST has to be defined as well
//#define STEPPER_SIMULATE_ENCODER
#define STEPPER_TEST_TRAVEL 65 //mm
//
#define MIN_MM 0
#define MAX_MM 60
#define POS_MAX_STEPS MAX_MM * STEPPER_STEPS_PER_MM
#define POS_MIN_STEPS MIN_MM * STEPPER_STEPS_PER_MM
#define SPEED_MIN 2.0 //mm/s
#define SPEED_MAX 60.0 //mm/s
#define ACCEL_MS 100.0 //ms from 0 to max
#define DECEL_MS 90.0 //ms from max to 0
#define STEPPER_STEPS_PER_ROT 1600
#define STEPPER_STEPS_PER_MM STEPPER_STEPS_PER_ROT/4
#define D_CABLE 6
#define D_REEL 160 //actual 170
#define PI 3.14159
//#define POS_MAX_STEPS GUIDE_MAX_MM * STEPPER_STEPS_PER_MM //note replaced with posMaxSteps
#define POS_MIN_STEPS GUIDE_MIN_MM * STEPPER_STEPS_PER_MM
//----------------------
//----- variables ------
//----------------------
typedef enum axisDirection_t {AXIS_MOVING_LEFT = 0, AXIS_MOVING_RIGHT} axisDirection_t;
static const char *TAG = "stepper"; //tag for logging
static const char *TAG = "stepper-ctrl"; //tag for logging
static axisDirection_t currentAxisDirection = AXIS_MOVING_RIGHT;
static bool stepp_direction = true;
static uint32_t posNow = 0;
static int layerCount = 0;
// queue for sending commands to task handling guide movement
static QueueHandle_t queue_commandsGuideTask;
// mutex to prevent multiple axis to config variables also accessed/modified by control task
SemaphoreHandle_t configVariables_mutex = xSemaphoreCreateMutex();
// configured winding width: position the axis returns again in steps
static uint32_t posMaxSteps = GUIDE_MAX_MM * STEPPER_STEPS_PER_MM; //assign default width
//----------------------
//----- functions ------
//----------------------
//=============================
//=== guide_getAxisPosSteps ===
//=============================
// return local variable posNow
// needed at shutdown detection to store last axis position in nvs
int guide_getAxisPosSteps(){
return posNow;
}
////move axis certain Steps (relative) between left and right or reverse when negative
//void travelSteps(int stepsTarget){
// //posNow = step.getPositionMm(); //not otherwise controlled, so no update necessary
// int stepsToGo, remaining;
//
// stepsToGo = abs(stepsTarget);
// if(stepsTarget < 0) stepp_direction = !stepp_direction; //invert direction in reverse mode
//
// while (stepsToGo != 0){
// //--- currently moving right ---
// if (stepp_direction == true){ //currently moving right
// remaining = POS_MAX_STEPS - posNow; //calc remaining distance fom current position to limit
// if (stepsToGo > remaining){ //new distance will exceed limit
// //....step.runAbs (POS_MAX_STEPS); //move to limit
// //....while(step.getState() != 1) vTaskDelay(1); //wait for move to finish
// posNow = POS_MAX_STEPS;
// stepp_direction = false; //change current direction for next iteration
// stepsToGo = stepsToGo - remaining; //decrease target length by already traveled distance
// ESP_LOGI(TAG, " --- moved to max -> change direction (L) --- \n ");
// }
// else { //target distance does not reach the limit
// //....step.runAbs (posNow + stepsToGo); //move by (remaining) distance to reach target length
// //....while(step.getState() != 1) vTaskDelay(1); //wait for move to finish
// ESP_LOGD(TAG, "moving to %d\n", posNow+stepsToGo);
// posNow += stepsToGo;
// stepsToGo = 0; //finished, reset target length (could as well exit loop/break)
// }
// }
//
// //--- currently moving left ---
// else {
// remaining = posNow - POS_MIN_STEPS;
// if (stepsToGo > remaining){
// //....step.runAbs (POS_MIN_STEPS);
// //....while(step.getState() != 1) vTaskDelay(2); //wait for move to finish
// posNow = POS_MIN_STEPS;
// stepp_direction = true;
// stepsToGo = stepsToGo - remaining;
// ESP_LOGI(TAG, " --- moved to min -> change direction (R) --- \n ");
// }
// else {
// //....step.runAbs (posNow - stepsToGo); //when moving left the coordinate has to be decreased
// while(step.getState() != 1) vTaskDelay(2); //wait for move to finish
// ESP_LOGD(TAG, "moving to %d\n", posNow - stepsToGo);
// posNow -= stepsToGo;
// stepsToGo = 0;
// }
// }
// }
// if(stepsTarget < 0) stepp_direction = !stepp_direction; //undo inversion of stepp_direction after reverse mode is finished
// return;
//}
//
//
////move axis certain Mm (relative) between left and right or reverse when negative
//void travelMm(int length){
// travelSteps(length * STEPPER_STEPS_PER_MM);
//}
//
//
////define zero/start position
////currently crashes into hardware limitation for certain time
////TODO: limit switch
//void home() {
// ESP_LOGW(TAG, "auto-home...");
// //....step.setSpeedMm(100, 500, 10);
// //....step.runInf(1);
// vTaskDelay(1500 / portTICK_PERIOD_MS);
// //....step.stop();
// //....step.resetAbsolute();
// ESP_LOGW(TAG, "auto-home finished");
//}
//=============================
//=== guide_setWindingWidth ===
//=============================
// set custom winding width (axis position the guide returns in mm)
void guide_setWindingWidth(uint8_t maxPosMm)
{
if (xSemaphoreTake(configVariables_mutex, portMAX_DELAY) == pdTRUE) // mutex to prevent multiple access by control and stepper-ctl task
{
posMaxSteps = maxPosMm * STEPPER_STEPS_PER_MM;
ESP_LOGI(TAG, "set winding width / max pos to %dmm", maxPosMm);
xSemaphoreGive(configVariables_mutex);
}
}
//=======================================
//=== guide_targetLength2WindingWidth ===
//=======================================
// calculate dynamic winding width in mm from cable length in mm
uint8_t guide_targetLength2WindingWidth(int lenMm)
{
#ifdef DYNAMIC_WINDING_WIDTH_ENABLED
uint8_t width;
//--- config ---
// define thresholds for winding widths according to target length:
if (lenMm <= 5000) // 0-5m
width = 15;
else if (lenMm <= 10000) // 6-10m
width = 25;
else if (lenMm <= 15000) // 11-15m
width = 30;
else if (lenMm <= 25000) // 16-25m
width = 65;
else // >25m
width = GUIDE_MAX_MM;
ESP_LOGW(TAG, "length2width: calculated windingWidth=%dmm from targetLength=%dm", width, lenMm);
return width;
#else
ESP_LOGD(TAG, "length2width: dynamic windingWidh not enabled, stay at GUIDE_MAX=%d", GUIDE_MAX_MM);
return GUIDE_MAX_MM;
#endif
//TODO update winding width here as well already?
}
//=============================
//=== guide_getWindingWidth ===
//=============================
// get currently configured winding width (axis position at which the guide returns in mm)
uint8_t guide_getWindingWidth()
{
if (xSemaphoreTake(configVariables_mutex, portMAX_DELAY) == pdTRUE) // mutex to prevent multiple access by control and stepper-ctl task
{
uint8_t returnValue = posMaxSteps / STEPPER_STEPS_PER_MM;
xSemaphoreGive(configVariables_mutex);
return returnValue;
}
return 0;
}
//==========================
//==== guide_moveToZero ====
//==========================
//tell stepper-control task to move cable guide to zero position
void guide_moveToZero(){
bool valueToSend = true; // or false
xQueueSend(queue_commandsGuideTask, &valueToSend, portMAX_DELAY);
ESP_LOGI(TAG, "sending command to stepper_ctl task via queue");
}
//---------------------
//---- travelSteps ----
//---------------------
//move axis certain Steps (relative) between left and right or reverse when negative
void travelSteps(int stepsTarget){
//TODO simplify this function, one simple calculation of new position?
//with new custom driver no need to detect direction change
// cancel when width is zero or no steps received
if (posMaxSteps == 0 || stepsTarget == 0){
ESP_LOGD(TAG, "travelSteps: MaxSteps or stepsTarget = 0 -> nothing to do");
return;
}
int stepsToGo, remaining;
stepsToGo = abs(stepsTarget);
// invert direction in reverse mode (cable gets spooled off reel)
if (stepsTarget < 0) {
currentAxisDirection = (currentAxisDirection == AXIS_MOVING_LEFT) ? AXIS_MOVING_RIGHT : AXIS_MOVING_LEFT; //toggle between RIGHT<->Left
}
while (stepsToGo != 0){
//--- currently moving right ---
if (currentAxisDirection == AXIS_MOVING_RIGHT){ //currently moving right
if (xSemaphoreTake(configVariables_mutex, portMAX_DELAY) == pdTRUE) { //prevent multiple acces on posMaxSteps by control-task
remaining = posMaxSteps - posNow; //calc remaining distance fom current position to limit
if (stepsToGo > remaining){ //new distance will exceed limit
stepper_setTargetPosSteps(posMaxSteps); //move to limit
stepper_waitForStop(1000);
posNow = posMaxSteps;
currentAxisDirection = AXIS_MOVING_LEFT; //change current direction for next iteration
//increment/decrement layer count depending on current cable direction
layerCount += (stepsTarget > 0) - (stepsTarget < 0);
if (layerCount < 0) layerCount = 0; //negative layers are not possible
stepsToGo = stepsToGo - remaining; //decrease target length by already traveled distance
ESP_LOGI(TAG, " --- moved to max -> change direction (L) --- \n ");
}
else { //target distance does not reach the limit
stepper_setTargetPosSteps(posNow + stepsToGo); //move by (remaining) distance to reach target length
ESP_LOGD(TAG, "moving to %d\n", posNow+stepsToGo);
posNow += stepsToGo;
stepsToGo = 0; //finished, reset target length (could as well exit loop/break)
}
xSemaphoreGive(configVariables_mutex);
}
}
//--- currently moving left ---
else if (currentAxisDirection == AXIS_MOVING_LEFT){
remaining = posNow - POS_MIN_STEPS;
if (stepsToGo > remaining){
stepper_setTargetPosSteps(POS_MIN_STEPS);
stepper_waitForStop(1000);
posNow = POS_MIN_STEPS;
currentAxisDirection = AXIS_MOVING_RIGHT; //switch direction
//increment/decrement layer count depending on current cable direction
layerCount += (stepsTarget > 0) - (stepsTarget < 0);
if (layerCount < 0) layerCount = 0; //negative layers are not possible
stepsToGo = stepsToGo - remaining;
ESP_LOGI(TAG, " --- moved to min -> change direction (R) --- \n ");
}
else {
stepper_setTargetPosSteps(posNow - stepsToGo); //when moving left the coordinate has to be decreased
ESP_LOGD(TAG, "moving to %d\n", posNow - stepsToGo);
posNow -= stepsToGo;
stepsToGo = 0;
}
}
}
// undo inversion of currentAxisDirection after reverse mode is finished
if (stepsTarget < 0) {
currentAxisDirection = (currentAxisDirection == AXIS_MOVING_LEFT) ? AXIS_MOVING_RIGHT : AXIS_MOVING_LEFT; //toggle between RIGHT<->Left
}
return;
}
//------------------
//---- travelMm ----
//------------------
//move axis certain Mm (relative) between left and right or reverse when negative
void travelMm(int length){
travelSteps(length * STEPPER_STEPS_PER_MM);
}
//----------------------
//---- init_stepper ----
//----------------------
//initialize/configure stepper instance
void init_stepper() {
//TODO unnecessary wrapper?
ESP_LOGW(TAG, "initializing stepper...");
// ESP_LOGW(TAG, "initializing stepper...");
// DendoStepper_config_t step_cfg = {
// .stepPin = STEPPER_STEP_PIN,
// .dirPin = STEPPER_DIR_PIN,
// .enPin = STEPPER_EN_PIN,
// .timer_group = TIMER_GROUP_0,
// .timer_idx = TIMER_0,
// .miStep = MICROSTEP_32,
// .stepAngle = 1.8};
// //....step.config(&step_cfg);
// //....step.init();
//
// //....step.setSpeed(1000, 1000, 1000); //random default speed
// //....step.setStepsPerMm(STEPPER_STEPS_PER_MM); //guide: 4mm/rot
stepper_init();
// create queue for sending commands to task handling guide movement
// currently length 1 and only one command possible, thus bool
queue_commandsGuideTask = xQueueCreate(1, sizeof(bool));
}
//--------------------------
//--- updateSpeedFromAdc ---
//--------------------------
//function that updates speed value using ADC input and configured MIN/MAX - used for testing only
//function that updates speed value using ADC input and configured MIN/MAX
void updateSpeedFromAdc() {
int potiRead = gpio_readAdc(ADC_CHANNEL_POTI); //0-4095 GPIO34
double poti = potiRead/4095.0;
int speed = poti*(SPEED_MAX-SPEED_MIN) + SPEED_MIN;
stepper_setSpeed(speed);
//....step.setSpeedMm(speed, ACCEL_MS, DECEL_MS);
ESP_LOGW(TAG, "poti: %d (%.2lf%%), set speed to: %d", potiRead, poti*100, speed);
}
//============================
//==== TASK stepper_test =====
//============================
//test axis without using encoder input
#ifndef STEPPER_SIMULATE_ENCODER
//----------------------------
//---- TASK stepper-test -----
//----------------------------
void task_stepper_test(void *pvParameter)
{
stepper_init();
int state = 0;
while(1){
vTaskDelay(20 / portTICK_PERIOD_MS);
@ -289,8 +183,6 @@ void task_stepper_test(void *pvParameter)
SW_CUT.handle();
SW_AUTO_CUT.handle();
#ifdef ONE_BUTTON_TEST //test with "reset-button" only
static int state = 0;
//cycle through test commands with one button
if (SW_RESET.risingEdge) {
switch (state){
@ -316,152 +208,80 @@ void task_stepper_test(void *pvParameter)
break;
}
}
#else //test with all buttons
if (SW_RESET.risingEdge) {
buzzer.beep(1, 500, 100);
stepper_setTargetPosMm(0);
}
if (SW_PRESET1.risingEdge) {
buzzer.beep(1, 200, 100);
stepper_setTargetPosMm(50);
}
if (SW_PRESET2.risingEdge) {
buzzer.beep(2, 200, 100);
stepper_setTargetPosMm(75);
}
if (SW_PRESET3.risingEdge) {
buzzer.beep(3, 200, 100);
stepper_setTargetPosMm(100);
}
#endif
}
// if (SW_PRESET1.risingEdge) {
// buzzer.beep(2, 300, 100);
// stepperSw_setTargetSteps(1000);
// }
// if (SW_PRESET2.risingEdge) {
// buzzer.beep(1, 500, 100);
// stepperSw_setTargetSteps(10000);
// }
// if (SW_PRESET3.risingEdge) {
// buzzer.beep(1, 100, 100);
// stepperSw_setTargetSteps(30000);
// }
}
#endif //end SIMULATE_ENCODER
//============================
//===== TASK stepper_ctl =====
//============================
//task controlling the linear axis guiding the cable according to wire length spooled
#ifdef STEPPER_SIMULATE_ENCODER
void task_stepper_test(void *pvParameter)
#else
//----------------------------
//----- TASK stepper-ctl -----
//----------------------------
void task_stepper_ctl(void *pvParameter)
#endif
{
//-- variables --
static int encStepsPrev = 0; //measured encoder steps at last run
static double travelStepsPartial = 0; //store resulted remaining partial steps last run
//temporary variables for calculating required steps, or logging
int encStepsNow = 0; //get curretly measured steps of encoder
int encStepsDelta = 0; //encoder steps changed since last iteration
double cableLen = 0;
double travelStepsExact = 0; //steps axis has to travel
int travelStepsFull = 0;
double travelMm = 0;
double turns = 0;
float currentDiameter;
//initialize stepper at task start
init_stepper();
//define zero-position
// use last known position stored at last shutdown to reduce time crashing into hardware limit
int posLastShutdown = nvsReadLastAxisPosSteps();
if (posLastShutdown >= 0)
{ // would be -1 when failed
ESP_LOGW(TAG, "auto-home: considerting pos last shutdown %dmm + tolerance %dmm",
posLastShutdown / STEPPER_STEPS_PER_MM,
AUTO_HOME_TRAVEL_ADD_TO_LAST_POS_MM);
// home considering last position and offset, but limit to max distance possible
stepper_home(MIN((posLastShutdown/STEPPER_STEPS_PER_MM + AUTO_HOME_TRAVEL_ADD_TO_LAST_POS_MM), MAX_TOTAL_AXIS_TRAVEL_MM));
}
else { // default to max travel when read from nvs failed
stepper_home(MAX_TOTAL_AXIS_TRAVEL_MM);
}
//repeatedly read changes in measured cable length and move axis accordingly
while(1){
// guide is disabled when posMaxSteps is zero:
if (posMaxSteps == 0)
{
// move to starting position
stepper_setTargetPosSteps(0);
vTaskDelay(1000 / portTICK_PERIOD_MS);
ESP_LOGD(TAG, "posMaxSteps is zero -> guide disabled, doing nothing");
// stop loop iteration
continue;
}
#ifdef STEPPER_SIMULATE_ENCODER
//TESTING - simulate encoder using switch
SW_RESET.handle();
//note
if (SW_RESET.risingEdge) encStepsNow += 500;
#else
//get current length
encStepsNow = encoder_getSteps();
#endif
// move to zero and reset if command received via queue
bool receivedValue;
if (xQueueReceive(queue_commandsGuideTask, &receivedValue, 0) == pdTRUE)
{
// Process the received value
// TODO support other commands (currently only move to zero possible)
ESP_LOGW(TAG, "task: move-to-zero command received via queue, starting move, waiting until position reached");
stepper_setTargetPosMm(0);
stepper_waitForStop();
//reset variables -> start tracking cable movement starting from position zero
// ensure stepsDelta is 0
encStepsNow = encoder_getSteps();
encStepsPrev = encStepsNow;
travelStepsPartial = 0;
// set locally stored axis position and counted layers to 0 (used for calculating the target axis coordinate and steps)
posNow = 0;
layerCount = 0;
currentAxisDirection = AXIS_MOVING_RIGHT;
ESP_LOGW(TAG, "at position 0, reset variables, resuming normal cable guiding operation");
}
//calculate change
encStepsDelta = encStepsNow - encStepsPrev;
//check if reset happend without moving to zero before - resulting in huge diff
if (encStepsDelta != 0 && encStepsNow == 0){ // this should not happen and causes weird movement
ESP_LOGE(TAG, "encoder steps changed to 0 (reset) without previous moveToZero() call, resulting in stepsDelta=%d", encStepsDelta);
}
//read potentiometer and normalize (0-1) to get a variable for testing
//float potiModifier = (float) gpio_readAdc(ADC_CHANNEL_POTI) / 4095; //0-4095 -> 0-1
//ESP_LOGI(TAG, "current poti-modifier = %f", potiModifier);
//calculate steps to move
cableLen = (double)encStepsDelta * 1000 / ENCODER_STEPS_PER_METER;
//effective diameter increases each layer
currentDiameter = D_REEL + LAYER_THICKNESS_MM * 2 * layerCount;
turns = cableLen / (PI * currentDiameter);
travelMm = turns * D_CABLE;
travelStepsExact = travelMm * STEPPER_STEPS_PER_MM + travelStepsPartial; //convert mm to steps and add not moved partial steps
travelStepsPartial = 0;
travelStepsFull = (int)travelStepsExact;
//move axis when ready to move at least 1 full step
if (abs(travelStepsFull) > 1){
travelStepsPartial = fmod(travelStepsExact, 1); //save remaining partial steps to be added in the next iteration
ESP_LOGI(TAG, "dCablelen=%.2lf, dTurns=%.2lf, travelMm=%.3lf, StepsExact: %.3lf, StepsFull=%d, StepsPartial=%.3lf, totalLayerCount=%d, diameter=%.1f", cableLen, turns, travelMm, travelStepsExact, travelStepsFull, travelStepsPartial, layerCount, currentDiameter);
ESP_LOGD(TAG, "MOVING %d steps", travelStepsFull);
travelSteps(travelStepsExact);
encStepsPrev = encStepsNow; //update previous length
}
else {
//TODO use encoder queue to only run this check at encoder event?
vTaskDelay(5);
}
vTaskDelay(5 / portTICK_PERIOD_MS);
}
// //variables
// int encStepsNow = 0; //get curret steps of encoder
// int encStepsPrev = 0; //steps at last check
// int encStepsDelta = 0; //steps changed since last iteration
//
// double cableLen = 0;
// double travelStepsExact = 0; //steps axis has to travel
// double travelStepsPartial = 0;
// int travelStepsFull = 0;
// double travelMm = 0;
// double turns = 0;
//
// float potiModifier;
//
// init_stepper();
// home();
//
// while(1){
// //get current length
// encStepsNow = encoder_getSteps();
//
// //calculate change
// encStepsDelta = encStepsNow - encStepsPrev; //FIXME MAJOR BUG: when resetting encoder/length in control task, diff will be huge!
//
// //read potentiometer and normalize (0-1) to get a variable for testing
// potiModifier = (float) gpio_readAdc(ADC_CHANNEL_POTI) / 4095; //0-4095 -> 0-1
// //ESP_LOGI(TAG, "current poti-modifier = %f", potiModifier);
//
// //calculate steps to move
// cableLen = (double)encStepsDelta * 1000 / ENCODER_STEPS_PER_METER;
// turns = cableLen / (PI * D_REEL);
// travelMm = turns * D_CABLE;
// travelStepsExact = travelMm * STEPPER_STEPS_PER_MM + travelStepsPartial; //convert mm to steps and add not moved partial steps
// travelStepsPartial = 0;
// travelStepsFull = (int)travelStepsExact;
//
// //move axis when ready to move at least 1 step
// if (abs(travelStepsFull) > 1){
// travelStepsPartial = fmod(travelStepsExact, 1); //save remaining partial steps to be added in the next iteration
// ESP_LOGD(TAG, "cablelen=%.2lf, turns=%.2lf, travelMm=%.3lf, travelStepsExact: %.3lf, travelStepsFull=%d, partialStep=%.3lf", cableLen, turns, travelMm, travelStepsExact, travelStepsFull, travelStepsPartial);
// ESP_LOGI(TAG, "MOVING %d steps", travelStepsFull);
// //TODO: calculate variable speed for smoother movement? for example intentionally lag behind and calculate speed according to buffered data
// //....step.setSpeedMm(35, 100, 50);
// //testing: get speed from poti
// //step.setSpeedMm(35, 1000*potiModifier+1, 1000*potiModifier+1);
// travelSteps(travelStepsExact);
// encStepsPrev = encStepsNow; //update previous length
// }
// else {
// //TODO use encoder queue to only run this check at encoder event?
// vTaskDelay(2);
// }
// }
}

View File

@ -10,21 +10,3 @@ void task_stepper_test(void *pvParameter);
//task that initializes and controls the stepper motor
// - moves stepper according to encoder movement
void task_stepper_ctl(void *pvParameter);
//tell stepper-control task to move cable guide to zero position
void guide_moveToZero();
// return local variable posNow that stores the current position of cable guide axis in steps
// needed by shutdown to store last axis position in nvs
int guide_getAxisPosSteps();
// set custom winding width (axis position the guide returns in mm)
void guide_setWindingWidth(uint8_t maxPosMm);
// get currently configured winding width (axis position the guide returns in mm)
uint8_t guide_getWindingWidth();
// calculate dynamic winding width in mm from cable length in mm according to fixed thresholds
uint8_t guide_targetLength2WindingWidth(int lenMm);

View File

@ -8,17 +8,14 @@ extern "C"
#include "esp_system.h"
#include "esp_log.h"
#include "driver/adc.h"
}
#include "config.h"
#include "global.hpp"
#include "config.hpp"
#include "control.hpp"
#include "buzzer.hpp"
#include "switchesAnalog.hpp"
#include "guide-stepper.hpp"
#include "encoder.hpp"
#include "shutdown.hpp"
#include "stepper.hpp"
@ -26,20 +23,15 @@ extern "C"
//=================================
//=========== functions ===========
//=================================
//------------------------
//--- configure output ---
//------------------------
//configure a gpio pin as output
//function to configure gpio pin as output
void gpio_configure_output(gpio_num_t gpio_pin){
gpio_pad_select_gpio(gpio_pin);
gpio_set_direction(gpio_pin, GPIO_MODE_OUTPUT);
}
//--------------------
//---- init gpios ----
//--------------------
//--- init gpios ---
void init_gpios(){
//--- outputs ---
//4x stepper mosfets
@ -61,8 +53,6 @@ void init_gpios(){
//initialize and configure ADC
adc1_config_width(ADC_WIDTH_BIT_12); //=> max resolution 4096
adc1_config_channel_atten(ADC_CHANNEL_POTI, ADC_ATTEN_DB_11); //max voltage
adc1_config_channel_atten(ADC_CHANNEL_SUPPLY_VOLTAGE, ADC_ATTEN_DB_11); //max voltage
}
@ -84,40 +74,33 @@ void task_buzzer( void * pvParameters ){
//======================================
extern "C" void app_main()
{
//init outputs and adc
//init outputs
init_gpios();
//enable 5V volage regulator (needed for display)
//enable 5V volate regulator
gpio_set_level(GPIO_NUM_17, 1);
//init encoder (global)
encoder_queue = encoder_init();
//define loglevel
esp_log_level_set("*", ESP_LOG_INFO); //default loglevel
esp_log_level_set("*", ESP_LOG_INFO);
esp_log_level_set("buzzer", ESP_LOG_ERROR);
esp_log_level_set("switches-analog", ESP_LOG_WARN);
esp_log_level_set("control", ESP_LOG_INFO);
esp_log_level_set("stepper-driver", ESP_LOG_WARN);
esp_log_level_set("stepper-ctrl", ESP_LOG_WARN);
esp_log_level_set("stepper", ESP_LOG_DEBUG);
esp_log_level_set("Dendostepper", ESP_LOG_WARN); //stepper lib
esp_log_level_set("calc", ESP_LOG_WARN); //stepper lib
esp_log_level_set("lowVoltage", ESP_LOG_INFO);
#ifdef STEPPER_TEST
//create task for testing the stepper motor
//create task for stepper testing
xTaskCreate(task_stepper_test, "task_stepper_test", configMINIMAL_STACK_SIZE * 3, NULL, 2, NULL);
//xTaskCreate(task_stepper_debug, "task_stepper_test", configMINIMAL_STACK_SIZE * 3, NULL, 2, NULL);
#else
//create task for detecting power-off
xTaskCreate(&task_shutDownDetection, "task_shutdownDet", 2048, NULL, 2, NULL);
// wait for nvs to be initialized in shutDownDetection task
vTaskDelay(50 / portTICK_PERIOD_MS);
//create task for controlling the machine
xTaskCreate(task_control, "task_control", configMINIMAL_STACK_SIZE * 3, NULL, 4, NULL);
//create task for controlling the stepper motor (linear axis that guids the cable)
//create task for controlling the stepper
xTaskCreate(task_stepper_ctl, "task_stepper_ctl", configMINIMAL_STACK_SIZE * 3, NULL, 2, NULL);
#endif

View File

@ -1,120 +0,0 @@
extern "C"
{
#include <stdio.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include "esp_system.h"
#include "esp_log.h"
#include "driver/adc.h"
#include "nvs_flash.h"
#include "nvs.h"
}
#include "config.h"
#include "shutdown.hpp"
#include "guide-stepper.hpp"
#define ADC_LOW_VOLTAGE_THRESHOLD 3200 // adc value where shut down is detected (store certain values before complete power loss)
static const char *TAG = "lowVoltage"; // tag for logging
nvs_handle_t nvsHandle; // access nvs that was opened once, in any function
// store a u32 value in nvs as "lastPosSteps"
void nvsWriteLastAxisPos(uint32_t value)
{
// update nvs value
esp_err_t err = nvs_set_u32(nvsHandle, "lastPosSteps", value);
if (err != ESP_OK)
ESP_LOGE(TAG, "nvs: failed writing");
err = nvs_commit(nvsHandle);
if (err != ESP_OK)
ESP_LOGE(TAG, "nvs: failed committing updates");
else
ESP_LOGI(TAG, "nvs: successfully committed updates");
ESP_LOGW(TAG, "updated value in nvs to %d", value);
}
// read "lastPosSteps" from nvs, returns -1 if failed
int nvsReadLastAxisPosSteps()
{
uint32_t valueRead;
esp_err_t err = nvs_get_u32(nvsHandle, "lastPosSteps", &valueRead);
switch (err)
{
case ESP_OK:
ESP_LOGW(TAG, "Successfully read value %d from nvs", valueRead);
return valueRead;
break;
case ESP_ERR_NVS_NOT_FOUND:
ESP_LOGE(TAG, "nvs: the value '%s' is not initialized yet", "lastPosSteps");
return -1;
break;
default:
ESP_LOGE(TAG, "Error (%s) reading nvs!", esp_err_to_name(err));
return -1;
}
}
// task that repeatedly checks supply voltage (12V) and saves certain values to nvs in case of power off detected
// note: with the 2200uF capacitor in the 12V supply and measuring if 12V start do drop here, there is more than enough time to take action until the 3v3 regulator turns off
void task_shutDownDetection(void *pvParameter)
{
//--- Initialize NVS ---
ESP_LOGW(TAG, "initializing nvs...");
esp_err_t err = nvs_flash_init();
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND)
{
ESP_LOGE(TAG, "NVS truncated -> deleting flash");
// Retry nvs_flash_init
ESP_ERROR_CHECK(nvs_flash_erase());
err = nvs_flash_init();
}
//--- open nvs-flash ---
ESP_LOGW(TAG, "opening NVS-handle...");
// create handle available for all functions in this file
err = nvs_open("storage", NVS_READWRITE, &nvsHandle);
if (err != ESP_OK)
ESP_LOGE(TAG, "Error (%s) opening NVS handle!\n", esp_err_to_name(err));
// read stored value (returns 0 if unitialized/failed)
//int lastPosSteps = nvsReadLastAxisPosSteps();
//ESP_LOGW(TAG, "=> read value %d from nvs (stored at last shutdown)", lastPosSteps);
// repeatedly read ADC and check if below low voltage threshold
bool voltageBelowThreshold = false;
while (1) //TODO limit save frequency in case voltage repeadedly varys between threshold for some reason (e.g. offset drift)
{
// read adc
int adc_reading = adc1_get_raw(ADC_CHANNEL_SUPPLY_VOLTAGE);
// evaulate threshold
if (adc_reading < ADC_LOW_VOLTAGE_THRESHOLD) // below threshold => POWER SHUTDOWN DETECTED
{
// write to nvs and log once at change to below
if (!voltageBelowThreshold){
nvsWriteLastAxisPos(guide_getAxisPosSteps());
ESP_LOGE(TAG, "voltage now below threshold! now=%d threshold=%d -> wrote last axis-pos to nvs", adc_reading, ADC_LOW_VOLTAGE_THRESHOLD);
voltageBelowThreshold = true;
}
}
else if (voltageBelowThreshold) // above threshold and previously below
{
// log at change to above
ESP_LOGE(TAG, "voltage above threshold again: %d > %d - issue with power supply, or too threshold too high?", adc_reading, ADC_LOW_VOLTAGE_THRESHOLD);
voltageBelowThreshold = false;
}
// always log for debugging/calibrating
ESP_LOGD(TAG, "read adc battery voltage: %d", adc_reading);
vTaskDelay(30 / portTICK_PERIOD_MS);
}
}

View File

@ -1,7 +0,0 @@
// task that repeatedly checks supply voltage (12V) and saves certain values to nvs in case of it drops below a certain threshold (power off detected)
void task_shutDownDetection(void *pvParameter);
// read last axis position in steps from nvs
// returns -1 when reading from nvs failed
int nvsReadLastAxisPosSteps();

View File

@ -1,6 +1,5 @@
//custom driver for stepper motor
#include "config.h"
#include "global.hpp"
#include "config.hpp"
#include "hal/timer_types.h"
#include <cstdint>
#include <inttypes.h>
@ -11,19 +10,16 @@ extern "C" {
#include "esp_log.h"
}
//=====================
//=== configuration ===
//=====================
//used macros from config.h:
//config from config.hpp
//#define STEPPER_STEP_PIN GPIO_NUM_18 //mos1
//#define STEPPER_DIR_PIN GPIO_NUM_16 //ST3
//#define STEPPER_STEPS_PER_MM 200/2 //steps/mm (steps-per-rot / slope)
//#define STEPPER_SPEED_DEFAULT 20 //mm/s
//#define STEPPER_SPEED_MIN 4 //mm/s - speed threshold at which stepper immediately starts/stops
//#define STEPPER_ACCEL_INC 3 //steps/s increment per cycle
//#define STEPPER_DECEL_INC 8 //steps/s decrement per cycle
#define STEPPER_STEPS_PER_MM 200/2 //steps/mm
#define STEPPER_SPEED_DEFAULT 20 //mm/s
#define STEPPER_SPEED_MIN 4 //mm/s - speed at which stepper immediately starts/stops
#define STEPPER_ACCEL_INC 3 //steps/s per cycle
#define STEPPER_DECEL_INC 8 //steps/s per cycle
#define TIMER_F 1000000ULL
#define TICK_PER_S TIMER_S
@ -34,7 +30,7 @@ extern "C" {
//========================
//=== global variables ===
//========================
static const char *TAG = "stepper-driver"; //tag for logging
static const char *TAG = "stepper-ctl"; //tag for logging
bool direction = 1;
bool directionTarget = 1;
@ -95,7 +91,7 @@ void task_stepper_debug(void *pvParameter){
//===== set speed =====
//=====================
void stepper_setSpeed(uint32_t speedMmPerS) {
ESP_LOGI(TAG, "set target speed from %u to %u mm/s (%u steps/s)",
ESP_LOGW(TAG, "set target speed from %u to %u mm/s (%u steps/s)",
speedTarget, speedMmPerS, speedMmPerS * STEPPER_STEPS_PER_MM);
speedTarget = speedMmPerS * STEPPER_STEPS_PER_MM;
}
@ -106,14 +102,14 @@ void stepper_setSpeed(uint32_t speedMmPerS) {
//== set target pos STEPS ==
//==========================
void stepper_setTargetPosSteps(uint64_t target_steps) {
ESP_LOGI(TAG, "update target position from %llu to %llu steps (stepsNow: %llu", posTarget, target_steps, posNow);
ESP_LOGW(TAG, "update target position from %llu to %llu steps (stepsNow: %llu", posTarget, target_steps, posNow);
posTarget = target_steps;
// Check if the timer is currently paused
if (!timerIsRunning){
// If the timer is paused, start it again with the updated targetSteps
timerIsRunning = true;
ESP_LOGI(TAG, "starting timer");
ESP_LOGW(TAG, "starting timer because did not run before");
ESP_ERROR_CHECK(timer_set_alarm_value(timerGroup, timerIdx, 1000));
//timer_set_counter_value(timerGroup, timerIdx, 1000);
ESP_ERROR_CHECK(timer_start(timerGroup, timerIdx));
@ -126,67 +122,24 @@ void stepper_setTargetPosSteps(uint64_t target_steps) {
//=== set target pos MM ===
//=========================
void stepper_setTargetPosMm(uint32_t posMm){
ESP_LOGI(TAG, "set target position to %u mm", posMm);
ESP_LOGW(TAG, "set target position to %u mm", posMm);
stepper_setTargetPosSteps(posMm * STEPPER_STEPS_PER_MM);
}
//=======================
//===== waitForStop =====
//=======================
//delay until stepper is stopped, optional timeout in ms, 0 = no limit
void stepper_waitForStop(uint32_t timeoutMs){
ESP_LOGI(TAG, "waiting for stepper to stop...");
uint32_t timestampStart = esp_log_timestamp();
while (timerIsRunning) {
if ( (esp_log_timestamp() - timestampStart) >= timeoutMs && timeoutMs != 0){
ESP_LOGE(TAG, "timeout waiting for stepper to stop");
return;
}
vTaskDelay(100 / portTICK_PERIOD_MS);
}
ESP_LOGI(TAG, "finished waiting stepper to stop");
return;
}
//======================
//======== home ========
//======================
//define zero/start position
//run to limit and define zero/start position.
//Currently simply runs stepper for travelMm and bumps into hardware limit
void stepper_home(uint32_t travelMm){
//TODO add timeout, limitswitch...
ESP_LOGW(TAG, "initiate auto-home, moving %d mm...", travelMm);
posNow = travelMm * STEPPER_STEPS_PER_MM;
while (posNow != 0){
//reactivate just in case stopped by other call to prevent deadlock
if (!timerIsRunning) {
stepper_setTargetPosSteps(0);
}
vTaskDelay(100 / portTICK_PERIOD_MS);
}
ESP_LOGW(TAG, "finished auto-home");
return;
}
//========================
//===== init stepper =====
//========================
void stepper_init(){
ESP_LOGI(TAG, "init - configure struct...");
ESP_LOGW(TAG, "init - configure struct...");
// Configure pulse and direction pins as outputs
ESP_LOGI(TAG, "init - configure gpio pins...");
ESP_LOGW(TAG, "init - configure gpio pins...");
gpio_set_direction(STEPPER_DIR_PIN, GPIO_MODE_OUTPUT);
gpio_set_direction(STEPPER_STEP_PIN, GPIO_MODE_OUTPUT);
ESP_LOGI(TAG, "init - initialize/configure timer...");
ESP_LOGW(TAG, "init - initialize/configure timer...");
timer_config_t timer_conf = {
.alarm_en = TIMER_ALARM_EN, // we need alarm
.counter_en = TIMER_PAUSE, // dont start now lol
@ -240,16 +193,8 @@ bool timer_isr(void *arg) {
//FIXME noticed crash: division by 0 when min speed > target speed
uint64_t stepsDecelRemaining = (speedNow - speedMin) / decel_increment;
//DECELERATE
//prevent hard stop (faster stop than decel ramp)
//Idea: when target gets lowered while decelerating,
// move further than target to not exceed decel ramp (overshoot),
// then change dir and move back to actual target pos
if (stepsToGo < stepsDecelRemaining/2){ //significantly less steps planned to comply with decel ramp
stepsToGo = stepsDecelRemaining; //set to required steps
}
if (stepsToGo <= stepsDecelRemaining) {
//FIXME if stepsToGo gets updated (lowered) close to target while close to target, the stepper may stop too fast -> implement possibility to 'overshoot and reverse'?
if ((speedNow - speedMin) > decel_increment) {
speedNow -= decel_increment;
} else {

View File

@ -2,24 +2,12 @@
//init stepper pins and timer
void stepper_init();
//delay until stepper is stopped, optional timeout in ms, 0 = no limit
void stepper_waitForStop(uint32_t timeoutMs = 0);
//run to limit and define zero/start position. (busy until finished)
//Currently simply runs stepper for travelMm and bumps into hardware limit
void stepper_home(uint32_t travelMm = 60);
//set absolute target position in steps
void stepper_setTargetPosSteps(uint64_t steps);
//set absolute target position in millimeters
void stepper_setTargetPosMm(uint32_t posMm);
//set target speed in millimeters per second
void stepper_setSpeed(uint32_t speedMmPerS);
//task that periodically logs variables for debugging stepper driver
void task_stepper_debug(void *pvParameter);

View File

@ -1,6 +1,4 @@
#include "switchesAnalog.hpp"
#include "config.h"
#include "global.hpp"
#define CHECK_BIT(var,pos) (((var)>>(pos)) & 1) //TODO duplicate code: same macro already used in vfd.cpp

View File

@ -8,6 +8,7 @@ extern "C"
#include <math.h>
}
#include "config.hpp"
#include "gpio_adc.hpp"

View File

@ -1,6 +1,4 @@
#include "vfd.hpp"
#include "config.h"
#include "global.hpp"
#define CHECK_BIT(var,pos) (((var)>>(pos)) & 1)

View File

@ -9,6 +9,7 @@ extern "C"
#include "esp_log.h"
}
#include "config.hpp"
//enum defining motor direction
typedef enum vfd_direction_t {FWD, REV} vfd_direction_t;

View File

@ -415,6 +415,7 @@ CONFIG_ESP_MAC_ADDR_UNIVERSE_ETH=y
# CONFIG_ESP32_UNIVERSAL_MAC_ADDRESSES_TWO is not set
CONFIG_ESP32_UNIVERSAL_MAC_ADDRESSES_FOUR=y
CONFIG_ESP32_UNIVERSAL_MAC_ADDRESSES=4
# CONFIG_ESP_MAC_IGNORE_MAC_CRC_ERROR is not set
# end of MAC Config
#