-
Notifications
You must be signed in to change notification settings - Fork 1
/
CRAMtool.m
6351 lines (5840 loc) · 238 KB
/
CRAMtool.m
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function varargout = CRAMtool(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @CRAMtool_OpeningFcn, ...
'gui_OutputFcn', @CRAMtool_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
function CRAMtool_OpeningFcn(hObject, eventdata, handles, varargin)
% Front panel image
try
axes(handles.lab)
data = load('images.mat');
handles.img_parameter = data.img_parameter;
logo_Lab = data.img_logo;
image(logo_Lab)
axis off
grid off
set(handles.lab, 'Box', 'off')
handles.lab.XRuler.Axle.LineStyle = 'none';
handles.lab.YRuler.Axle.LineStyle = 'none';
catch
errordlg('images.mat not found')
return
end
axes(handles.axes)
% Simulation parameters
handles.num = 1e15; % starting force scale
handles.derivative_factor = 1e7; % Derivative factor, i.e. incdir = fabsize/derivative_factor
handles.targetnum = 10;
% Tensile Young's Modulus
handles.Et=0.55e9;
% Compressive Young's Modulus
handles.Ec=0.27e9;
% Tensile Yield Stress
handles.sigmat=27e6;
% Compressive Yield Stress
handles.sigmac=24e6;
% Density
handles.dens=2159;
% Poisson's Ratio
handles.v=0.46;
% Static Coefficient of Friction
handles.fric=0.07;
handles.defaults = [];
handles.defaultsSet = false;
% GUI operating parameters
handles.CRAMlocs = [];
handles.LocMat = [];
handles.Grounds = [];
handles.groundvec = [];
handles.Connections = [];
handles.PitchRadius = [];
handles.PitchCRAM = [];
handles.nf = [];
handles.fmag = [];
handles.dirMtx = [];
handles.nm = [];
handles.mmag = [];
handles.Frames = str2double(get(handles.step7_editFrames, 'String'));
% GUI display parameters
handles.step = 1;
handles.shadowOn = [0 0.447 0.741];
handles.shadowOff = [0.7 0.7 0.7];
handles.doneColor = [0 0.44 0.74];
handles.previousColor = [0.85 0.32 0.1];
handles.ForceColor = [0 0 1];
handles.MomentColor = [0 0 0];
handles.GravColor = [0 0.44 0.74];
handles.LineWidth = 2;
handles.LastGIF = [];
handles.text_force = [];
handles.text_moment = [];
handles.GIF.delay = 0.05;
handles.GIF.loopCount = inf;
set(handles.step1_done, 'ForegroundColor', handles.doneColor)
set(handles.step2_done, 'ForegroundColor', handles.doneColor)
set(handles.step3_done, 'ForegroundColor', handles.doneColor)
set(handles.step4_done, 'ForegroundColor', handles.doneColor)
set(handles.step5_done, 'ForegroundColor', handles.doneColor)
set(handles.step6_done, 'ForegroundColor', handles.doneColor)
set(handles.step7_done, 'ForegroundColor', handles.doneColor)
set(handles.step2_previous, 'ForegroundColor', handles.previousColor)
set(handles.step3_previous, 'ForegroundColor', handles.previousColor)
set(handles.step4_previous, 'ForegroundColor', handles.previousColor)
set(handles.step5_previous, 'ForegroundColor', handles.previousColor)
set(handles.step6_previous, 'ForegroundColor', handles.previousColor)
set(handles.step7_previous, 'ForegroundColor', handles.previousColor)
set(handles.step8_previous, 'ForegroundColor', handles.previousColor)
UpdateAxes(handles);
handles = UpdateDisplay(hObject, handles, handles.step);
% This code allows key presses (like 'l' or 'g' to be pressed even if the
% main GUI is not selected (such as when a button is in focus)
p = get(hObject, 'Children');
for k = 2:length(p)
pp = get(p(k),'Children');
for j = 1:length(pp)
if ~isempty(get(pp(j),'Tag'))
if isprop(pp(j),'KeyPressFcn')
tag = get(pp(j),'Tag');
str = ['handles. ' tag];
fcnstr = '@(hObject,eventdata)CRAMtool(''figure1_KeyPressFcn'',hObject,eventdata,guidata(hObject))';
set(eval(str),'KeyPressFcn',eval(fcnstr))
end
if isprop(pp(j), 'Units')
tag = get(pp(j),'Tag');
str = ['handles. ' tag];
set(eval(str),'Units','pixels')
end
end
end
end
set(handles.view_x,'KeyPressFcn',[])
set(handles.view_y,'KeyPressFcn',[])
set(handles.view_d,'KeyPressFcn',[])
set(handles.step7_editFrames,'KeyPressFcn',[])
% Choose default command line output for CRAMtool
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
function varargout = CRAMtool_OutputFcn(hObject, eventdata, handles)
% Get default command line output from handles structure
varargout{1} = handles.output;
%% GUI operation
function handles = simulation_settings_ClickedCallback(hObject, eventdata, handles)
defaults{1} = num2str(handles.derivative_factor);
defaults{2} = num2str(handles.targetnum);
str = {'Derivative factor: Make this number as large as possible without causing a singular matrix error.', ...
'Step-size factor: The larger this number is, the more accurate the simulation will be but the longer it will take to run.'};
Answer = inputdlg(str, 'Simulation settings', 1, defaults);
if isempty(Answer); return; end
handles.derivative_factor = str2double(Answer{1});
handles.targetnum = str2double(Answer{2});
guidata(hObject, handles)
function menu_saveCRAM_ClickedCallback(hObject, eventdata, handles)
[filename, pathname] = uiputfile('*.mat', 'Pick .mat file');
if isequal(filename, 0) || isequal(pathname, 0) % Cancelled
return
end
savefile = fullfile(pathname, filename);
if ~isempty(handles.CRAMlocs) && ~isempty(handles.Connections)
InputLoc = handles.LocMat(:,2:3);
Grounds = handles.groundvec;
elseif ~isempty(handles.CRAMlocs)
InputLoc = handles.CRAMlocs;
Grounds = handles.Grounds;
else
return
end
InputConnect = handles.Connections;
[FOV(1), FOV(2), FOV(3)] = GetAxes(handles);
save(savefile, 'InputLoc', 'Grounds', 'InputConnect', 'FOV')
function figure1_KeyPressFcn(hObject, eventdata, handles)
key = eventdata.Key;
switch key
case 'n'
if handles.step == 1
handles = step1_newCRAMclick_Callback(hObject, eventdata, handles);
elseif handles.step == 2
handles = step2_newGround_Callback(hObject, eventdata, handles);
elseif handles.step == 3
handles = step3_newConnection_Callback(hObject, eventdata, handles);
end
case 'r'
if handles.step == 1
handles = step1_removeCRAM_Callback(hObject, eventdata, handles);
elseif handles.step == 2
handles = step2_removeGround_Callback(hObject, eventdata, handles);
elseif handles.step == 3
handles = step3_removeConnection_Callback(hObject, eventdata, handles);
elseif handles.step == 6
handles = step6_remove_Callback(hObject, eventdata, handles);
end
case 'equal'
value = str2double(get(handles.view_d, 'String'));
set(handles.view_d, 'String', num2str(value / 1.1))
UpdateAxes(handles);
case 'hyphen'
value = str2double(get(handles.view_d, 'String'));
set(handles.view_d, 'String', num2str(value * 1.1))
UpdateAxes(handles);
case 'uparrow'
pos = str2double(get(handles.view_y, 'String'));
range = str2double(get(handles.view_d, 'String'));
set(handles.view_y, 'String', num2str(pos + 0.1*range))
UpdateAxes(handles);
case 'downarrow'
pos = str2double(get(handles.view_y, 'String'));
range = str2double(get(handles.view_d, 'String'));
set(handles.view_y, 'String', num2str(pos - 0.1*range))
UpdateAxes(handles);
case 'leftarrow'
pos = str2double(get(handles.view_x, 'String'));
range = str2double(get(handles.view_d, 'String'));
set(handles.view_x, 'String', num2str(pos - 0.1*range))
UpdateAxes(handles);
case 'rightarrow'
pos = str2double(get(handles.view_x, 'String'));
range = str2double(get(handles.view_d, 'String'));
set(handles.view_x, 'String', num2str(pos + 0.1*range))
UpdateAxes(handles);
case 'return'
switch handles.step
case 1; handles = step1_done_Callback(hObject, eventdata, handles);
case 2; handles = step2_done_Callback(hObject, eventdata, handles);
case 3; handles = step3_done_Callback(hObject, eventdata, handles);
case 4; handles = step4_done_Callback(hObject, eventdata, handles);
case 5; handles = step5_done_Callback(hObject, eventdata, handles);
case 6; handles = step6_done_Callback(hObject, eventdata, handles);
case 7; handles = step7_done_Callback(hObject, eventdata, handles);
end
case 'backspace'
switch handles.step
case 2; handles = step2_previous_Callback(hObject, eventdata, handles);
case 3; handles = step3_previous_Callback(hObject, eventdata, handles);
case 4; handles = step4_previous_Callback(hObject, eventdata, handles);
case 5; handles = step5_previous_Callback(hObject, eventdata, handles);
case 6; handles = step6_previous_Callback(hObject, eventdata, handles);
case 7; handles = step7_previous_Callback(hObject, eventdata, handles);
case 8; handles = step8_previous_Callback(hObject, eventdata, handles);
end
end
guidata(hObject, handles)
function handles = UpdateInstructions(handles, step)
switch step
case 1
str = {'Define design window parameters using "Set axis limits".';
' - "Center:" defines the central point in the design window.';
' - "Field-of-view (zoom):" defines the range of the axes.';
' ';
'Adjust settings in "1. Add circular cams" to place new cams.';
' - Select cam locations using "Click to add". (alt.: n key)';
' - Enter exact cam locations using "By value".';
' - Upload .mat file of desired locations using "Load .mat".';
' - Remove undesired cam locations using "Delete cam." (alt.: r key) '
' - Clear the entire design window using "Clear all".';
' - Click "Next" when you have finished placing the cams.'};
case 2
str = {'Define which of the previously placed cams are held fixed';
'using "2. Set grounds". Note that grounded cams are shown';
'red while all other cams are shown blue.';
' - Select "New ground (click)" to define grounded cams. (alt.: n key)';
' - Select "Remove ground" to un-ground a grounded cam. (alt.: r key)';
' - Click "Previous" to return to the last step.';
' - Click "Next" when you have finished grounding cams.'};
case 3
str = {'Define which cams should be joined together by flexure';
'straps using "3. Add connections".';
' - Click "New connection" and then click on the two cams';
' you''d like to join. A dotted black line will designate';
' connected cams. (alt.: n key)';
' - Click "Remove connection" to delete a joint. (alt.: r key)';
' - Click "Previous" to return to the last step.';
' - Click "Next" when you have finished joining the cams.'};
case 4
str = {'Define the pitch radius of a single circular cam using';
'"4. Specify pitch radius" and the remaining radii will be';
'calculated automatically.';
' - Click "Specify pitch radius" and then select the cam for';
' which you''d like to enter its pitch radius.';
' - Enter the pitch radius for that cam in the pop-up window.'
' - Click "Previous" to return to the last step.';
' - Click "Next" when you have finished defining cam radii.'};
case 5
str = {'Define geometric parameters and material properties using';
'"5. Geometry and materials".';
' - Click "Geometry" to specify the geometric parameters.';
' They are initially set to reasonable default values that';
' can be changed in the pop-up window as desired.';
' An image will fill the design window with the labeled';
' geometric parameters to clarify what they are.';
' - Click "Materials" to specify the material properties';
' of the consituent material. The default properties are';
' those of Teflon.';
' - Click "Previous" to return to the last step.';
' - Click "Next" when you are finished entering the values.'};
case 6
str = {'Define loads on the ungrounded cams using "6. Add loads".';
'Note that forces will always act at the cam centers and';
'will follow the cams as they move.';
' - Click "Force" or "Moment" to add a load. Be sure to click';
' near the center of the cam that you''d like to load.';
' Pop-up windows will guide you in selecting the '
' magnitudes and directions of these loads.'
' - Click "Remove" to delete every load on the cam that you';
' select except for gravity.';
' - Click "Gravity?" to load all the ungrounded cams in the';
' downward direction using Earth''s gravitational pull.';
' - Click "Previous" to return to the last step.';
' - Click "Next" when you are finished loading the cams.'};
case 7
str = {'You are now ready to simulated your CRAM design for the';
'loads specified. The simulation will generate an animation of';
'the CRAM moving. Select the number of frames you''d like ';
'this animation to contain using "Frames:" in '
'"7. Run simulation".';
' - Click "Simulate" to run the simulation.';
' - Click "Stop" to terminate the simulation.';
' - Click "Previous" to return to the last step.';
' - Click "Next" when you are finished loading the cams.'};
case 8
str = {'You can now see how each layer should be CADed with ';
'straight straps to enable the fabrication and assembly of ';
'your design using "8. CAD to fabricate".';
' - Click "View" to see the undefomed layers.'
' - Click "Previous" to return to the last step.'};
end
set(handles.text_instructions, 'String', str)
function handles = UpdateDisplay(hObject, handles, step, clear_all)
% Step 1
set(handles.panel_axes, 'ShadowColor', handles.shadowOff)
set(handles.panel_addCRAMs, 'ShadowColor', handles.shadowOff)
set(handles.step1_loadLocs, 'Enable', 'Off')
set(handles.step1_newCRAMclick, 'Enable', 'Off')
set(handles.step1_newCRAMvalue, 'Enable', 'Off')
set(handles.step1_done, 'Enable', 'Off')
set(handles.step1_removeCRAM, 'Enable', 'Off')
set(handles.step1_clearAll, 'Enable', 'Off')
% Step 2
set(handles.panel_grounds, 'ShadowColor', handles.shadowOff)
set(handles.step2_newGround, 'Enable', 'Off')
set(handles.step2_removeGround, 'Enable', 'Off')
set(handles.step2_done, 'Enable', 'Off')
set(handles.step2_previous, 'Enable', 'Off')
% Step 3
set(handles.panel_connections, 'ShadowColor', handles.shadowOff)
set(handles.step3_newConnection, 'Enable', 'Off')
set(handles.step3_removeConnection, 'Enable', 'Off')
set(handles.step3_done, 'Enable', 'Off')
set(handles.step3_previous, 'Enable', 'Off')
% Step 4
set(handles.panel_matPropSettings, 'ShadowColor', handles.shadowOff)
set(handles.step4_geometrySettings, 'Enable', 'Off')
set(handles.step4_materialSettings, 'Enable', 'Off')
set(handles.step4_done, 'Enable', 'Off')
set(handles.step4_previous, 'Enable', 'Off')
% Step 5
set(handles.panel_settings, 'ShadowColor', handles.shadowOff)
set(handles.step5_pitchRadius, 'Enable', 'Off')
set(handles.step5_done, 'Enable', 'Off')
set(handles.step5_previous, 'Enable', 'Off')
% Step 6
set(handles.panel_forces, 'ShadowColor', handles.shadowOff)
set(handles.step6_done, 'Enable', 'Off')
set(handles.step6_previous, 'Enable', 'Off')
set(handles.step6_addForce, 'Enable', 'Off')
set(handles.step6_addMoment, 'Enable', 'Off')
set(handles.step6_remove, 'Enable', 'Off')
set(handles.step6_gravity, 'Enable', 'Off')
% Step 7
set(handles.panel_simulate, 'ShadowColor', handles.shadowOff)
set(handles.step7_simulate, 'Enable', 'Off')
set(handles.step7_editFrames, 'Enable', 'Off')
set(handles.step7_previous, 'Enable', 'Off')
set(handles.step7_stop, 'Enable', 'Off')
set(handles.step7_done, 'Enable', 'Off')
% Step 8
set(handles.step8_undeformed, 'Enable', 'Off')
set(handles.panel_fabricate, 'ShadowColor', handles.shadowOff)
set(handles.step8_previous, 'Enable', 'Off')
if nargin < 4
switch step
case 1
set(handles.panel_axes, 'ShadowColor', handles.shadowOn)
set(handles.panel_addCRAMs, 'ShadowColor', handles.shadowOn)
set(handles.step1_loadLocs, 'Enable', 'On')
set(handles.step1_newCRAMclick, 'Enable', 'On')
set(handles.step1_newCRAMvalue, 'Enable', 'On')
set(handles.step1_done, 'Enable', 'On')
set(handles.step1_removeCRAM, 'Enable', 'On')
set(handles.text_status, 'String', 'Step 1: Add circular cams by specifying their centroids.')
set(handles.step1_clearAll, 'Enable', 'On')
case 2
set(handles.panel_grounds, 'ShadowColor', handles.shadowOn)
set(handles.step2_newGround, 'Enable', 'On')
set(handles.step2_removeGround, 'Enable', 'On')
set(handles.step2_done, 'Enable', 'On')
set(handles.step2_previous, 'Enable', 'On')
set(handles.text_status, 'String', 'Step 2: Select which cams should be grounded or held fixed.')
case 3
set(handles.panel_connections, 'ShadowColor', handles.shadowOn)
set(handles.step3_newConnection, 'Enable', 'On')
set(handles.step3_removeConnection, 'Enable', 'On')
set(handles.step3_done, 'Enable', 'On')
set(handles.step3_previous, 'Enable', 'On')
set(handles.text_status, 'String', 'Step 3: Define which cams should be connected together by flexure straps.')
case 4
set(handles.panel_settings, 'ShadowColor', handles.shadowOn)
set(handles.step5_pitchRadius, 'Enable', 'On')
set(handles.step5_done, 'Enable', 'On')
set(handles.step5_previous, 'Enable', 'On')
set(handles.text_status, 'String', 'Step 4: Define the pitch radii for every circular cam.')
case 5
set(handles.panel_matPropSettings, 'ShadowColor', handles.shadowOn)
set(handles.step4_geometrySettings, 'Enable', 'On')
set(handles.step4_materialSettings, 'Enable', 'On')
set(handles.step4_done, 'Enable', 'On')
set(handles.step4_previous, 'Enable', 'On')
set(handles.text_status, 'String', 'Step 5: Input geometric parameters and material properties.')
case 6
set(handles.panel_forces, 'ShadowColor', handles.shadowOn)
set(handles.step6_addForce, 'Enable', 'On')
set(handles.step6_addMoment, 'Enable', 'On')
set(handles.step6_remove, 'Enable', 'On')
set(handles.step6_done, 'Enable', 'On')
set(handles.step6_previous, 'Enable', 'On')
set(handles.step6_gravity, 'Enable', 'On')
set(handles.text_status, 'String', 'Step 6: Specify forces and moments on the desired cams.')
case 7
set(handles.panel_simulate, 'ShadowColor', handles.shadowOn)
set(handles.step7_simulate, 'Enable', 'On')
set(handles.step7_editFrames, 'Enable', 'On')
set(handles.step7_previous, 'Enable', 'On')
set(handles.panel_axes, 'ShadowColor', handles.shadowOn)
set(handles.text_status, 'String', 'Step 7: Simulate your design for the given loads to generate an animation.')
set(handles.step7_done, 'Enable', 'On')
case 8
set(handles.step8_undeformed, 'Enable', 'On')
set(handles.panel_fabricate, 'ShadowColor', handles.shadowOn)
set(handles.text_status, 'String', 'Step 8: View a CAD model of each undeformed layer so your design can be fabricated and assembled.')
set(handles.step8_previous, 'Enable', 'On')
end
end
handles = UpdateInstructions(handles, step);
guidata(hObject, handles)
function handles = drawSimple(hObject, handles)
try delete(handles.blue); end
try delete(handles.red); end
try
for k = 1:length(handles.black)
delete(handles.black{k})
end
end
try
for k = 1:length(handles.circle)
delete(handles.circle{k})
end
end
try
delete(handles.axes.Children)
end
if handles.step >= 3
x = handles.LocMat(:,2);
y = handles.LocMat(:,3);
grounds = handles.groundvec;
Connections = handles.Connections;
else
x = handles.CRAMlocs(:,1);
y = handles.CRAMlocs(:,2);
grounds = handles.Grounds;
Connections = handles.Connections;
end
hold off;
handles.blue = plot(handles.axes, x, y, 'bo');
hold on;
if ~isempty(grounds)
handles.red = plot(handles.axes, x(grounds), y(grounds), 'ro');
end
if ~isempty(Connections)
for k = 1:size(Connections,1)
I1 = Connections(k,1);
I2 = Connections(k,2);
handles.black{k} = plot(handles.axes, [x(I1), x(I2)], [y(I1), y(I2)], 'k--');
end
end
UpdateAxes(handles);
guidata(hObject, handles)
function handles = DrawCircle(handles, xc, yc, r)
theta = linspace(0,2*pi, 200)';
x = r*cos(theta) + xc;
y = r*sin(theta) + yc;
handles.circle{1} = plot(handles.axes, x, y, 'k');
function handles = DrawCircles(handles)
for k = 1:size(handles.LocMat,1)
handles.circle{k} = plaincircle(handles.LocMat(k,2), handles.LocMat(k,3), handles.LocMat(k,4));
end
UpdateAxes(handles);
function handles = drawPatchCRAM(handles)
FullJointMat = handles.FullJointMat;
LocMat = handles.LocMat;
jointnum = handles.jointnum;
C = handles.C;
boltr = handles.boltr;
t = handles.t;
W = handles.W;
del = handles.del;
dcut1 = handles.dcut1;
dcut2 = handles.dcut2;
O = handles.O;
Et = handles.Et;
Ec = handles.Ec;
v = handles.v;
fric = handles.fric;
if ~isempty(handles.text_force)
for k = 1:length(handles.text_force)
delete(handles.text_force{k})
end
handles.text_force = [];
end
if ~isempty(handles.text_moment)
for k = 1:length(handles.text_moment)
delete(handles.text_moment{k})
end
handles.text_moment = [];
end
drawCRAM(FullJointMat,LocMat,jointnum, C, boltr, t, W, dcut1, dcut2, Ec, v)
UpdateAxes(handles);
function handles = drawForces(handles)
axes(handles.axes)
hold on
xl = xlim;
yl = ylim;
xr = range(xl);
yr = range(yl);
drawscale = max([xr yr])/20;
if get(handles.step6_gravity, 'Value')
[~, ~, gravforce] = UpdateGravity(handles);
else
gravforce = [];
end
maxF = max([max(handles.fmag), max(gravforce)]);
if ~isempty(handles.fmag)
for k = 1:size(handles.fmag,1)
if handles.fmag(k) == 0; continue; end % Do not draw mag = 0
forcedir = handles.dirMtx(k,:);
x = handles.LocMat(handles.nf(k),2);
y = handles.LocMat(handles.nf(k),3);
str = ['F = ' num2str(handles.fmag(k)) ' N'];
scale = handles.fmag(k) / maxF;
scale = max([scale 0.1]);
xc = forcedir(1)*drawscale*3.5*scale;
yc = forcedir(2)*drawscale*3.5*scale;
t = text(x+xc, y+yc, str);
set(t, 'FontSize', 14)
set(t, 'Color', handles.ForceColor)
ext = get(t, 'Extent');
h = ext(4);
if yc < h/2
yc = yc + sign(yc)*h/2;
set(t, 'Position', [x+xc y+yc])
end
theta = atan2(forcedir(2), forcedir(1));
if cos(theta) > 1/2
set(t, 'HorizontalAlignment', 'Left')
elseif cos(theta) < -1/2
set(t, 'HorizontalAlignment', 'Right')
else
set(t, 'HorizontalAlignment', 'Center')
end
handles.text_force{k} = t;
q = quiver(x, y, forcedir(1)*drawscale*3*scale, forcedir(2)*drawscale*3*scale);
len = hypot(forcedir(1)*drawscale*3*scale, forcedir(2)*drawscale*3*scale);
set(q, 'Color', handles.ForceColor);
set(q, 'MaxHeadSize', drawscale/len*2)
set(q, 'MaxHeadSize', 1)
set(q, 'LineWidth', handles.LineWidth)
end
end
if ~isempty(handles.mmag)
for k = 1:size(handles.mmag,1)
if handles.mmag(k) == 0; continue; end % Do not draw mag = 0
x = handles.LocMat(handles.nm(k),2);
y = handles.LocMat(handles.nm(k),3);
str = ['M = ' num2str(abs(handles.mmag(k))) ' Nm'];
t = text(x, y-drawscale*1.3, str);
set(t, 'FontSize', 14)
set(t, 'Color', handles.MomentColor)
handles.text_moment{k} = t;
drawCircularArrow(handles, x, y, drawscale/1.5, sign(handles.mmag(k)))
end
end
% Check to see if gravity toggle is enabled
if get(handles.step6_gravity, 'Value')
for k = 1:length(gravforce)
F = gravforce(k);
forcedir = [0 -1];
x = handles.LocMat(k,2);
y = handles.LocMat(k,3);
str = ['F = ' num2str(F,3) ' N'];
scale = F/maxF;
scale = max([scale 0.1]);
xc = forcedir(1)*drawscale*3.5*scale;
yc = forcedir(2)*drawscale*3.5*scale;
t = text(x+xc, y+yc, str);
set(t, 'FontSize', 14)
set(t, 'Color', handles.GravColor)
ext = get(t, 'Extent');
h = ext(4);
if yc < h/2
yc = yc + sign(yc)*h/2;
set(t, 'Position', [x+xc y+yc])
end
set(t, 'HorizontalAlignment', 'Center')
handles.text_force{k} = t;
q = quiver(x, y, forcedir(1)*drawscale*3*scale, forcedir(2)*drawscale*3*scale);
len = hypot(forcedir(1)*drawscale*3*scale, forcedir(2)*drawscale*3*scale);
set(q, 'Color', handles.GravColor);
set(q, 'MaxHeadSize', drawscale/len*2)
set(q, 'LineWidth', handles.LineWidth)
end
end
function drawGrounds(handles)
x = handles.LocMat(:,2);
y = handles.LocMat(:,3);
grounds = handles.groundvec;
if ~isempty(grounds)
plot(handles.axes, x(grounds), y(grounds), 'ro');
end
function drawCircularArrow(handles, xc, yc, radius, dir)
% Circle
theta = linspace(pi/4,7*pi/4,200);
x = radius*cos(theta) + xc;
y = radius*sin(theta) + yc;
plot(x, y, 'color', handles.MomentColor, 'LineWidth', handles.LineWidth)
% Arrowhead
L = radius/2;
dir = sign(dir);
if dir == 1
xe = x(end);
ye = y(end);
x1 = [xe xe-L];
y1 = [ye ye];
x2 = [xe xe];
y2 = [ye ye-L];
else
xe = x(1);
ye = y(1);
x1 = [xe xe-L];
y1 = [ye ye];
x2 = [xe xe];
y2 = [ye ye+L];
end
plot(x1, y1, 'color', handles.MomentColor, 'LineWidth', handles.LineWidth)
plot(x2, y2, 'color', handles.MomentColor, 'LineWidth', handles.LineWidth)
function radius = SelectionRadius(handles)
dmax = str2double(get(handles.view_d,'String'));
radius = dmax/50;
function [D, Ddiag, Dtri] = DistMatrix(P1, P2)
n1 = size(P1,1);
n2 = size(P2,1);
D = zeros(n1, n2);
for r = 1:n1
for c = 1:n2
p1 = P1(r,:);
p2 = P2(c,:);
D(r,c) = sqrt( (p1(1)-p2(1))^2 + (p1(2)-p2(2))^2 );
end
end
if nargout >= 2 && n1 == n2
Ddiag = D;
for r = 1:n1
Ddiag(r,r) = NaN;
end
end
if nargout > 2 && n1 == n2
Dtri = triu(D);
Dtri(Dtri == 0) = NaN;
end
function toolbar_hotkeys_ClickedCallback(hObject, eventdata, handles)
str = {'CRAM Tool hotkeys: ';
' ';
'N: add new cam, ground, or connection (steps 1-3)';
'R: remove cam, ground, or connection (steps 1-3)';
' ';
'Arrow keys: pan the display area';
'+/- keys: zoom the display area';
' ';
'Return: proceed to next step';
'Backspace: return to previous step'};
msgbox(str,'CRAM Tool hotkeys')
function r = range(vec)
r = max(vec) - min(vec);
%% Step 1: Set axes and input CRAM locations
function UpdateAxes(handles)
xc = str2double(get(handles.view_x, 'String'));
yc = str2double(get(handles.view_y, 'String'));
d = str2double(get(handles.view_d, 'String'));
xMin = xc - d/2;
xMax = xc + d/2;
yMin = yc - d/2;
yMax = yc + d/2;
axis equal
xlim(handles.axes, [xMin xMax])
ylim(handles.axes, [yMin yMax])
xlabel('Meters')
function [xc, yc, d] = GetAxes(handles)
xc = str2double(get(handles.view_x, 'String'));
yc = str2double(get(handles.view_y, 'String'));
d = str2double(get(handles.view_d, 'String'));
function SetAxes(handles, xc, yc, d)
set(handles.view_x, 'String', num2str(xc))
set(handles.view_y, 'String', num2str(yc))
set(handles.view_d, 'String', num2str(d))
UpdateAxes(handles);
guidata(hObject, handles)
function bool = BoundCheck(handles, x, y)
% Make sure the user clicked in the box
xl = xlim(handles.axes);
yl = ylim(handles.axes);
bx = and(x>xl(1), x<xl(2));
by = and(y>yl(1), y<yl(2));
bool = and(bx, by);
function view_x_Callback(hObject, eventdata, handles)
UpdateAxes(handles);
function view_x_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function view_y_Callback(hObject, eventdata, handles)
UpdateAxes(handles);
function view_y_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function view_d_Callback(hObject, eventdata, handles)
UpdateAxes(handles);
function view_d_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function step1_loadLocs_Callback(hObject, eventdata, handles)
str = {'Use the Save Current CRAM Configuration icon in the GUI toolbar to automatically save a .mat file'};
uiwait(msgbox(str,'Load CRAM','modal'));
[filename, pathname] = uigetfile('*.mat', 'Pick .mat file');
if isequal(filename, 0) || isequal(pathname, 0) % Cancelled
return
end
data = load(fullfile(pathname, filename));
handles.CRAMlocs = data.InputLoc;
handles.CRAMlocs = bsxfun(@minus, handles.CRAMlocs, mean(handles.CRAMlocs));
xr = range(handles.CRAMlocs(:,1));
yr = range(handles.CRAMlocs(:,2));
r = max([xr yr]);
set(handles.view_d, 'String', num2str(3*r))
set(handles.text_status, 'String', ['Loaded: ' filename ', press Next to continue'])
try
handles.Grounds = data.Grounds;
catch
handles.Grounds = [];
end
try
handles.Connections = data.InputConnect;
catch
handles.Connections = [];
end
try
FOV = data.FOV;
SetAxes(handles, FOV(1), FOV(2), FOV(3));
end
handles = drawSimple(hObject, handles);
guidata(hObject, handles)
function handles = step1_newCRAMclick_Callback(hObject, eventdata, handles)
if ~isempty(handles.Connections)
Button = questdlg('Adding a new cam will delete existing connections','Connection warning','Proceed','Cancel','Proceed');
switch Button
case 'Proceed'
handles.Connections = [];
case 'Cancel'
return
end
end
handles = UpdateDisplay(hObject, handles, handles.step, 'clear');
[x, y] = cinput(1);
handles = UpdateDisplay(hObject, handles, handles.step);
bool = BoundCheck(handles, x, y); if ~bool; return; end
handles.CRAMlocs = [x, y; handles.CRAMlocs];
handles.Grounds = handles.Grounds + 1;
handles = drawSimple(hObject, handles);
guidata(hObject, handles)
function step1_newCRAMvalue_Callback(hObject, eventdata, handles)
if ~isempty(handles.Connections)
Button = questdlg('Adding a new cam will delete existing connections','Connection warning','Proceed','Cancel','Proceed');
switch Button
case 'Proceed'
handles.Connections = [];
case 'Cancel'
return
end
end
Answer = inputdlg({'X', 'Y'}, 'Enter new cam centroid location', 1, {'0', '0'});
if isempty(Answer); return; end
x = str2double(Answer{1});
y = str2double(Answer{2});
% handles.CRAMlocs(end+1,:) = [x, y];
handles.CRAMlocs = [x, y; handles.CRAMlocs];
handles.Grounds = handles.Grounds + 1;
if ~isempty(handles.Connections); handles.Connections = handles.Connections + 1; end
handles = drawSimple(hObject, handles);
guidata(hObject, handles)
function handles = step1_removeCRAM_Callback(hObject, eventdata, handles)
if ~isempty(handles.Connections)
Button = questdlg('Removing a cam will delete existing connections','Connection warning','Proceed','Cancel','Proceed');
switch Button
case 'Proceed'
handles.Connections = [];
case 'Cancel'
return
end
end
handles = UpdateDisplay(hObject, handles, handles.step, 'clear');
[x, y] = cinput(1);
handles = UpdateDisplay(hObject, handles, handles.step);
bool = BoundCheck(handles, x, y); if ~bool; return; end
D = DistMatrix([x y], handles.CRAMlocs);
[~,I] = min(D);
if D(I) < SelectionRadius(handles)
handles.CRAMlocs(I,:) = [];
handles.Grounds = [];
handles.groundvec = [];
handles.Connections = [];
handles.PitchRadius = [];
handles.PitchCRAM = [];
guidata(hObject, handles)
end
handles = drawSimple(hObject, handles);
function handles = step1_done_Callback(hObject, eventdata, handles)
if isempty(handles.CRAMlocs)
errordlg('No cams placed - define cam locations before proceeding');
return
elseif size(handles.CRAMlocs,1) < 2
errordlg('You must place at least two cams');
return
end
handles.step = 2;
handles = UpdateDisplay(hObject, handles, handles.step);
guidata(hObject, handles)
function step1_clearAll_Callback(hObject, eventdata, handles)
Button = questdlg('Are you sure you would like to clear all cam locations, connections, and grounds?', 'Clear all?', 'Confirm', 'Cancel', 'Cancel');
switch Button
case 'Confirm'
handles.CRAMlocs = zeros(0,2);
handles.LocMat = zeros(0,4);
handles.groundvec = [];
handles.Grounds = [];
handles.Connections = [];
handles.PitchRadius = [];
handles.PitchCRAM = [];
handles.nf = [];
handles.fmag = [];
handles.dirMtx = [];
handles.nm = [];
handles.mmag = [];
hold off;
handles = drawSimple(hObject, handles);
end
guidata(hObject, handles)
%% Step 2: Specify ground locations
function handles = step2_newGround_Callback(hObject, eventdata, handles)
if ~isempty(handles.Connections)
Button = questdlg('Adding a ground cam will delete existing connections','Connection warning','Proceed','Cancel','Proceed');
switch Button
case 'Proceed'
handles.Connections = [];
case 'Cancel'
return
end
end
handles = UpdateDisplay(hObject, handles, handles.step, 'clear');
[x, y] = cinput(1);
handles = UpdateDisplay(hObject, handles, handles.step);
bool = BoundCheck(handles, x, y); if ~bool; return; end
D = DistMatrix([x y], handles.CRAMlocs);
[~,I] = min(D);
if D(I) < SelectionRadius(handles)
handles.Grounds = [handles.Grounds, I];
handles.Grounds = unique(handles.Grounds);
handles = drawSimple(hObject, handles);
guidata(hObject, handles)
end
function handles = step2_removeGround_Callback(hObject, eventdata, handles)
if ~isempty(handles.Connections)
Button = questdlg('Removing a ground will delete existing connections','Connection warning','Proceed','Cancel','Proceed');
switch Button
case 'Proceed'
handles.Connections = [];
case 'Cancel'
return
end
end
handles = UpdateDisplay(hObject, handles, handles.step, 'clear');
[x, y] = cinput(1);
handles = UpdateDisplay(hObject, handles, handles.step);
bool = BoundCheck(handles, x, y); if ~bool; return; end
D = DistMatrix([x y], handles.CRAMlocs);
[~,I] = min(D);
if D(I) < SelectionRadius(handles)
handles.Grounds(handles.Grounds == I) = [];
handles.Connections = [];
guidata(hObject, handles)
end
handles = drawSimple(hObject, handles);
function handles = step2_done_Callback(hObject, eventdata, handles)
if isempty(handles.Grounds)
errordlg('No ground specified');
return
elseif length(handles.Grounds) == size(handles.CRAMlocs,1)
errordlg('Not all cams can be grounded');
return
end
handles.step = 3;
handles = UpdateDisplay(hObject, handles, handles.step);
handles = ChangeOrder(handles);
guidata(hObject, handles)
function handles = step2_previous_Callback(hObject, eventdata, handles)
handles.step = 1;