openjscad-aurora-webapp

Форк
0
158 строк · 6.6 Кб
1
// title: Servo Motor
2
// author: Joost Nieuwenhuijse
3
// license: MIT License
4

5
// This demo intends to show how to use properties and connectors.
6
// The servoMotor() function constructs the shape of a standard servo
7
// It also defines a property for the cutout shape, and a connector property
8
// which can be used to correctly attach the motor to a surface.
9
//
10
// By using connectors, we don't need to know the actual orientation of each
11
// object. Instead we just attach the two objects by attaching their connectors.
12
//
13
// The cutout shape is automatically transformed with every transformation of
14
// the servo. We can simply subtract it from an object to make space for the servo
15
// motor. 
16
function main(params)
17
{
18
  // the servo motor solid:
19
  var servo = servoMotor();  
20
  
21
  // the plate:
22
  var plate = CSG.cube({radius: [40,40,4]});
23
  
24
  // Define a Connector on the plate, at the place where we want to attach the servo:
25
  plate.properties.servoConnector = new CSG.Connector(
26
    [0, 0, 4],    // point
27
    [0, 0, 1],    // axis: pointing upwards
28
    [2.5, 1.1, 0] // normal: use some random vector in the z plane
29
  );
30

31
  // Do some random rotations:
32
  plate = plate.rotateX(25);
33
  plate = plate.rotateZ(10);
34
  // now we really don't know the orientation of the plane anymore!
35

36
  // Still we can perfectly align the servo motor to the plane at the servoConnector
37
  // point that we decided on earlier: 
38
  servo = servo.connectTo(
39
    servo.properties.servomotor.surfaceConnector,   // the servo's pre defined Connector
40
    plate.properties.servoConnector,                // the connector on the surface
41
    false,                                          // we don't want to mirror; the Connector's axes should point in the same direction 
42
    0                                               // normalrotation; we could use it to rotate the servo in the plane
43
  );  
44
  
45
  // Construct the result; use the parameters set by the end user:
46
  var result = new CSG();
47
  if(params.cutout == 1) plate = plate.subtract(servo.properties.servomotor.cutout);
48
  if(params.showplate == 1) result = result.union(plate);
49
  if(params.showservo == 1) result = result.union(servo);
50
  if(params.showcutout == 1) result = result.union(servo.properties.servomotor.cutout.setColor(0,0.8,0));
51

52
  return result;
53
}
54

55
// Here we define the user editable parameters: 
56
function getParameterDefinitions() {
57
  return [
58
    { name: 'showservo', caption: 'Show servo:', type: 'choice', values: [0, 1], initial: 1, captions: ["No", "Yes"]},
59
    { name: 'showplate', caption: 'Show plate:', type: 'choice', values: [0, 1], initial: 1, captions: ["No", "Yes"]},
60
    { name: 'showcutout', caption: 'Show cutout:', type: 'choice', values: [0, 1], initial: 0, captions: ["No", "Yes"]},
61
    { name: 'cutout', caption: 'Subtract the servo cutout shape from the plate:', type: 'choice', values: [0, 1], initial: 1, captions: ["No", "Yes"]}
62
  ];
63
}
64

65
// The servoMotor() function constructs the shape of a standard '3003' servo 
66
// Returns a CSG solid. The solid has the following additional properties:
67
//    .properties.servomotor.cutout: a CSG solid that can be used to create a cutout for the servo motor (including screw holes)
68
//    .properties.servomotor.surfaceConnector: a CSG.Connector that can be used to attach the servo motor to a surface
69
function servoMotor()
70
{
71
  var width = 20;
72
  var length = 40.5;
73
  var h1 = 26.5;
74
  var h2 = 29;
75
  var h4 = 35.9;
76
  var l1 = 55.4;
77
  var w1 = 17.8;  
78
  var holeradius = 2.1;
79
  var holex1 = 2 * 2.54;
80
  var holey1 = 9.5 * 2.54;
81
  var cutoutmargin = 1;  
82
  var shaftradius = 3;
83
  var shafty = 9.25;
84
  var shafttop = 43; 
85
  var cyl2radius = 7;
86
  var cyl2height = 2;
87
  
88
  var resolution = 16; // for all circular objects
89
  
90
  // result: this is the solid in which we will store the servomotor
91
  var result = CSG.cube({radius:[width/2, length/2, h4/2]});
92
  
93
  // cutout: this is the solid for the cutout. It is never rendered directly,
94
  // but it will be returned as a property of the resulting solid.
95
  // it can be used to cutout the shape of the servo motor and screw holes
96
  // from another solid
97
  var cutout = CSG.cube({radius:[width/2+cutoutmargin, length/2+cutoutmargin, h4/2+cutoutmargin]}); 
98

99
  // add a 'bottomface' property. Since the cube already has predifined connectors at each face,
100
  // we can just copy the 5th:
101
  result.properties.servomotor = new CSG.Properties();
102
  result.properties.servomotor.bottomface = result.properties.cube.facecenters[5];
103

104
  // get the z coordinate of the bottom face:
105
  var bottomz = result.properties.servomotor.bottomface.point.z;
106
  
107
  // the tabs at the end containing the screw holes:
108
  var cube2 = CSG.cube({radius:[w1/2, l1/2, (h2-h1)/2]});
109
  cube2 = cube2.translate([0, 0, (h2-h1)/2 + bottomz + h1]);
110
  result = result.union(cube2);
111
  
112
  // create the cylinders for cutting out the screw holes:
113
  for(var hole = 0; hole < 4; hole++)
114
  {
115
    var xoffset = (hole & 1)? holex1 : -holex1;
116
    var yoffset = (hole & 2)? holey1 : -holey1;
117
    var cylstart = new CSG.Vector3D([xoffset, yoffset, bottomz+h2]); 
118
    var cylend = new CSG.Vector3D([xoffset, yoffset, bottomz]); 
119
    var cutoutcylinder = CSG.cylinder({start: cylstart, end: cylend, radius: holeradius, resolution: resolution});
120
    
121
    // create the screw hole in the tabs:
122
    result = result.subtract(cutoutcylinder);
123

124
    // And also add the cutout cylinder to the cutout shape:
125
    cutout = cutout.union(cutoutcylinder);
126
  }
127
  
128
  // cylinder at top:
129
  var p1 = new CSG.Vector3D([0, shafty, bottomz+h4]);
130
  p2 = p1.plus(new CSG.Vector3D([0, 0, cyl2height]));
131
  var cyl = CSG.cylinder({start: p1, end: p2, radius: cyl2radius, resolution: resolution});
132
  result = result.union(cyl);
133
  
134
  // make the entire motor grey:
135
  result = result.setColor(0.2,0.2,0.2);
136
  
137
  // create the shaft:
138
  p1 = new CSG.Vector3D([0, shafty, bottomz+h4]);
139
  p2 = p1.plus(new CSG.Vector3D([0, 0, cyl2height]));
140
  var shaft = CSG.cylinder({start: [0, shafty, bottomz+h4], end: [0, shafty, bottomz+shafttop], radius: shaftradius, resolution: resolution});
141
  shaft = shaft.setColor(1,1,1);
142
  result = result.union(shaft);
143
  
144
  // add the cutout solid to the properties:
145
  result.properties.servomotor.cutout = cutout;
146
  
147
  // Add a Connector to facilitate proper alignment of the servo motor to a surface
148
  //   The connector's point is at the x/y center of the box, in the bottom plane of the tabs 
149
  //   The connector's axis points towards the top of the box
150
  //   The connector's normal points towards one of the tabs at the side
151
  result.properties.servomotor.surfaceConnector = new CSG.Connector(
152
    [0, 0, bottomz+h1],    // point
153
    [0, 0, 1],             // axis
154
    [0, 1, 0]              // normal
155
  );
156
  
157
  return result;
158
}
159

160

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.