// ============================================
// SaltyLab — Motor Mount Plate
// 350×150×6mm PETG
// Mounts both 8" hub motors + 2020 extrusion spine
// ============================================
include <dimensions.scad>

plate_w = 350;   // Width (axle to axle direction)
plate_d = 150;   // Depth (front to back)
plate_h = 6;     // Thickness

// Motor axle positions (centered, symmetric)
motor_spacing = 280;  // Center-to-center axle distance

// Extrusion spine mount (centered, 2x M5 bolts)
spine_offset_y = 0;   // Centered front-to-back
spine_bolt_spacing = 60; // Two bolts along spine

// Motor clamp dimensions
clamp_w = 30;
clamp_h = 25;    // Height above plate for clamping axle
clamp_gap = motor_axle_dia + tol*2;  // Slot for axle
clamp_bolt_offset = 10; // M5 clamp bolt offset from center

module motor_clamp() {
    difference() {
        // Clamp block
        translate([-clamp_w/2, -clamp_w/2, 0])
            cube([clamp_w, clamp_w, plate_h + clamp_h]);
        
        // Axle hole (through, slightly oversized)
        translate([0, 0, plate_h + clamp_h/2 + 5])
            rotate([0, 90, 0])
                cylinder(d=clamp_gap, h=clamp_w+2, center=true, $fn=40);
        
        // Clamp slit (allows tightening)
        translate([0, 0, plate_h + clamp_h/2 + 5])
            cube([clamp_w+2, 1.5, clamp_h], center=true);
        
        // Clamp bolt holes (M5, horizontal through clamp ears)
        translate([0, clamp_bolt_offset, plate_h + clamp_h/2 + 5])
            rotate([0, 90, 0])
                cylinder(d=m5_clear, h=clamp_w+2, center=true, $fn=30);
        translate([0, -clamp_bolt_offset, plate_h + clamp_h/2 + 5])
            rotate([0, 90, 0])
                cylinder(d=m5_clear, h=clamp_w+2, center=true, $fn=30);
    }
}

module motor_mount_plate() {
    difference() {
        union() {
            // Main plate
            translate([-plate_w/2, -plate_d/2, 0])
                cube([plate_w, plate_d, plate_h]);
            
            // Left motor clamp
            translate([-motor_spacing/2, 0, 0])
                motor_clamp();
            
            // Right motor clamp
            translate([motor_spacing/2, 0, 0])
                motor_clamp();
            
            // Reinforcement ribs (bottom)
            for (x = [-100, 0, 100]) {
                translate([x - 2, -plate_d/2, 0])
                    cube([4, plate_d, plate_h]);
            }
        }
        
        // Extrusion spine bolt holes (M5, 2x along center)
        for (y = [-spine_bolt_spacing/2, spine_bolt_spacing/2]) {
            translate([0, y, -1])
                cylinder(d=m5_clear, h=plate_h+2, $fn=30);
            // Counterbore for bolt head
            translate([0, y, plate_h - 2.5])
                cylinder(d=10, h=3, $fn=30);
        }
        
        // Weight reduction holes
        for (x = [-70, 70]) {
            for (y = [-40, 40]) {
                translate([x, y, -1])
                    cylinder(d=25, h=plate_h+2, $fn=40);
            }
        }
        
        // Corner rounding (chamfer edges)
        // (simplified — round in slicer or add minkowski)
    }
}

motor_mount_plate();
