main.cpp 4.98 KB
Newer Older
Jake Read's avatar
Jake Read committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <Arduino.h>

#include "drivers/indicators.h"
#include "utils/cobs.h"
#include "osap/osap.h"

OSAP* osap = new OSAP("stepper motor drop");

#include "drivers/ucbus_drop.h"
#include "drivers/step_a4950.h"

union chunk_float32 {
  uint8_t bytes[4];
  float f;
};

union chunk_float64 {
  uint8_t bytes[8];
  double f;
};

// C_SCALE 
// 1: DACs go 0->512 (of 4096, peak current is 1.6A at 4096): 0.2A
// 2: DACs go 0->1024,
// ...
// 8: DACs go full width 

Jake Read's avatar
Jake Read committed
28
29
30
31
32
33
34
35
36
37
// clank cz:
// Z: Bus Drop 5, Axis Pick 2, Invert ?, SPU 1386.6666667
// TC: Bus Drop 4, 
// YL: Bus Drop 1, Invert true
// YR: Bus Drop 2, Invert false
// X: Bus Drop 3, Invert false

#define BUS_DROP 3
#define IS_LAST_DROP false

Jake Read's avatar
Jake Read committed
38
#define AXIS_PICK 0   // Z: 2, Y: 1, X: 0
Jake Read's avatar
Jake Read committed
39
40
41
#define AXIS_INVERT false  // Z: ?, YL: ?, YR: ?, X: ?
#define SPU 320.0F //1386.6666667F // always posiive! z: 3200, xy: 400, clank-cz: xy: 320
#define C_SCALE 0.3F // 0-1, floating
Jake Read's avatar
Jake Read committed
42
43
44
45
46
47
48
49
50
51
52
53
#define TICKS_PER_PACKET 20.0F

void setup() {
  ERRLIGHT_SETUP;
  CLKLIGHT_SETUP;
  DEBUG1PIN_SETUP;
  DEBUG2PIN_SETUP;
  DEBUG3PIN_SETUP;
  DEBUG4PIN_SETUP;
  // osap
  osap->description = "remote stepper drop";
  // bus 
Jake Read's avatar
Jake Read committed
54
  ucBusDrop->init(IS_LAST_DROP, BUS_DROP);
Jake Read's avatar
Jake Read committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
  // stepper
  stepper_hw->init(AXIS_INVERT, C_SCALE);
}

uint8_t bChPck[64];

volatile float current_floating_pos = 0.0F;
volatile int32_t current_step_pos = 0;
volatile uint32_t delta_steps = 0;

volatile float vel = 0.0F; 
volatile float move_counter = 0.0F;

volatile boolean setBlock = false;

void loop() {
  DEBUG2PIN_TOGGLE;
  osap->loop();
  stepper_hw->dacRefresh();
  // do packet B grab / handle 
  if(ucBusDrop->ctr_b()){
    uint16_t len = ucBusDrop->read_b(bChPck);
    uint16_t ptr = 0;
    switch(bChPck[ptr]){
      case AK_SETCURRENT: {
        // get currents 
        ptr ++;
        chunk_float32 currentChunks[3];
        currentChunks[0] = { .bytes = { bChPck[ptr ++], bChPck[ptr ++], bChPck[ptr ++], bChPck[ptr ++] } };
        currentChunks[1] = { .bytes = { bChPck[ptr ++], bChPck[ptr ++], bChPck[ptr ++], bChPck[ptr ++] } };
        currentChunks[2] = { .bytes = { bChPck[ptr ++], bChPck[ptr ++], bChPck[ptr ++], bChPck[ptr ++] } };
        // set current
        stepper_hw->setCurrent(currentChunks[AXIS_PICK].f);
        break;
        }
      default:
        // noop,
        break;
    }
  }
} // end loop 

void UCBus_Drop::onRxISR(void){
  if(setBlock) return;
  //DEBUG2PIN_TOGGLE;
  move_counter += vel;
  boolean move_check = (move_counter > 1.0F);
  //DEBUG2PIN_TOGGLE;
  if(move_check){
    move_counter -= 1.0F;
    if(delta_steps == 0){
      // nothing 
    } else {
      //DEBUG1PIN_TOGGLE;
      stepper_hw->step();
      delta_steps --;
      if(stepper_hw->getDir()){
        current_step_pos ++;
      } else {
        current_step_pos --;
      }
    }
  }
}

void UCBus_Drop::onPacketARx(void){
  if(setBlock) return;
  //DEBUG2PIN_TOGGLE;
  // last move is done, convert back steps -> float,
  current_floating_pos = current_step_pos / SPU;
  vel = 0.0F; // reset zero in case packet is not move 
  uint8_t bptr = 0;
  // switch bus packet types 
  switch(inBufferA[0]){
    case UB_AK_GOTOPOS:
      {
        bptr = AXIS_PICK * 4 + 1;
        chunk_float32 target = {
          .bytes = { inBufferA[bptr], inBufferA[bptr + 1], inBufferA[bptr + 2], inBufferA[bptr + 3] }
        };
        /*
        chunk_float64 target = { 
          .bytes = { inBuffer[bptr], inBuffer[bptr + 1], inBuffer[bptr + 2], inBuffer[bptr + 3],
                    inBuffer[bptr + 4], inBuffer[bptr + 5], inBuffer[bptr + 6], inBuffer[bptr + 7] }};
                    */
        float delta = target.f - current_floating_pos;
        // update,
        //move_counter = 0.0F; // shouldn't need this ? try deleting 
        // direction swop,
        if(delta > 0){
          stepper_hw->dir(true);
        } else {
          stepper_hw->dir(false);
        }
        // how many steps, 
        delta_steps = lroundf(abs(delta * SPU));
        // what speed 
        vel = abs(delta * SPU) / TICKS_PER_PACKET;
        // for str8 r8 
        /*
        if(delta_steps == 0){
          vel = 0.0F;
        } else {
          vel = abs(delta * SPU) / TICKS_PER_PACKET;
        }
        */
        break; // end gotopos 
      }
    case UB_AK_SETPOS:
      {
        // reqest is to set position, not go to it... 
        bptr = AXIS_PICK * 4 + 1;
        chunk_float32 target = {
          .bytes = { inBufferA[bptr], inBufferA[bptr + 1], inBufferA[bptr + 2], inBufferA[bptr + 3] }
        };
        float target_current_pos = target.f;
        int32_t target_current_steps = lroundf(target_current_pos * SPU);
        setBlock = true; // don't do step work while these are modified 
        current_floating_pos = target_current_pos;
        current_step_pos = target_current_steps;
        vel = 0;
        delta_steps = 0;
        setBlock = false;
        break;
      }
    default:
      break;
  }  
  //DEBUG2PIN_OFF;
}

void OSAP::handleAppPacket(uint8_t *pck, uint16_t pl, uint16_t ptr, uint16_t segsize, VPort* vp, uint16_t vpi, uint8_t pwp){
  vp->clearPacket(pwp);
}