Skip to content

Commit

Permalink
[fossasia#509] Robotic arm config saving feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Pipe-Runner committed Aug 29, 2019
1 parent 2af5d90 commit 028042a
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 29 deletions.
18 changes: 16 additions & 2 deletions background_tasks/playback.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

loadBalancer.job(
ipcRenderer,
'linker',
'playback',
() => {
loadBalancer.onRecieveData(ipcRenderer, 'linker', args => {
loadBalancer.onRecieveData(ipcRenderer, 'playback', args => {
pyshell.send(JSON.stringify(args));
});

Expand All @@ -24,6 +24,20 @@

pyshell.on('message', function(results) {
log.info(results);
try {
const parsedJSON = JSON.parse(results);
switch (parsedJSON['type']) {
case 'FETCH_ROB_ARM':
ipcRenderer.send('FETCH_ROB_ARM');
break;
case 'DATA_WRITING_STATUS':
ipcRenderer.send('DATA_WRITING_STATUS', parsedJSON);
break;
}
}
catch (error) {
log.error(error);
}
});
},
() => {
Expand Down
4 changes: 4 additions & 0 deletions public/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,7 @@ ipcMain.on('CONNECTION_STATUS', (event, args) => {
ipcMain.on('DATA_WRITING_STATUS', (event, args) => {
mainWindow.webContents.send('DATA_WRITING_STATUS', args);
});

ipcMain.on('FETCH_ROB_ARM', (event, args) => {
mainWindow.webContents.send('FETCH_ROB_ARM', args);
});
41 changes: 21 additions & 20 deletions scripts/file_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ def update_buffer(self, inst_type, **kwargs):
yData = ' '.join(map(str, kwargs['yData'].tolist()))
data = str(kwargs['timestamp']) + ", " + str(kwargs['datetime']) + ", " + \
kwargs['channel'] + ", " + xData + ", " + \
yData + ", " + str(kwargs['timebase']) + ", " + " " + ", " + " " +"\n"
yData + ", " + str(kwargs['timebase']) + \
", " + " " + ", " + " " + "\n"

if inst_type == 'MUL_MET':
data = str(kwargs['timestamp']) + ", " + str(kwargs['datetime']) + ", " + \
kwargs['data'] + ", " + str(kwargs['value']) + ", " + " " + ", " + " " + "\n"
kwargs['data'] + ", " + \
str(kwargs['value']) + ", " + " " + ", " + " " + "\n"

self.data_buffer.append(data)

Expand Down Expand Up @@ -63,7 +65,7 @@ def save_config(self, data_path, device_type, **kwargs):
str(kwargs['p2']) + ", " + str(kwargs['dc_2']) + ", " + \
str(kwargs['p3']) + ", " + str(kwargs['dc_3']) + ", " + \
str(kwargs['p4']) + ", " + str(kwargs['dc_4']) + ", " + \
" " + ", " + " " + "\n"
" " + ", " + " " + "\n"
buffer.append(data)
self.file_pointed.writelines(buffer)

Expand All @@ -73,7 +75,8 @@ def save_config(self, data_path, device_type, **kwargs):
"Timestamp, DateTime, PCS, PV1, PV2, PV3, Latitude, Longitude \n")
data = str(kwargs['timestamp']) + ", " + str(kwargs['datetime']) + ", " + \
str(kwargs['pcs']) + ", " + str(kwargs['pv1']) + ", " + \
str(kwargs['pv2']) + ", " + str(kwargs['pv3']) + ", " + " " + "," + " " + "\n"
str(kwargs['pv2']) + ", " + str(kwargs['pv3']) + \
", " + " " + "," + " " + "\n"
buffer.append(data)
self.file_pointed.writelines(buffer)

Expand Down Expand Up @@ -106,6 +109,20 @@ def get_config_from_file(self, data_path, device_type):
"latitude": " ",
"longitude": " "
}
if device_type == 'PowerSource':
f = open(data_path, "r")
lines = f.readlines()
data = lines[3].split(',')
output = {'type': 'GET_CONFIG_PWR_SRC',
'pcs': data[2],
'pv1': data[3],
'pv2': data[4],
'pv3': data[5],
'latitude': " ",
'longitude': " "
}
print(json.dumps(output))
sys.stdout.flush()
print(json.dumps(output))
sys.stdout.flush()
print(json.dumps(output))
Expand All @@ -120,19 +137,3 @@ def stop_recording(self):
print(json.dumps({'type': 'DATA_WRITING_STATUS',
'message': 'Data recording stopped', }))
sys.stdout.flush()

def get_config_from_file(self, data_path, device_type):
if device_type == 'PowerSource':
f = open(data_path, "r")
lines = f.readlines()
data = lines[3].split(',')
output = {'type': 'GET_CONFIG_PWR_SRC',
'pcs': data[2],
'pv1': data[3],
'pv2': data[4],
'pv3': data[5],
'latitude': " ",
'longitude': " "
}
print(json.dumps(output))
sys.stdout.flush()
20 changes: 16 additions & 4 deletions scripts/playback_bridge.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
import sys
import time
import json
from playback_robot import PlaybackRobot

playbackRobot = PlaybackRobot()


def main():
print('script started')
sys.stdout.flush()
while(True):
in_stream_data = input()
parsed_stream_data = json.loads(in_stream_data)
command = parsed_stream_data['command']

# # -------------------------- Script termination block ----------------------------
if command == 'SAVE_CONFIG_ROB_ARM':
output = {'type': 'FETCH_ROB_ARM'}
print(json.dumps(output))
sys.stdout.flush()

if command == 'WRITE_ROB_ARM':
servo1 = parsed_stream_data['servo1']
servo2 = parsed_stream_data['servo2']
servo3 = parsed_stream_data['servo3']
servo4 = parsed_stream_data['servo4']
data_path = parsed_stream_data['dataPath']
playbackRobot.write(data_path, servo1, servo2, servo3, servo4)

if command == 'KILL':
exit()


if __name__ == '__main__':
main()
print("app exited successfully")
sys.stdout.flush()
22 changes: 22 additions & 0 deletions scripts/playback_robot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import time
import datetime
import sys
import json


class PlaybackRobot:
def write(self, data_path, servo1, servo2, servo3, servo4):
file_name = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S:000")
file_pointed = open(data_path + '/' + file_name + '.csv', "w+")
file_pointed.write("%s, %s \n\n" % (
'RobotArm', str(datetime.datetime.now())))
file_pointed.write(
"Time, Servo1, Servo2, Servo3, Servo4, Latitude, Longitude\n")
for i in range(len(servo1)):
file_pointed.write(
"%s, %s, %s, %s, %s\n" % (
str(i + 1), str(servo1[i]), str(servo2[i]), str(servo3[i]), str(servo4[i])))
file_pointed.close()
print(json.dumps({'type': 'DATA_WRITING_STATUS',
'message': 'Config saved', }))
sys.stdout.flush()
2 changes: 1 addition & 1 deletion src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ class App extends Component {
/>
<Route
path="/robotarm"
render={props => <RobotArm {...props} />}
render={props => <RobotArm {...props} dataPath={dataPath} />}
/>
<Route
path="/sensors"
Expand Down
17 changes: 17 additions & 0 deletions src/components/Appshell/Appshell.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,23 @@ const Appshell = ({
<SaveIcon />
</IconButton>
);
case '/robotarm':
return (
<IconButton
className={classes.iconButton}
size="medium"
onClick={() => {
loadBalancer.start(ipcRenderer, 'playback');
setTimeout(() => {
loadBalancer.sendData(ipcRenderer, 'playback', {
command: 'SAVE_CONFIG_ROB_ARM',
});
}, 500);
}}
>
<SaveIcon />
</IconButton>
);
case '/multimeter':
return isWriting ? (
<IconButton
Expand Down
2 changes: 2 additions & 0 deletions src/screen/LoggedData/LoggedData.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ class LoggedData extends Component {
return <PowerSourceIcon color="red" size={'2em'} />;
case 'Multimeter':
return <MultimeterIcon color="red" size={'2em'} />;
case 'RobotArm':
return <MultimeterIcon color="red" size={'2em'} />;
default:
break;
}
Expand Down
2 changes: 0 additions & 2 deletions src/screen/Oscilloscope/Oscilloscope.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ class Oscilloscope extends Component {
super(props);
this.timeBaseList = [0.5, 1, 5, 10, 20, 25, 50];
this.state = {
// isWriting: false,
// isReading: false,
timeBaseIndex: 0,
activeChannels: {
ch1: true,
Expand Down
22 changes: 22 additions & 0 deletions src/screen/RobotArm/RobotArm.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,28 @@ class RobotArm extends Component {
this.timer = null;
}

componentDidMount() {
ipcRenderer.on('FETCH_ROB_ARM', (event, args) => {
const { servo1, servo2, servo3, servo4 } = this.state;
const { dataPath } = this.props;
loadBalancer.sendData(ipcRenderer, 'playback', {
command: 'WRITE_ROB_ARM',
servo1,
servo2,
servo3,
servo4,
dataPath,
});
setTimeout(() => {
loadBalancer.stop(ipcRenderer, 'playback');
}, 500);
});
}

componentWillUnmount() {
ipcRenderer.removeAllListeners('FETCH_ROB_ARM');
}

sendCommand = () => {
const { active, timeLine, servo1, servo2, servo3, servo4 } = this.state;
if (timeLine < 60) {
Expand Down

0 comments on commit 028042a

Please sign in to comment.