Archive for the 'CNC' category
Cracking a NUT with code
November 18, 2008 9:48 pmI got the gear that I lent to Amanda’s campaign back and am getting it set up for a project that me and a few friends are doing. Today was mainly getting the PowerCom KIN-1500AP UPS up and running. I ended up having to write some code to get the shutdown working properly.
See:
#! /usr/bin/perl
# little script that will wait till batteries are almost dead to power down # if power comes back before then then NUT is restarted
# min battery level to start shutdown $min_battery_level = 80;
# seconds to wait between poll of UPS status $poll_interval = 10;
$upsc_bin = "/bin/upsc"; $logger_bin = "/usr/bin/logger"; $upsname = "black@localhost";
$ups_status = &get_ups_status(); $ups_battery_level = &get_ups_battery_level();
`$logger_bin -t $0 "called by NUT"`;
`$logger_bin -t $0 "UPS_STATUS is $ups_status"`;
`$logger_bin -t $0 "UPS_BATTRY is at $ups_battery_level"`;
while ( &get_ups_battery_level() > $min_battery_level ) {
### The UPS is back on line and we can exit
if ( &get_ups_status() eq 'good' ) {
`$logger_bin -t $0 "UPS_STATUS is good."`;
`$logger_bin -t $0 "Restarting NUT and exiting $0"`;
`/etc/init.d/nut restart`;
exit 1;
}
else {
foreach ( `$upsc_bin $upsname` ) {
if ($_ =~ m/ups\.status/) {
`$logger_bin -t $0 "$_"`;
$ups_battery_level = &get_ups_battery_level();
`$logger_bin -t $0 "battery.charge: $ups_battery_level"`;
sleep $poll_interval;
}
}
}
}
# If we get to this point the battery is really low and we need to shutdownp `$logger_bin -t $0 "battery.charge is below $min_battery_level"`; `$logger_bin -t $0 "Shutting system down"`;
`/sbin/shutdown -h -P now`;
########################################
sub get_ups_battery_level() {
foreach ( `$upsc_bin $upsname` ) {
if ($_ =~ m/battery\.charge/) {
$charge = $_;
$charge =~ s/(.*)(\:\ )(.*)/$3/ ;
chomp ($charge);
}
}
return $charge;
} #End of sub get_ups_battery_level()
sub get_ups_status() {
foreach ( `$upsc_bin $upsname` ) {
if ($_ =~ m/ups\.status/) {
if ( $_ =~ m/OL/ ) {
$status = "good";
}
else {
$status = "bad";
}
}
}
return $status;
} #End of sub get_ups_status()
The CNC machine is working great. I am still learning the intricacies of CAM and gcode… well that and waiting for the variable speed controller and mill ends to arrive. I emailed the city to find out the process for selection on the CRC. This week they are reviewing the applications and the beginning of December is when they will interview. Jonathan over at Bikeportland.org posted a very telling story of why I feel that Citizen oversight of the police service is necessary:


