-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGearbox.cpp
More file actions
129 lines (115 loc) · 4 KB
/
Gearbox.cpp
File metadata and controls
129 lines (115 loc) · 4 KB
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
// Pico Electronic Leadscrew
// https://github.com/funkenjaeger/pico-els
//
// MIT License
//
// Copyright (c) 2025 Evan Dudzik
//
// This software is based on the Clough42 Electronic Leadscrew project under the MIT license
// https://github.com/clough42/electronic-leadscrew
// Leveraged portions of this software are Copyright (c) 2019 James Clough
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "Gearbox.h"
#include "Configuration.h"
#include <stdio.h>
#include "pico/stdlib.h"
#include "pico/binary_info.h"
#include "hardware/i2c.h"
#include "cppcrc.h"
Gearbox :: Gearbox(void) {
i2c_init(i2c0, GEARBOX_I2C_BAUDRATE);
gpio_set_function(GEARBOX_SDA_PIN, GPIO_FUNC_I2C);
gpio_set_function(GEARBOX_SCL_PIN, GPIO_FUNC_I2C);
gpio_pull_up(GEARBOX_SDA_PIN);
gpio_pull_up(GEARBOX_SCL_PIN);
this->state.direction = FORWARD;
this->state.feed_thread = FEED;
this->state.gear = A;
this->state.finalDriveRatio = FEED_GEAR_RATIO * GEARBOX_DRIVE_RATIO_A;
}
bool Gearbox :: getState(GearboxState* state) {
int ret;
uint8_t rxdata[4];
ret = i2c_read_blocking(i2c_default, 0x55, rxdata, 4, false);
bool readSucceeded = (ret == 4);
printf("i2c read %d bytes\n", ret);
if(!readSucceeded) {
*state = this->state;
return false;
}
uint8_t crc_value = CRC8::CRC8::calc(rxdata, sizeof(rxdata)-1);
if(readSucceeded && crc_value == rxdata[3]) {
switch(rxdata[0]){
case 'F':
this->state.direction = FORWARD;
break;
case 'R':
this->state.direction = REVERSE;
break;
default:
break;
}
switch(rxdata[1]){
case 'A':
this->state.gear = A;
break;
case 'B':
this->state.gear = B;
break;
case 'C':
this->state.gear = C;
}
float gearRatio;
switch(this->state.gear){
case A:
gearRatio = GEARBOX_DRIVE_RATIO_A;
break;
case B:
gearRatio = GEARBOX_DRIVE_RATIO_B;
break;
case C:
gearRatio = GEARBOX_DRIVE_RATIO_C;
break;
default:
gearRatio = 0.0;
}
switch(rxdata[2]){
case 'F':
this->state.feed_thread = FEED;
break;
case 'T':
this->state.feed_thread = THREAD;
}
float feedThreadDriveRatio;
switch(this->state.feed_thread){
case FEED:
feedThreadDriveRatio = FEED_GEAR_RATIO;
break;
case THREAD:
feedThreadDriveRatio = THREAD_GEAR_RATIO;
break;
default:
feedThreadDriveRatio = 0.0;
}
this->state.finalDriveRatio = gearRatio * feedThreadDriveRatio;
}
*state = this->state;
return true;
}