main.cpp 10.1 KB
Newer Older
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
#include <Arduino.h>

#include "indicators.h"
#include "drivers/step_a4950.h"
#include "osape-d51/osape/osap/osap.h"
#include "osape-d51/vertices/vt_usbSerial.h"
#include "osape-d51/vertices/vt_ucBusDrop.h"
#include "osape-d51/vertices/ucBusDrop.h"

// bare defaults: use vm / bus id to set on startup 
uint8_t axis_pick = 0;
float spu = 400.0F;
float old_spu = 400.0F;
volatile boolean spu_was_set = false;
float c_scale = 0.00F;
#define TICKS_PER_PACKET 25.0F
#define TICKS_PER_SECOND 50000.0F

// -------------------------------------------------------- AXIS PICK EP 

EP_ONDATA_RESPONSES onAxisPickData(uint8_t* data, uint16_t len){
  if(data[0] > 3){
    axis_pick = 0;
  } else {
    axis_pick = data[0];
  }
  return EP_ONDATA_ACCEPT;
}

vertex_t* axisPickEp = osapBuildEndpoint("axisPick", onAxisPickData, nullptr);

// -------------------------------------------------------- AXIS INVERSION EP

EP_ONDATA_RESPONSES onAxisInvertData(uint8_t* data, uint16_t len){
  if(data[0] > 0){
    stepper_hw->setInversion(true);
  } else {
    stepper_hw->setInversion(false);
  }
  return EP_ONDATA_ACCEPT;
}

vertex_t* axisInvertEp = osapBuildEndpoint("axisInvert", onAxisInvertData, nullptr);

// -------------------------------------------------------- MICROSTEP EP 

EP_ONDATA_RESPONSES onMicrostepData(uint8_t* data, uint16_t len){
  stepper_hw->setMicrostep(data[0]);
  return EP_ONDATA_ACCEPT;
}

vertex_t* microstepEp = osapBuildEndpoint("microstep", onMicrostepData, nullptr);

// -------------------------------------------------------- SPU EP

EP_ONDATA_RESPONSES onSPUData(uint8_t* data, uint16_t len){
  chunk_float32 spuc = { .bytes = { data[0], data[1], data[2], data[3] } };
  old_spu = spu;
  spu = fabsf(spuc.f);
  spu_was_set = true;
  return EP_ONDATA_ACCEPT;
}

vertex_t* spuEp = osapBuildEndpoint("SPU", onSPUData, nullptr);

// -------------------------------------------------------- CSCALE DATA

EP_ONDATA_RESPONSES onCScaleData(uint8_t* data, uint16_t len){
  chunk_float32 cscalec = { .bytes = { data[0], data[1], data[2], data[3] } };
  if(cscalec.f > 1.0F){
    cscalec.f = 1.0F;
  } else if (cscalec.f < 0.0F){
    cscalec.f = 0.0F;
  }
  stepper_hw->setCurrent(cscalec.f);
  return EP_ONDATA_ACCEPT;
}

vertex_t* cScaleEp = osapBuildEndpoint("CScale", onCScaleData, nullptr);

// -------------------------------------------------------- HOME ROUTINE

// some homeing globals, 
#define HOME_NOT 0
#define HOME_FIRST 1
#define HOME_BACKOFF 2

uint8_t homing = 0;           // statemachine 
float homeStepCounter = 0.0F; // step-float-counter
float homePos = 0.0F;         // position (units)
float homeStepRate = 0.0F;    // rate (steps/tick)
float homePosRate = 0.0F;     // rate (units/tick)
boolean homeDir = false;      // direction 
float homeOffset = 0.0F;      // after-home offset 

EP_ONDATA_RESPONSES onHomeData(uint8_t* data, uint16_t len){
  chunk_float32 rate = { .bytes = { data[0], data[1], data[2], data[3] } };
  chunk_float32 offset = { .bytes = { data[4], data[5], data[6], data[7] } };
  homing = HOME_FIRST;
  homeStepCounter = 0.0F;
  if(rate.f > 0){
    homeDir = true;
    stepper_hw->dir(true);
  } else {
    homeDir = false;
    stepper_hw->dir(false);
  }
  homeStepRate = abs(rate.f * spu) / TICKS_PER_SECOND;
  homePosRate = abs(rate.f) / TICKS_PER_SECOND;
  homeOffset = offset.f;
  return EP_ONDATA_ACCEPT;
}

vertex_t* homeEp = osapBuildEndpoint("Home", onHomeData, nullptr);

// -------------------------------------------------------- HOME STATE 

boolean beforeHomeStateQuery(void);

vertex_t* homeStateEp = osapBuildEndpoint("HomeState", nullptr, beforeHomeStateQuery);

boolean beforeHomeStateQuery(void){
  homeStateEp->ep->data[0] = homing;
  homeStateEp->ep->dataLen = 1;
  return true;
}

// -------------------------------------------------------- LIMIT SETUP 

#define LIMIT_PORT PORT->Group[0]
#define LIMIT_PIN 23 
#define LIMIT_BM ((uint32_t)(1 << LIMIT_PIN))

void limitSetup(void){
  // not-an-output 
  LIMIT_PORT.DIRCLR.reg = LIMIT_BM;
  // enable input 
  LIMIT_PORT.PINCFG[LIMIT_PIN].bit.INEN = 1;
  // enable pull 
  LIMIT_PORT.PINCFG[LIMIT_PIN].bit.PULLEN = 1;
  // 'pull' references direction from 'out' register, so we set hi to pull up (switch pulls to gnd)
  LIMIT_PORT.OUTSET.reg = LIMIT_BM;
}

boolean limitIsMade(void){
  // return true if switch is hit 
  return (LIMIT_PORT.IN.reg & LIMIT_BM);
}

void setup() {
  ERRLIGHT_SETUP;
  CLKLIGHT_SETUP;
  DEBUG1PIN_SETUP;
Jake Read's avatar
Jake Read committed
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
  // limit switch 
  //limitSetup();
  // osap
  osapSetup();
  // ports 
  vt_usbSerial_setup();
  osapAddVertex(vt_usbSerial);    // 0
  vt_ucBusDrop_setup();
  osapAddVertex(vt_ucBusDrop);    // 1
  // axis pick 
  osapAddVertex(axisPickEp);      // 2
  // axis invert
  osapAddVertex(axisInvertEp);    // 3
  // microstep 
  osapAddVertex(microstepEp);     // 4
  // SPU 
  osapAddVertex(spuEp);           // 5
  // cscale 
  osapAddVertex(cScaleEp);        // 6
  // homing 
  osapAddVertex(homeEp);          // 7 
  osapAddVertex(homeStateEp);     // 8 
  // stepper init 
  stepper_hw->init(false, c_scale);
Jake Read's avatar
Jake Read committed
179
180
181
182
  */
  // bare serial
  Serial.begin(9600);
  ucBusDrop_setup(true, 0);
183
184
185
186
187
188
189
190
191
192
}

// have available, 
// stepper_hw->setCurrent(currentChunks[AXIS_PICK].f);

//#define TEST_TX 

unsigned long lastTx = 0;
uint8_t tstTx[14] = {1, 2, 3, 4, 5, 0, 7, 8, 9, 10, 11, 12, 13, 14};

Jake Read's avatar
Jake Read committed
193
194
uint8_t tstRx[256];

195
void loop() {
Jake Read's avatar
Jake Read committed
196
197
198
199
200
201
202
203
204
205
206
  if(ucBusDrop_ctrB()){
    ERRLIGHT_TOGGLE;
    uint16_t len = ucBusDrop_readB(tstRx);
    logPacket(tstRx, len);
    if(ucBusDrop_ctsB()){
      CLKLIGHT_TOGGLE;
      ucBusDrop_transmitB(tstRx, len);
    }
  }
  //osapLoop();
  //stepper_hw->dacRefresh();
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
  #ifdef TEST_TX
  if(millis() > lastTx + 500){
    lastTx = millis();
    if(ucBusDrop_ctsB()){
      ERRLIGHT_TOGGLE;
      ucBusDrop_transmitB(tstTx, 14);
      for(uint8_t i = 0; i < 14; i ++){
        //tstTx[i] ++;
      }
    }
  }
  #endif 
  /*
  // still not sure: this registers as "made" on init (?) 
  if(limitIsMade()){
    ERRLIGHT_ON;
  } else {
    ERRLIGHT_OFF;
  }
  */
} // end loop 


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 ucBusDrop_onPacketARx(uint8_t* inBufferA, volatile uint16_t len){
  #warning debug-d out 
  return;
  // don't execute when we have been given a set-position block 
  if(setBlock) return;
  // don't execute if we are currently homing
  if(homing) return;
  //DEBUG2PIN_TOGGLE;
  // last move is done, convert back steps -> float,
  if(spu_was_set){
    current_floating_pos = current_step_pos / old_spu;
    current_step_pos = lroundf(current_floating_pos * spu);
    spu_was_set = false;
  } else {
    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 ucBusDrop_onRxISR(void){
  #warning debug-d out
  return;
  // no-op when given a set block, 
  if(setBlock) return;
  // incremental motion if is homing 
  if(homing != 0){
    switch(homing){
      case HOME_FIRST:
        if(limitIsMade()){
          // traaaaaaansition -> backoff 
          stepper_hw->dir(!homeDir);
          homeStepCounter = 0.0F;
          homePos = 0.0F;
          homing = HOME_BACKOFF;
        } else {
          homeStepCounter += homeStepRate;
          if(homeStepCounter >= 1.0F){
            homeStepCounter -= 1.0F;
            stepper_hw->step();
          }
        }
        break;
      case HOME_BACKOFF:
        homeStepCounter += homeStepRate;
        homePos += homePosRate;
        if(homeStepCounter >= 1.0F){  // backoff motion 
          homeStepCounter -= 1.0F;
          stepper_hw->step();
        }
        if(homePos >= homeOffset){ // until more than 2mm away 
          // traaaaaaaaaaaaaansition -> end 
          homing = 0;
        }
        break;
      default:
        homing = 0;
    }
    return;
  }
  // normal step operation 
  //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 --;
      }
    }
  }
}