Skip to content

Commit 8ae3ee7

Browse files
author
Ezra Boley
committed
Got the motor to spin with the brakes not actuated
1 parent 76709f9 commit 8ae3ee7

File tree

14 files changed

+157
-75
lines changed

14 files changed

+157
-75
lines changed

embedded/app/include/motor.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,9 @@ int stopMotor(void);
2525

2626
void idleMotor(void);
2727

28+
void setMotorEn();
29+
void clrMotorEn();
30+
void SetupMotor();
31+
32+
2833
#endif

embedded/app/include/transitions.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ int genTranAction(void);
55

66
int toPropulsion(void);
77

8+
int toPumpdown(void);
9+
810
int toBraking(void);
911

1012
int toCrawl(void);

embedded/app/main/badgerloop_HV.cpp

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,9 @@ extern "C"
2323
#include "state_machine.h"
2424
#include "NCD9830DBR2G.h"
2525
}
26-
extern bool noTorqueMode;
27-
extern bool motorIsEnabled;
2826
void emergQuitter(int sig, siginfo_t* inf, void *nul) {
2927
printf("shutdown\n");
30-
motorIsEnabled = false;
31-
noTorqueMode = false;
32-
usleep(10000);
28+
setMCUHVEnabled(false);
3329
rmsCmdNoTorque();
3430
sleep(1);
3531
rmsDischarge();
@@ -46,7 +42,9 @@ int init() {
4642
SetupCANDevices();
4743
initProcIox(true);
4844
initHVIox(true);
49-
/* initMotor(); */
45+
46+
SetupMotor();
47+
/* initMotor(); */
5048
initPressureSensors();
5149
/* Allocate needed memory for state machine and create graph */
5250
buildStateMachine();
@@ -65,11 +63,6 @@ int init() {
6563
return 0;
6664
}
6765

68-
void shutdown() {
69-
setTorque(0); /* Try our best to de-escalate while not overstaying our welcome */
70-
data->flags->shutdown = true;
71-
return;
72-
}
7366
int main() {
7467
/* Create the big data structures to house pod data */
7568

@@ -81,11 +74,6 @@ int main() {
8174
printf("Here\n");
8275

8376
while(1) {
84-
if (data->flags->shutdown) {
85-
if (getMotorIsOn())
86-
stopMotor();
87-
exit(0);
88-
}
8977
runStateMachine();
9078
if (data->flags->shouldBrake) {
9179
signalLV((char *)"brake");
@@ -97,6 +85,7 @@ int main() {
9785
data->flags->brakeInit = false;
9886
}
9987
if (data->flags->clrMotionData) {
88+
printf("signal clear\n");
10089
signalLV((char *) "clrMotion");
10190
data->flags->clrMotionData = false;
10291
}

embedded/app/main/badgerloop_LV.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,28 +45,27 @@ int main() {
4545
exit(1);
4646
}
4747
flags_t *f = data->flags;
48-
if (f == NULL) printf("its null\n");
4948
while(1) {
5049
usleep(100000);
51-
if (f->shouldBrake) {
50+
if (data->flags->shouldBrake) {
5251
brake();
53-
f->shouldBrake = false;
52+
data->flags->shouldBrake = false;
5453
}
55-
if (f->brakePrimAct) {
54+
if (data->flags->brakePrimAct) {
5655
brakePrimaryActuate();
57-
f->brakePrimAct = false;
56+
data->flags->brakePrimAct = false;
5857
}
59-
if (f->brakePrimRetr) {
58+
if (data->flags->brakePrimRetr) {
6059
brakePrimaryUnactuate();
61-
f->brakePrimRetr = false;
60+
data->flags->brakePrimRetr = false;
6261
}
63-
if (f->brakeSecAct) {
62+
if (data->flags->brakeSecAct) {
6463
brakeSecondaryActuate();
65-
f->brakeSecAct = false;
64+
data->flags->brakeSecAct = false;
6665
}
67-
if (f->brakeSecRetr) {
66+
if (data->flags->brakeSecRetr) {
6867
brakeSecondaryUnactuate();
69-
f->brakeSecRetr = false;
68+
data->flags->brakeSecRetr = false;
7069
}
7170
// Control loop
7271
}

embedded/app/src/motor.c

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include <unistd.h>
99
#include <hv_iox.h>
1010
#include <data.h>
11-
11+
bool invEn = false;
1212
/***
1313
* The high level interface for the motor
1414
*/
@@ -55,6 +55,37 @@ int initMotor() {
5555
return 0;
5656
}
5757

58+
static pthread_t hbThread;
59+
static bool motorEnabled = false;
60+
static bool noTorqueMode = false;
61+
static void motorHbLoop();
62+
63+
void setMotorEn() {
64+
motorEnabled = true;
65+
}
66+
67+
void clrMotorEn() {
68+
motorEnabled = false;
69+
}
70+
71+
72+
73+
void SetupMotor() {
74+
pthread_create(&hbThread, NULL, (motorHbLoop), NULL);
75+
}
76+
77+
static void motorHbLoop() {
78+
while(1) {
79+
if (motorEnabled)
80+
rmsInvEn();
81+
else if (noTorqueMode)
82+
rmsInvEnNoTorque();
83+
else
84+
rmsIdleHb();
85+
usleep(10000);
86+
}
87+
}
88+
5889
void joinMotorHbThread() {
5990
JOIN_HB_THREAD = true;
6091
if (pthread_join(hbLoop, NULL) != 0) {
@@ -102,6 +133,7 @@ int startMotor() {
102133
rmsClrFaults();
103134
rmsInvDis();
104135
/* idleMotor();*/
136+
/* invEn = true;*/
105137
sleep(3); /* Not ideal, but unless we have a way to check status this stays */
106138
rmsInvEnNoTorque();
107139
setMotorIsOn(true);
@@ -139,12 +171,14 @@ static void *motorHeartbeatLoop(void *unusedParam) {
139171

140172
sem_wait(&hbSem);
141173
if (getMotorIsOn()) {
142-
printf("MOTOR IS ON\n");
174+
printf("CURR TIME: %llu\n", getuSTimestamp());
143175
rmsInvEn();
144176
/* if (rmsSendHbMsg(getTorque()) != 0) {*/
145177
/* fprintf(stderr, "Failed to send heartbeat\n");*/
146178
/* }*/
147-
} else {
179+
/* } else if (invEn) {*/
180+
/* rmsInvEnNoTorque();*/
181+
}else {
148182
if (rmsIdleHb() != 0) {
149183
fprintf(stderr, "Failed to send idle heartbeat\n");
150184
}

embedded/app/src/nav.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,10 @@ void navLoop(void *unused) {
182182
imuDirFn[CURR_DIR].setPos(0);
183183
}
184184

185-
if ((getuSTimestamp() - data->timers->startTime) > TOTAL_RUN_TIME) {
186-
data->flags->shouldStop = true;
187-
}
185+
/* if ((getuSTimestamp() - data->timers->startTime) > TOTAL_RUN_TIME) {*/
186+
/* data->flags->shouldStop = true;*/
187+
/* }*/
188188

189-
if (data->flags->shouldBrake) {
190-
printf("BRAKE\n");
191-
}
192189
lastRetroCount = data->motion->retroCount;
193190
/* showNavData(); */
194191
/* csvFormatShow(); */

embedded/app/src/state_machine.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ static int addTransition(char *stateName, stateTransition_t *trans) {
132132

133133
static int initIdle(state_t *idle) {
134134

135-
initTransition(idle->transitions[0], findState(PUMPDOWN_NAME), genTranAction);
135+
initTransition(idle->transitions[0], findState(PUMPDOWN_NAME), toPumpdown);
136136
initTransition(idle->transitions[1], findState(NON_RUN_FAULT_NAME), genTranAction);
137137
initTransition(idle->transitions[2], findState(RUN_FAULT_NAME), toRunFault);
138138
addTransition(IDLE_NAME, idle->transitions[0]);
@@ -265,11 +265,10 @@ state_t *getCurrState() {
265265
void runStateMachine(void) {
266266
/* The cmd receiver will populate this field if we get an override */
267267
if (strcmp(stateMachine.overrideStateName, BLANK_NAME) != 0) {
268-
269268
state_t *tempState = findState(stateMachine.overrideStateName);
270269
/* TODO We also need to execute a transition if it exists here */
271270
if (tempState != NULL) {
272-
stateTransition_t *trans = findTransition(tempState, stateMachine.overrideStateName);
271+
stateTransition_t *trans = findTransition(stateMachine.currState, stateMachine.overrideStateName);
273272
if (trans != NULL) trans->action();
274273
stateMachine.currState = tempState;
275274
}
@@ -304,7 +303,7 @@ void buildStateMachine(void) {
304303
initState(stateMachine.allStates[0], IDLE_NAME, idleAction, 3);
305304
initState(stateMachine.allStates[1], PUMPDOWN_NAME, pumpdownAction, 2);
306305
initState(stateMachine.allStates[2], PROPULSION_NAME, propulsionAction, 2);
307-
initState(stateMachine.allStates[3], BRAKING_NAME, brakingAction, 3);
306+
initState(stateMachine.allStates[3], BRAKING_NAME, brakingAction, 2);
308307
initState(stateMachine.allStates[4], SERV_PRECHARGE_NAME, servPrechargeAction, 2);
309308
initState(stateMachine.allStates[5], CRAWL_NAME, crawlAction, 3);
310309
initState(stateMachine.allStates[6], STOPPED_NAME, stoppedAction, 3);

embedded/app/src/states.c

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,20 @@ stateTransition_t * pumpdownAction() {
109109
// First check for nominal values?
110110
data->state = 2;
111111
/* Check IMD status */
112+
static firstRun = true;
113+
uint64_t start;
114+
if (firstRun) {
115+
start = getuSTimestamp();
116+
firstRun = false;
117+
}
118+
119+
/* if (getuSTimestamp() - start > 3000000)*/
120+
/* return findTransition(stateMachine.currState,PROPULSION_NAME);*/
121+
/**/
112122
if (data->flags->emergencyBrake) {
113123
return findTransition(stateMachine.currState ,RUN_FAULT_NAME);
114124
}
125+
115126
#ifndef NO_FAULT
116127
if (!getIMDStatus()) {
117128
return stateMachine.currState->fault;
@@ -126,7 +137,6 @@ stateTransition_t * pumpdownAction() {
126137
if(!checkPrerunPressures()){
127138
return stateMachine.currState->fault;
128139
}
129-
#endif
130140
// TODO check LV Power
131141
// TODO check LV Temp
132142

@@ -137,18 +147,18 @@ stateTransition_t * pumpdownAction() {
137147
if(!checkPrerunRMS()){
138148
return stateMachine.currState->fault;
139149
}
150+
#endif
140151

141-
142-
if(data->flags->readyCommand){
143-
return findTransition(stateMachine.currState, PROPULSION_NAME);
144-
}
152+
/* if(data->flags->readyCommand){*/
153+
/* return findTransition(stateMachine.currState, PROPULSION_NAME);*/
154+
/* }*/
145155

146156
return NULL;
147157
}
148158

149159
stateTransition_t * propulsionAction() {
150160
static bool isInit = false;
151-
161+
setMotorEn();
152162
data->state = 3;
153163
if (!isInit) {
154164
data->flags->clrMotionData = true;
@@ -209,11 +219,13 @@ stateTransition_t * propulsionAction() {
209219
}
210220

211221
stateTransition_t * brakingAction() {
222+
212223
data->state = 4;
213224
// TODO Do we differenciate between primary and secondary braking systems?
214225
// TODO Add logic to handle switches / actuate both
215226
/* Check IMD status */
216227
if (data->flags->emergencyBrake) {
228+
printf("?\n");
217229
return findTransition(stateMachine.currState ,RUN_FAULT_NAME);
218230
}
219231
#ifndef NO_FAULT
@@ -243,9 +255,9 @@ stateTransition_t * brakingAction() {
243255
return stateMachine.currState->fault;
244256
}
245257
#endif
246-
if ((getuSTimestamp() - data->timers->startTime) > MAX_RUN_TIME){
247-
return stateMachine.currState->fault;
248-
}
258+
/* if ((getuSTimestamp() - data->timers->startTime) > MAX_RUN_TIME){*/
259+
/* return stateMachine.currState->fault;*/
260+
/* }*/
249261

250262
// CHECK TRANSITION CRITERIA
251263
/* if(checkStopped()){

embedded/app/src/transitions.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,55 @@
1+
#include <hv_iox.h>
12
#include <stdio.h>
23
#include <stdbool.h>
34
#include <motor.h>
45
#include <data.h>
56
#include <transitions.h>
67
#include <braking.h>
8+
#include <rms.h>
9+
710

811
/* If there is nothing special to do */
912
int genTranAction() {
1013
return 0;
1114
}
1215

16+
int toPumpdown() {
17+
printf("LETS GO\n");
18+
setMCULatch(true);
19+
usleep(10000);
20+
setMCULatch(false);
21+
setMCUHVEnabled(true);
22+
sleep(1);
23+
if(rmsEnHeartbeat() != 0) printf("EEERR0\n");
24+
if (rmsClrFaults() != 0) printf("eeE1\n");
25+
if(rmsInvDis() != 0) printf("EEERR2\n");
26+
return 0;
27+
/* sleep(3);*/
28+
}
29+
1330
int toPropulsion() {
1431
/* if (startMotor() != 0) return 1;*/
1532
/* setTorque(FULL_SPEED_AHEAD);*/
1633
data->timers->startTime = getuSTimestamp();
34+
1735
/* FIXME I need a way to tell if this was successful */
1836
return 0;
1937
}
2038

39+
40+
2141
int toBraking() {
2242
/* if (stopMotor() != 0) return 1;*/
43+
clrMotorEn();
44+
usleep(1000);
45+
rmsCmdNoTorque();
46+
usleep(1000);
47+
rmsDischarge();
48+
usleep(1000);
49+
rmsInvDis();
50+
usleep(1000);
51+
52+
setMCUHVEnabled(false);
2353
brakeHV();
2454
return 0;
2555
}
@@ -29,7 +59,7 @@ int toCrawl() {
2959
brakeSecondaryUnactuate(); /* Usually doesnt do anything */
3060

3161
data->timers->crawlTimer = getuSTimestamp();
32-
62+
3363
if (startMotor() != 0) return 1;
3464
setTorque(CRAWL_TORQUE);
3565
return 0;

embedded/peripherals/src/can_devices.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pthread_t CANThread;
1414

1515
void SetupCANDevices(){
1616
initCan();
17-
initMotor();
17+
/* initMotor();*/
1818
if (pthread_create(&CANThread, NULL, CANLoop, NULL)){
1919
fprintf(stderr, "Error creating CAN thread\n");
2020
}

0 commit comments

Comments
 (0)