Skip to content

Commit fbb6d58

Browse files
committed
motor
1 parent 944a4e5 commit fbb6d58

File tree

5 files changed

+182
-25
lines changed

5 files changed

+182
-25
lines changed

pie_app/bPins.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def exiting(self):
9595
# then check for level/tick and we are in pigpio
9696
def gpio_InputPinCallback(self, pin):
9797
""" Input pin callback for GPIO """
98-
print('=== bPins.gpio_InputPinCallback() pin:', pin)
98+
#print('=== bPins.gpio_InputPinCallback() pin:', pin)
9999
now = time.time()
100100
self.inputCallback(pin, now)
101101

@@ -110,6 +110,7 @@ def inputCallback(self, pin, now, tick=None):
110110
if pin not in self.pinNumberDict_:
111111
print('error: PinThread.inputCallback received bad pin', pin)
112112
else:
113+
currentState = GPIO.input(pin)
113114
pinDict = self.pinNumberDict_[pin]
114115
name = pinDict['name']
115116
if name == 'triggerIn':
@@ -180,7 +181,7 @@ def inputCallback(self, pin, now, tick=None):
180181
videoTimestamp = str(frame.timestamp)
181182

182183
# log the event
183-
self.trial.newEvent(name, True, now=now, str=videoTimestamp, tick=tick)
184+
self.trial.newEvent(name, currentState, now=now, str=videoTimestamp, tick=tick)
184185

185186
##########################################
186187
# Output pins on/off

pie_app/static/treadmill.js

Lines changed: 125 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ app.controller('configFormController', function($scope, $rootScope, $http, $inte
9393
if ($scope.configData.trial.runtime.cameraState == 'armed' || $scope.configData.trial.runtime.cameraState == 'armedrecording') {
9494
//20180718, get this working
9595
//console.log('CallParentMethod_SetConfigData() is calling $route.reload()')
96-
console.log('$rootScope.$on("CallParentMethod_SetConfigData" is calling $route.reload')
96+
97+
// this gets called at an interval (very expensive for Pi to be serving this)
98+
//console.log('$rootScope.$on("CallParentMethod_SetConfigData" is calling $route.reload')
9799
$route.reload();
98100
}
99101

@@ -224,7 +226,9 @@ app.controller('configFormController', function($scope, $rootScope, $http, $inte
224226
}); // configFormController
225227

226228

229+
227230
//////////////////////////////////////////////////////////////////////////////
231+
// controller for motor
228232
app.controller('arduinoFormController', function($scope, $rootScope, $http, statusFactory) {
229233

230234
// take current params and submit
@@ -244,13 +248,78 @@ app.controller('arduinoFormController', function($scope, $rootScope, $http, stat
244248
});
245249
};
246250

247-
//rebuild plotly but do not submit
251+
// 20181220
252+
//$scope.$on('someEvent', function(event, args) {
253+
/*
254+
$scope.$on('someEvent', function(event, data) {
255+
//args is secondsElapsedStr
256+
console.log('arduinoFormController received someEvent')
257+
//$scope.refreshPlotly(args);
258+
});
259+
*/
260+
261+
//
262+
//
263+
//
264+
//
265+
//
266+
//
267+
//
268+
//
269+
// THIS IS FUCKING IMPOSSIBLE TO GET WORKING
270+
// I CAN NOT DEAL WITH THESE JAVASCRIPT FRAMEWORKS !!!!!!!!!!!!!!!!!
271+
// THEY MAKE MY CODING STALL FOR DAYS AND WEEKS
272+
// COME ON GOOD AND FACEBOOK, THIS IS FUCKING BULLSHIT
273+
//
274+
//
275+
//
276+
//
277+
//
278+
//
279+
//
280+
//
281+
//
282+
283+
/*
284+
$rootScope.$on("rootEvent", function(event, thisConfig) {
285+
console.log('arduinoFormController received rootEvent')
286+
})
287+
$scope.$on("rootEvent", function(event, thisConfig) {
288+
console.log('arduinoFormController received rootEvent')
289+
})
290+
*/
291+
292+
$scope.$on('rootEvent2', function (event, secondsElapsedStr) {
293+
console.log('I am from motor controller', secondsElapsedStr);
294+
var msElapsed = parseFloat(secondsElapsedStr) * 1000;
295+
Plotly.restyle('test_plotly', 'x', [[msElapsed, msElapsed]]);
296+
});
297+
298+
/*
299+
$scope.$on('rootEvent', function(event, data){
300+
console.log('arduinoFormController received rootEvent')
301+
})
302+
$rootScope.$on('rootEvent', function(event, data){
303+
console.log('arduinoFormController received rootEvent')
304+
})
305+
*/
306+
307+
//rebuild plotly but do not submit
248308
$scope.motorParamChange = function() {
249309
// if form fields don't pass validation, they will be 'undefined'
250310
console.log('motorParamChange()', $scope.data)
251311
buildPlotly($scope.data)
252312
}
253313

314+
// respond to new secondsElapsedStr
315+
/*
316+
$scope.updateRuntime - function() {
317+
// Plotly.react(gd, data, layout, config)
318+
//buildPlotly($scope.data) //$scope.data is motorParams
319+
$scope.refreshPlotly($scope.status)
320+
}
321+
*/
322+
254323
var myPromise = statusFactory.getStatus()
255324
myPromise.then(function(result) {
256325
var status = result
@@ -268,6 +337,11 @@ app.controller('arduinoFormController', function($scope, $rootScope, $http, stat
268337

269338
}); // mypromise.then
270339

340+
// respond to new secondsElapsedStr
341+
$scope.refreshPlotly = function(status) {
342+
console.log('refreshPlotly()')
343+
}
344+
271345
var buildPlotly = function(motorParams){
272346
var trialMS = motorParams.motorNumEpochs * motorParams.motorRepeatDuration
273347
//console.log('buildPlotly() trialMS:', trialMS)
@@ -292,8 +366,8 @@ app.controller('arduinoFormController', function($scope, $rootScope, $http, stat
292366

293367
var data = [
294368
{
295-
x: [],
296-
y: [],
369+
x: [100,100],
370+
y: [0, 1],
297371
type: 'scatter'
298372
}
299373
];
@@ -316,6 +390,7 @@ app.controller('arduinoFormController', function($scope, $rootScope, $http, stat
316390
'color': lineColor,
317391
},
318392
'fillcolor': lineColor,
393+
opacity: 0.5,
319394
};
320395
shapeList.push(thisRect)
321396
}
@@ -569,6 +644,7 @@ app.controller('treadmill', function($scope, $rootScope, $window, $http, $locati
569644

570645
///////////////////////////////////////////////////////////////////////////
571646
// counter to display elapsed recording time rather than hitting REST with http.get
647+
// this is used while we are acquiring data, VERY IMPORTANT
572648
var defaultInterval = 800; // 800
573649
var counter = 0;
574650
$scope.myIntervalFunction = function(){
@@ -578,6 +654,51 @@ app.controller('treadmill', function($scope, $rootScope, $window, $http, $locati
578654
counter += 0.4
579655
counter = Math.round(counter * 100) / 100
580656
$scope.status.trial.runtime.secondsElapsedStr = counter
657+
658+
//
659+
//
660+
//
661+
//
662+
//
663+
//
664+
//
665+
//
666+
// THIS IS FUCKING IMPOSSIBLE TO GET WORKING
667+
// I CAN NOT DEAL WITH THESE JAVASCRIPT FRAMEWORKS !!!!!!!!!!!!!!!!!
668+
// THEY MAKE MY CODING STALL FOR DAYS AND WEEKS
669+
// COME ON GOOD AND FACEBOOK, THIS IS FUCKING BULLSHIT
670+
//
671+
//
672+
//
673+
//
674+
//
675+
//
676+
//
677+
//
678+
//
679+
680+
// see: https://stackoverflow.com/questions/9293423/can-one-angularjs-controller-call-another
681+
// 20181220, update plot of motor !!!
682+
//$scope.refreshPlotly($scope.status)
683+
684+
// emit an update (for motor controller)
685+
//$rootScope.$broadcast('someEvent', $scope.status.trial.runtime.secondsElapsedStr);
686+
687+
//broadcast from rootScope and receive on $scope, this is fucking confusing
688+
//console.log('emit someEvent', $scope.status.trial.runtime.secondsElapsedStr)
689+
//$rootScope.$broadcast('someEvent', $scope.status.trial.runtime.secondsElapsedStr);
690+
691+
console.log('emit rootEvent', $scope.status.trial.runtime.secondsElapsedStr)
692+
//$rootScope.$broadcast('rootEvent', 'Hello from the rootScope!');
693+
//$rootScope.$emit("rootEvent", 'Hello from the rootScope!');
694+
$rootScope.$broadcast("rootEvent2", $scope.status.trial.runtime.secondsElapsedStr);
695+
//$scope.$broadcast('rootEvent', 'Hello from the rootScope!');
696+
//$scope.$emit('rootEvent', 'Hello from the rootScope!');
697+
698+
//$scope.$emit('someEvent', $scope.status.trial.runtime.secondsElapsedStr);
699+
// this will be captured/responded in motor controller with
700+
// motor controller repsonds with
701+
//$scope.$on('someEvent', function(event, args) {});
581702
}
582703

583704
$scope.setInterval = function(state) {

pie_app/templates/partials/motor_table.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
<!-- 20181220, had to add this <div> so we actually instantiate a motor controller -->
2+
<!-- and it can receive $on events from main contorller -->
3+
<div ng-controller="arduinoFormController">
4+
15
<span ng-if="status.trial.config.hardware.serial.useSerial">
26
<button type="button"
37
class="btn btn-link btn-sm"
@@ -101,7 +105,7 @@
101105
ng-disabled="! allowParamEdit()"
102106
min="1"
103107
step="1"
104-
max="700"
108+
max="1000"
105109
required>
106110
</label>
107111
</td>
@@ -166,3 +170,5 @@
166170

167171
</span> <!-- showMotor -->
168172
</span> <!-- use serial -->
173+
174+
</div> <!-- arduinoFormController -->

pie_app/treadmill.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ def loadConfig(self, loadThis):
175175
def serialUpdateMotor(self, motorDict):
176176
""" todo: put this in bTrial """
177177

178-
print('serialUpdateMotor()')
179-
print(motorDict)
178+
#print('serialUpdateMotor()')
179+
#print('motorDict:', motorDict)
180180

181181
for key, value in motorDict.items():
182182
#convert python based variable to arduino

platformio/treadmill/src/treadmill2.cpp

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@ const int newEpochPin = 12;//output pulse when new motor epoch
5858
const int encoderOutPin = 10; //output pulse when encoder changes
5959

6060
//encoder pins (these pins need to have low level interrupts)
61-
const int encoderPinA = 15;
62-
const int encoderPinB = 16;
61+
//todo: 20181220, this is not working any more. Give encoder unique pins and make sure output to raspberry is working
62+
const int encoderPinA = 21;
63+
const int encoderPinB = 22;
6364

6465
//motor pins (assuming easydriver board)
6566
const int motorStepPin = 17;
@@ -87,10 +88,15 @@ struct trial
8788
//parameters
8889
int armed; //boolean, added 20180702
8990

90-
int preDur; //ms
91-
int postDur; //ms
91+
//was this
92+
//int preDur; //ms
93+
//int postDur; //ms
94+
unsigned long preDur; //ms
95+
unsigned long postDur; //ms
9296

93-
int epochDur;
97+
//was this
98+
//int epochDur;
99+
unsigned long epochDur;
94100
int numEpoch;
95101

96102
//int numPulse;
@@ -103,12 +109,15 @@ struct trial
103109

104110
String duringPulse; // ("Rotate", "Locked", "Free")
105111
String betweenPulse; // ("Locked", "Free")
112+
String betweenTrial; // ("Locked", "Free")
106113

107114
//runtime
108115
boolean trialIsRunning;
109116
int trialNumber;
110117
unsigned long trialStartMillis;
111-
int trialDur; //ms, numEpoch*epochDur
118+
//was this
119+
//int trialDur; //ms, numEpoch*epochDur
120+
unsigned long trialDur; //ms, numEpoch*epochDur
112121
int currentEpoch;
113122
unsigned long epochStartMillis;
114123
//unsigned int currentPulse; //count 0,1,2,... as we run
@@ -290,6 +299,7 @@ void setup()
290299

291300
trial.duringPulse = "Rotate";
292301
trial.betweenPulse = "Locked";
302+
trial.betweenTrial = "Locked"; // ("Locked", "Free")
293303

294304
trial.currentFrame = 0;
295305
//trial.currentEncoderIndex = 0;
@@ -657,12 +667,14 @@ void updateTrial(unsigned long now) {
657667
return;
658668
}
659669

660-
g_inPreTrial = (g_msIntoTrial <= trial.preDur);
670+
// '<' is critical here!!!, <= and the first epoch overlaps with 2nd
671+
g_inPreTrial = (g_msIntoTrial < trial.preDur);
661672
g_inPostTrial = (g_msIntoTrial > (trial.preDur + (trial.epochDur * trial.numEpoch)));
662673
g_inPulseTrial = ((!g_inPreTrial) && (!g_inPostTrial));
663674

664-
if (!g_inPreTrial) {
665-
int tmpCurrEpoch = (g_msIntoTrial-trial.preDur) / trial.epochDur + 1;
675+
//if (!g_inPreTrial) {
676+
if (g_inPulseTrial) {
677+
int tmpCurrEpoch = int(floor((g_msIntoTrial-trial.preDur) / trial.epochDur) + 1);
666678
if (tmpCurrEpoch>0 && tmpCurrEpoch<=trial.numEpoch) {
667679
//epoch
668680
if (tmpCurrEpoch != trial.currentEpoch) {
@@ -674,22 +686,31 @@ void updateTrial(unsigned long now) {
674686
digitalWrite(newEpochPin, LOW);
675687
}
676688

689+
//was here
677690
g_msIntoEpoch = now - trial.epochStartMillis;
678691

679692
}
693+
//g_msIntoEpoch = now - trial.epochStartMillis;
694+
680695
}
696+
//g_msIntoEpoch = now - trial.epochStartMillis;
681697
}
682698

683699
/////////////////////////////////////////////////////////////
684700
void updateMotor(unsigned long now) {
685-
if (trial.trialIsRunning && trial.useMotor && g_inPulseTrial) {
686-
int motorStart = trial.motorDel;
687-
int motorStop = motorStart + trial.motorDur;
688-
if (!motor.isRunning && (g_msIntoEpoch >= motorStart) && (g_msIntoEpoch < motorStop)) {
701+
//need to use g_inPulseTrial as g_msIntoEpoch is not defined until g_msIntoEpoch is defined
702+
unsigned long tmpMillis = (now - trial.trialStartMillis - trial.preDur) % (trial.epochDur);
703+
//if (trial.trialIsRunning && trial.useMotor && g_inPulseTrial) {
704+
if (trial.trialIsRunning && trial.useMotor) {
705+
unsigned long motorStart = trial.motorDel;
706+
unsigned long motorStop = motorStart + trial.motorDur;
707+
//if (!motor.isRunning && (g_msIntoEpoch >= motorStart) && (g_msIntoEpoch < motorStop)) {
708+
if (!motor.isRunning && (tmpMillis > motorStart) && (tmpMillis <= motorStop)) {
689709
digitalWrite(motorOnPin, HIGH);
690710
motor.isRunning = true;
691711
newevent(g_msIntoTrial, "startMotor", trial.currentEpoch);
692-
} else if (motor.isRunning && (g_msIntoEpoch > motorStop)) {
712+
//} else if (motor.isRunning && (g_msIntoEpoch > motorStop)) {
713+
} else if (motor.isRunning && (tmpMillis <= motorStart)) {
693714
digitalWrite(motorOnPin, LOW);
694715
motor.isRunning = false;
695716
newevent(g_msIntoTrial, "stopMotor", trial.currentEpoch);
@@ -725,7 +746,15 @@ void updateMotor(unsigned long now) {
725746
} else {
726747
// this should not effect arduino code (it is just visual output)
727748
// treadmill.py WILL receive a motor off event and log it !!!
728-
digitalWrite(motorOnPin, LOW);
749+
750+
// motor is free to spin when not running a trial
751+
//digitalWrite(motorOnPin, LOW);
752+
// motor is locked when not running a trial
753+
if (trial.betweenTrial == "Locked") {
754+
digitalWrite(motorResetPin, HIGH);
755+
} else if (trial.betweenTrial == "Free") {
756+
digitalWrite(motorResetPin, LOW);
757+
}
729758
}
730759
}
731760

@@ -764,7 +793,7 @@ unsigned long now;
764793
/////////////////////////////////////////////////////////////
765794
void loop()
766795
{
767-
now = millis();
796+
now = millis(); // returns unsigned long
768797

769798
//
770799
//process trial and frame triggers

0 commit comments

Comments
 (0)