main.cpp 2.91 KB
Newer Older
Jake Read's avatar
Jake Read committed
1
2
3
4
5
6
7
8
9
10
11
#include <Arduino.h>

// system
#include "drivers/indicators.h"
#include "comm/cobs.h"
#include "comm/cobsserial.h"

// loadcell / stepper 
#include "lib/HX711.h"
#include "lib/AccelStepper.h"

Jake Read's avatar
Jake Read committed
12
13
#define LOADCELL_DOUT_PIN 12
#define LOADCELL_SCK_PIN 11
Jake Read's avatar
Jake Read committed
14
15
16
17
18
19
20
21
22
23
#define LOADCELL_OFFSET 50682624
#define LOADCELL_DIVIDER 5895655

#define STEPPER_STEP_PIN 9
#define STEPPER_DIR_PIN 10
#define STEPPER_ENABLE_PIN 5

HX711 loadcell;
AccelStepper stepper(AccelStepper::DRIVER, 9, 10);

Jake Read's avatar
Jake Read committed
24
25
26
27
28
// using A4988 step driver, 
// want to limit to 1A current for thermal safety, 
// vref = 8 * I_max * 0.050 // is 8 * 1A * 50mOhm 
// so, target vref at 0.4v

Jake Read's avatar
Jake Read committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
void enableStepDriver(void){
  digitalWrite(STEPPER_ENABLE_PIN, LOW);
}

void disableStepDriver(void){
  digitalWrite(STEPPER_ENABLE_PIN, HIGH);
}

COBSSerial* cobsSerial = new COBSSerial();

void setup() {
  // do osap setup, 
  ERRLIGHT_SETUP;
  CLKLIGHT_SETUP;
  cobsSerial->init();
  // do application setup, 
  // EN pin on step driver 
  pinMode(5, OUTPUT);
  disableStepDriver();
  // stepper basics hello 
Jake Read's avatar
Jake Read committed
49
50
  stepper.setMaxSpeed(2500);
  stepper.setAcceleration(5000);
Jake Read's avatar
Jake Read committed
51
52
53
54
55
56
57
58
59
60
  // loacell 
  loadcell.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
  loadcell.set_scale(LOADCELL_DIVIDER);
  loadcell.set_offset(LOADCELL_OFFSET);
  loadcell.tare();
}

uint8_t* pck;
uint16_t pl = 0;

Jake Read's avatar
Jake Read committed
61
62
63
64
65
66
67
68
69
70
71
72
uint8_t res[1024];
uint16_t rl = 0;

#define DEXKEY_LOADCELLREADING 12 
#define DEXKEY_LOADCELLTARE 14
#define DEXKEY_STEPS 15
#define DEXKEY_MOTORENABLE 16

uint16_t one = 1;
int32_t stepsToTake = 0;
int32_t position = 0;

Jake Read's avatar
Jake Read committed
73
void loop() {
Jake Read's avatar
Jake Read committed
74
  stepper.run();
Jake Read's avatar
Jake Read committed
75
76
77
78
  cobsSerial->loop();
  if(cobsSerial->hasPacket()){
    CLKLIGHT_TOGGLE;
    cobsSerial->getPacket(&pck, &pl);
Jake Read's avatar
Jake Read committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
    switch(pck[0]){
      case DEXKEY_LOADCELLREADING:
        if(loadcell.is_ready()){
          int32_t reading = loadcell.get_value(10);
          rl = 0;
          res[rl ++] = DEXKEY_LOADCELLREADING;
          ts_writeInt32(reading, res, &rl);
          debugmsg("reads: " + String(reading));
        } else {
          debugmsg("loadcell not ready");
        }
        break;
      case DEXKEY_LOADCELLTARE:
        loadcell.tare();
        rl = 0;
        res[rl ++] = DEXKEY_LOADCELLTARE;
        break;
      case DEXKEY_STEPS:
        stepsToTake = 0;
        one = 1;
        ts_readInt32(&stepsToTake, pck, &one);
        debugmsg("to take steps: " + String(stepsToTake));
        stepper.runToNewPosition(position + stepsToTake);
        position += stepsToTake;
        rl = 0;
        res[rl ++] = DEXKEY_STEPS;
        ts_writeInt32(stepsToTake, res, &rl);
        break;
      case DEXKEY_MOTORENABLE:
        rl = 0;
        res[rl ++] = DEXKEY_MOTORENABLE;
        if(pck[1] > 0){
          enableStepDriver();
          res[rl ++] = 1;
        } else {
          disableStepDriver();
          res[rl ++] = 0;
        }
      default:
        // no-op 
        break;
    }
    if(rl > 0){
      cobsSerial->sendPacket(res, rl);
    }
Jake Read's avatar
Jake Read committed
124
125
126
    cobsSerial->clearPacket();
  }
} // end loop