@@ -26,6 +26,17 @@ void led_on() {
2626 digitalWrite (LED_BUILTIN, HIGH);
2727}
2828
29+ typedef struct {
30+ Fix16 P;
31+ Fix16 I;
32+ Fix16 D;
33+ } PIDGains;
34+
35+ union PIDData {
36+ Fix16 txFloat;
37+ uint8_t txArray[4 ];
38+ } PIDGain;
39+
2940void setup () {
3041
3142 WDT->CTRL .reg &= ~WDT_CTRL_ENABLE;
@@ -46,7 +57,7 @@ void setup() {
4657 encoders[1 ] = new EncoderWrapper (ENCODER_1_PIN_A, ENCODER_1_PIN_B, 0 );
4758
4859 pid_control[0 ] = new PIDWrapper (encoders[0 ]->position , encoders[0 ]->velocity , dcmotors[0 ], 0 , 10 , 100 ); // 10ms period velo, 100ms period pos
49- pid_control[1 ] = new PIDWrapper (encoders[1 ]->position , encoders[1 ]->velocity , dcmotors[1 ], 1 , 10 , 100 ),
60+ pid_control[1 ] = new PIDWrapper (encoders[1 ]->position , encoders[1 ]->velocity , dcmotors[1 ], 1 , 10 , 100 );
5061
5162 Wire.begin (I2C_ADDRESS);
5263 Wire.onRequest (requestEvent);
@@ -101,7 +112,7 @@ void receiveEvent(int howMany) {
101112 return ;
102113 }
103114
104- uint8_t buf[8 ];
115+ uint8_t buf[12 ];
105116 int i = 0 ;
106117 while (Wire.available () && i < sizeof (buf)) {
107118 buf[i++] = (uint8_t )Wire.read ();
@@ -135,13 +146,10 @@ void receiveEvent(int howMany) {
135146 break ;
136147 case SET_PID_GAIN_CL_MOTOR:
137148 {
138- int16_t P16 = *((int16_t *)&buf[0 ]);
139- int16_t I16 = *((int16_t *)&buf[2 ]);
140- int16_t D16 = *((int16_t *)&buf[4 ]);
141- Fix16 P = ((Fix16)P16) / short (1000 );
142- Fix16 I = ((Fix16)I16) / short (1000 );
143- Fix16 D = ((Fix16)D16) / short (1000 );
144- pid_control[target]->setGains (P, I , D);
149+ Fix16 P = *((Fix16*)&buf[0 ]);
150+ Fix16 I = *((Fix16*)&buf[4 ]);
151+ Fix16 D = *((Fix16*)&buf[8 ]);
152+ pid_control[target]->setGains (P, I, D);
145153 break ;
146154 }
147155 case RESET_PID_GAIN_CL_MOTOR:
@@ -151,10 +159,10 @@ void receiveEvent(int howMany) {
151159 pid_control[target]->setControlMode ((cl_control)value);
152160 break ;
153161 case SET_POSITION_SETPOINT_CL_MOTOR:
154- pid_control[target]->setSetpoint (TARGET_POSITION, Fix16 (value * 1.0 ));
162+ pid_control[target]->setSetpoint (TARGET_POSITION, Fix16 (value * 1.0 )); // Change to integer. "value" is a 32 bit data type in this case (int).
155163 break ;
156164 case SET_VELOCITY_SETPOINT_CL_MOTOR:
157- pid_control[target]->setSetpoint (TARGET_VELOCITY, Fix16 (value * 1.0 ));
165+ pid_control[target]->setSetpoint (TARGET_VELOCITY, Fix16 (value * 1.0 )); // Change to integer
158166 break ;
159167 case SET_MAX_ACCELERATION_CL_MOTOR: {
160168 pid_control[target]->setMaxAcceleration (Fix16 (value * 1.0 ));
@@ -211,6 +219,18 @@ void requestEvent() {
211219 break ;
212220 case GET_FREE_RAM:
213221 Wire.write ((int )FreeRam ());
222+ break ;
223+ case GET_PID_VAL:
224+ Fix16 gains[3 ];
225+ pid_control[target]->getGains ((Fix16*)gains);
226+
227+ PIDGains pidGains;
228+ pidGains.P = gains[0 ];
229+ pidGains.I = gains[1 ];
230+ pidGains.D = gains[2 ];
231+
232+ Wire.write ((uint8_t *)&pidGains, sizeof (pidGains));
233+
214234 break ;
215235 }
216236 interrupts ();
0 commit comments