egoShield
egoShieldTeach.h
Go to the documentation of this file.
1 /********************************************************************************************
2 * File: egoShieldTeach.h *
3 * Version: 1.0.0 *
4 * Date: January 10th, 2018 *
5 * Author: Mogens Groth Nicolaisen *
6 * *
7 *********************************************************************************************
8 * (C) 2018 *
9 * *
10 * uStepper ApS *
11 * www.ustepper.com *
12 * administration@ustepper.com *
13 * *
14 * The code contained in this file is released under the following open source license: *
15 * *
16 * Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International *
17 * *
18 * The code in this file is provided without warranty of any kind - use at own risk! *
19 * neither uStepper ApS nor the author, can be held responsible for any damage *
20 * caused by the use of the code contained in this file ! *
21 * *
22 ********************************************************************************************/
105 #ifndef egoShield_h
106 #define egoShield_h
107 
108 #include "uStepper.h"
109 #include "U8g2lib.h"
110 #include "SPI.h"
111 #include "Arduino.h"
112 #include "SoftwareSerial.h"
113 
115 #define FWBT A3
116 
117 #define PLBT A1
118 
119 #define RECBT A2
120 
121 #define BWBT A0
122 
123 #define CNT 50
124 
125 #define OPTO 3
126 
128 #define FULL 1
129 
130 #define HALF 2
131 
132 #define QUARTER 4
133 
134 #define EIGHT 8
135 
136 #define SIXTEEN 16
137 
138 #define en_width 11
139 #define en_height 9
140 static unsigned char fw_bits[] = {
141  0x41, 0x00, 0xc3, 0x00, 0xc7, 0x01, 0xcf, 0x03, 0xdf, 0x07, 0xcf, 0x03,
142  0xc7, 0x01, 0xc3, 0x00, 0x41, 0x00 };
143 static unsigned char bw_bits[] = {
144  0x10, 0x04, 0x18, 0x06, 0x1c, 0x07, 0x9e, 0x07, 0xdf, 0x07, 0x9e, 0x07,
145  0x1c, 0x07, 0x18, 0x06, 0x10, 0x04 };
146 #define tt_width 10
147 #define tt_height 10
148 static unsigned char rec_bits[] = {
149  0xfc, 0x00, 0xfe, 0x01, 0xff, 0x03, 0xff, 0x03, 0xff, 0x03, 0xff, 0x03,
150  0xff, 0x03, 0xff, 0x03, 0xfe, 0x01, 0xfc, 0x00 };
151 static unsigned char stop_bits[] = {
152  0xff, 0x03, 0xff, 0x03, 0xff, 0x03, 0xff, 0x03, 0xff, 0x03, 0xff, 0x03,
153  0xff, 0x03, 0xff, 0x03, 0xff, 0x03, 0xff, 0x03 };
154 static unsigned char pse_bits[] = {
155  0xcc, 0x00, 0xcc, 0x00, 0xcc, 0x00, 0xcc, 0x00, 0xcc, 0x00, 0xcc, 0x00,
156  0xcc, 0x00, 0xcc, 0x00, 0xcc, 0x00, 0xcc, 0x00 };
157 #define play_width 6
158 #define play_height 11
159 static unsigned char play_bits[] = {
160  0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x01 };
161 #define loop_width 11
162 #define loop_height 10
163 static unsigned char loop_bits[] = {
164  0x04, 0x00, 0x08, 0x00, 0x9e, 0x03, 0x09, 0x04, 0x05, 0x04, 0x01, 0x05,
165  0x81, 0x04, 0xce, 0x03, 0x80, 0x00, 0x00, 0x01 };
166 #define logo_width 90
167 #define logo_height 16
168 static unsigned char logo_bits[] = {
169  0x00, 0x00, 0x7e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
170  0x00, 0x80, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
171  0x00, 0xc0, 0xff, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
172  0x00, 0xc0, 0xc7, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
173  0x3e, 0xcf, 0x07, 0xfc, 0xe3, 0xc3, 0x7b, 0x78, 0x0f, 0x3e, 0x9c, 0x01,
174  0xdf, 0xcf, 0x3f, 0xfe, 0xf3, 0xf7, 0xff, 0xfe, 0x1f, 0x7f, 0xff, 0x03,
175  0x3e, 0xcf, 0xff, 0x78, 0x38, 0xc7, 0xf3, 0x78, 0x9e, 0x73, 0xfe, 0x03,
176  0x3c, 0x8f, 0xff, 0x79, 0xfc, 0xcf, 0xf3, 0x79, 0xde, 0xff, 0xbc, 0x01,
177  0x1c, 0x47, 0xfe, 0x78, 0xfc, 0xcf, 0xf3, 0x78, 0xbe, 0x7f, 0x1e, 0x00,
178  0x3c, 0xef, 0xf0, 0x79, 0x38, 0xc0, 0xf3, 0x79, 0xde, 0x03, 0x1e, 0x00,
179  0xbe, 0xcf, 0xff, 0x78, 0x78, 0xc4, 0xf3, 0x78, 0x9e, 0x47, 0x3e, 0x00,
180  0xfc, 0xdf, 0x7f, 0xf8, 0xf3, 0xc7, 0x7f, 0xf8, 0x0f, 0x7f, 0x7e, 0x00,
181  0x38, 0x07, 0x3f, 0xf0, 0xe1, 0xc3, 0x3f, 0xf8, 0x07, 0x3e, 0x7f, 0x00,
182  0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x03, 0x7c, 0x00, 0x00, 0x00, 0x00,
183  0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x07, 0xfc, 0x00, 0x00, 0x00, 0x00,
184  0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x07, 0xf8, 0x00, 0x00, 0x00, 0x00 };
185 
186 
190 typedef struct {
191  uint8_t debounce,
192  state,
193  holdCnt,
194  btn;
195  uint16_t time;
196 } buttons;
197 
198 #define DEPRESSED 0
199 #define PRESSED 1
200 #define HOLD 2
202 #define HOLDTIME 75
203 #define HOLDTICK 5
206 #define RESETWDT asm volatile("WDR \n\t")
207 
209 extern "C" void WDT_vect(void) __attribute__ ((signal,used));
210 
222 {
223 public:
229  egoShield(void);
251  void egoShield::setup(uint16_t acc = 1500,
252  uint16_t vel = 1000,
253  uint8_t uStep = SIXTEEN,
254  uint16_t fTol = 10,
255  uint16_t fHys = 5,
256  float P = 1.0,
257  float I = 0.02,
258  float D = 0.006,
259  float res = 1);
263  void loop(void);
267  uStepper stepper;
268 private:
270  SoftwareSerial *BTSerial;
272  U8G2_SSD1306_128X64_NONAME_1_4W_SW_SPI *u8g2;
274  uint8_t place;
276  uint8_t endmove;
278  float pos[CNT];
280  bool pidFlag;
282  bool record;
284  bool loopMode;
286  bool longPushFlag[4];
288  char state;
290  uint8_t rec;
292  uint8_t play;
294  uint8_t fw;
296  uint8_t bw;
298  float setPoint;
300  uint16_t acceleration;
302  uint16_t velocity;
304  uint8_t microStepping;
306  uint16_t faultTolerance;
308  uint16_t faultHysteresis;
310  float pTerm;
312  float iTerm;
314  float dTerm;
316  float stepSize;
318  uint16_t interval;
320  float resolution;
322  bool brakeFlag;
323 
324  volatile buttons forwardBtn = {0x1F, DEPRESSED, 0, 0, 0},
325  playBtn = {0x1F, DEPRESSED, 0, 0, 0},
326  recordBtn = {0x1F, DEPRESSED, 0, 0, 0},
327  backwardsBtn = {0x1F, DEPRESSED, 0, 0, 0};
334  void resetButton(buttons *btn);
338  void resetAllButton();
342  void inputs(void);
355  uint8_t buttonState(uint8_t button, uint8_t nmbr);
359  void idleMode(void);
363  void playMode(void);
367  void recordMode(void);
371  void pauseMode(void);
375  void timeMode(void);
379  void manForward(void);
383  void manBackward(void);
387  void startPage(void);
395  void idlePage(bool pidMode, float pos);
407  void recordPage(bool pidMode, bool recorded, uint8_t index, float pos);
417  void playPage(bool loopMode, bool pidMode, uint8_t index, bool mode);
427  void pausePage(bool loopMode, bool pidMode, uint8_t index);
431  void changeVelocity(bool speedDirection = 1);
439  void debounce(buttons *btn, uint8_t sample);
440  friend void WDT_vect(void) __attribute__ ((signal,used));
441 };
442 #endif
uint8_t place
uint8_t endmove
void loop(void)
Contains the main logic of the shield functionality, e.g. transition between states (idle...
void playPage(bool loopMode, bool pidMode, uint8_t index, bool mode)
Holds the code for the play page of the OLED.
Watchdog timer interrupt handler, for examining the buttons periodically.
void recordMode(void)
Holds the record logic, showing the record page and recording positions from user input...
uStepper stepper
Creates an uStepper instance.
void changeVelocity(bool speedDirection=1)
Holds the code for the changing velocity during sequence play.
void idleMode(void)
Holds the idle logic; page to show, what buttons to enable etc.
uint8_t fw
void debounce(buttons *btn, uint8_t sample)
This function handles the debouncing and tracking of whether buttons are pressed, released or held...
void resetAllButton()
Resets the state of all 4 buttons at once.
float resolution
volatile buttons recordBtn
uint8_t play
void recordPage(bool pidMode, bool recorded, uint8_t index, float pos)
Holds the code for the record page of the OLED.
uint16_t acceleration
void idlePage(bool pidMode, float pos)
Holds the code for the idle page of the OLED.
void timeMode(void)
Holds the timelapse logic, showing the timelapse page.
friend void WDT_vect(void) __attribute__((signal
U8G2_SSD1306_128X64_NONAME_1_4W_SW_SPI * u8g2
volatile buttons playBtn
void playMode(void)
Holds the play logic, showing play page and running the recorded sequence.
void manBackward(void)
Holds the manual backward logic for driving the stepper motor manually with the pushbuttons.
uint8_t microStepping
float pos[CNT]
void pauseMode(void)
Holds the pause logic, showing the pause page and pausing the playing of a sequence.
bool longPushFlag[4]
void resetButton(buttons *btn)
Function for resetting the state of a button seperately.
struct to hold information required to debounce button.
uint16_t time
uint8_t rec
uint16_t velocity
#define SIXTEEN
void inputs(void)
Reads the four buttons and writes their value; no push, short push or long push, to global variables...
#define CNT
uint8_t state
uint16_t faultTolerance
SoftwareSerial * BTSerial
void startPage(void)
Holds the code for the start page of the OLED.
volatile buttons forwardBtn
float stepSize
void pausePage(bool loopMode, bool pidMode, uint8_t index)
Holds the code for the pause page of the OLED.
void WDT_vect(void) __attribute__((signal
float setPoint
void manForward(void)
Holds the manual forward logic for driving the stepper motor manually with the pushbuttons.
uint16_t faultHysteresis
volatile buttons backwardsBtn
uint8_t buttonState(uint8_t button, uint8_t nmbr)
Returns the button state of the appropriate button.
uint8_t bw
egoShield(void)
Constructor of egoShield class.
uint16_t interval
void setup(uint16_t acc=1500, uint16_t vel=1000, uint8_t uStep=SIXTEEN, uint16_t fTol=10, uint16_t fHys=5, float P=1.0, float I=0.02, float D=0.006, float res=1)
Initializes buttons, OLED, uStepper and BT-module.