openjscad-aurora-webapp
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.
16function main(params)17{
18// the servo motor solid:19var servo = servoMotor();20
21// the plate:22var 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:25plate.properties.servoConnector = new CSG.Connector(26[0, 0, 4], // point27[0, 0, 1], // axis: pointing upwards28[2.5, 1.1, 0] // normal: use some random vector in the z plane29);30
31// Do some random rotations:32plate = plate.rotateX(25);33plate = 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 servoConnector37// point that we decided on earlier:38servo = servo.connectTo(39servo.properties.servomotor.surfaceConnector, // the servo's pre defined Connector40plate.properties.servoConnector, // the connector on the surface41false, // we don't want to mirror; the Connector's axes should point in the same direction420 // normalrotation; we could use it to rotate the servo in the plane43);44
45// Construct the result; use the parameters set by the end user:46var result = new CSG();47if(params.cutout == 1) plate = plate.subtract(servo.properties.servomotor.cutout);48if(params.showplate == 1) result = result.union(plate);49if(params.showservo == 1) result = result.union(servo);50if(params.showcutout == 1) result = result.union(servo.properties.servomotor.cutout.setColor(0,0.8,0));51
52return result;53}
54
55// Here we define the user editable parameters:
56function getParameterDefinitions() {57return [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
69function servoMotor()70{
71var width = 20;72var length = 40.5;73var h1 = 26.5;74var h2 = 29;75var h4 = 35.9;76var l1 = 55.4;77var w1 = 17.8;78var holeradius = 2.1;79var holex1 = 2 * 2.54;80var holey1 = 9.5 * 2.54;81var cutoutmargin = 1;82var shaftradius = 3;83var shafty = 9.25;84var shafttop = 43;85var cyl2radius = 7;86var cyl2height = 2;87
88var resolution = 16; // for all circular objects89
90// result: this is the solid in which we will store the servomotor91var 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 holes96// from another solid97var 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:101result.properties.servomotor = new CSG.Properties();102result.properties.servomotor.bottomface = result.properties.cube.facecenters[5];103
104// get the z coordinate of the bottom face:105var bottomz = result.properties.servomotor.bottomface.point.z;106
107// the tabs at the end containing the screw holes:108var cube2 = CSG.cube({radius:[w1/2, l1/2, (h2-h1)/2]});109cube2 = cube2.translate([0, 0, (h2-h1)/2 + bottomz + h1]);110result = result.union(cube2);111
112// create the cylinders for cutting out the screw holes:113for(var hole = 0; hole < 4; hole++)114{115var xoffset = (hole & 1)? holex1 : -holex1;116var yoffset = (hole & 2)? holey1 : -holey1;117var cylstart = new CSG.Vector3D([xoffset, yoffset, bottomz+h2]);118var cylend = new CSG.Vector3D([xoffset, yoffset, bottomz]);119var cutoutcylinder = CSG.cylinder({start: cylstart, end: cylend, radius: holeradius, resolution: resolution});120
121// create the screw hole in the tabs:122result = result.subtract(cutoutcylinder);123
124// And also add the cutout cylinder to the cutout shape:125cutout = cutout.union(cutoutcylinder);126}127
128// cylinder at top:129var p1 = new CSG.Vector3D([0, shafty, bottomz+h4]);130p2 = p1.plus(new CSG.Vector3D([0, 0, cyl2height]));131var cyl = CSG.cylinder({start: p1, end: p2, radius: cyl2radius, resolution: resolution});132result = result.union(cyl);133
134// make the entire motor grey:135result = result.setColor(0.2,0.2,0.2);136
137// create the shaft:138p1 = new CSG.Vector3D([0, shafty, bottomz+h4]);139p2 = p1.plus(new CSG.Vector3D([0, 0, cyl2height]));140var shaft = CSG.cylinder({start: [0, shafty, bottomz+h4], end: [0, shafty, bottomz+shafttop], radius: shaftradius, resolution: resolution});141shaft = shaft.setColor(1,1,1);142result = result.union(shaft);143
144// add the cutout solid to the properties:145result.properties.servomotor.cutout = cutout;146
147// Add a Connector to facilitate proper alignment of the servo motor to a surface148// The connector's point is at the x/y center of the box, in the bottom plane of the tabs149// The connector's axis points towards the top of the box150// The connector's normal points towards one of the tabs at the side151result.properties.servomotor.surfaceConnector = new CSG.Connector(152[0, 0, bottomz+h1], // point153[0, 0, 1], // axis154[0, 1, 0] // normal155);156
157return result;158}
159
160