I have started this because the discussion in the 'Visual NXC' forum was getting well off-topic.
Because there are so many ways to do things within NXC, I suggested that a repository of short programs, subroutines or even code segments be created. The idea is that those of us who are neither trained programmers or experts in NXC can find a simple solution for common subjects such as using the sensors, opening a file (which might already exist), etc. I see these as 'NXC myblocks' which people can drop into their programs or just peruse to get ideas.
I'm sure this will not be universally acclaimed but thought I would raise the subject anyway.
To start, I have created an example of how contributions to such a library might be set out.
Puts on hard hat and takes cover .......
Function of the code
This code operates two relays attached to Port C of the Lego NXT to operate the focus and shutter on a Canon 350D or 400D digital camera via a standard stereo connection cable.
The focus relay is operated by sending a 'motor reverse' signal to port C while the shutter is operated by sending a 'motor forward signal'. Diodes select which relay will work depending on the direction of the applied voltage.
It relies on user input from the main program to set variables according to whether
1. the camera is set to use shutter lock or normal operation
2. the camera is set up for auto or manual focus.
It is assumed that both relays have the same nominal operating voltage. A value of 6 volts is set initially but this can be varied by the user at the start of the main program. The program automatically sets the output voltage according to the nominal relay voltage, the actual voltage of the NXT batteries and the voltage drop across rhe dipdes (0.7v).
All other values are set within the program and cannot be changed whilst the program is running.
Version 1.2 - 28 March 2011.
Language
The ptogram is written in NXC as used in the Bricx Command Center build 3.3.8.9 dated 03/11/2010.
The program runs on standard NXT firmware version1.29. Compatibilty with earlier versions has not been tested.
User license
This code may be freely used, copied, amended and distributed for personal use only.
Code Segment
I have initialised all the variables at the start (before the first task) so they are global. I do not know if these could be defined within the sub-routine.
long relay_volts = 6; // nominal design voltage for focus & shutter relays
byte relay_power; // power % applied to relays to focus & take photo
bool mirror; // set to true if mirror lock on & false if off
bool focus; // set to true if auto & false if manual
unsigned long focus_time = 2000; // time allowed in milliseconds for camera focus
unsigned long shoot_time = 250; // time in milliseconds to energise camera shutter
long shot_count; // number of shots taken in a sequence
shot_count = 0; // Zero photo counter
sub take_shot () // Focus the camera then take a shot -----------------------------------------
{
relay_volts = relay_volts * 1000 + 700; // Convert to millivolts & allow for diodes
relay_power = 100*relay_volts / BatteryLevel(); // Factor down NXT voltage to avoid burning relays
if (relay_power > 100) {relay_power = 100;} // Must be <= 100%
if (focus == true) // Skip this part if auto focus not used
{
Wait (1000); // Wait for vibrations to cease
OnRev (OUT_C, relay_power); // Focus relay on Port C
Wait (focus_time); // Keep relay energised so the camera can focus
Off(OUT_C); // Switch relay off NB no way to tell if focus achieved
}
if (mirror == true) // Skip this part if mirror lock not used
{
OnFwd (OUT_C, relay_power); // Operate shutter - lock mirror in up position
Wait (shoot_time); // Keep relay energised
Off(OUT_C); // Switch relay off - mirror will stay up
}
Wait (2000); // Wait for vibrations to cease
OnFwd (OUT_C, relay_power); // Operate shutter (mirror will drop if already open)
Wait (shoot_time); // Keep relay energised
Off(OUT_C); // Switch relay off
Wait (2000); // Wait for camera to finish storing shots
shot_count += 1; // Increment the shot counter
} // End of subroutine ================================================
Example of possible layout for discussion. Please do not comment on the coding!
(2.03 KiB) Downloaded 259 times
A sophistical rhetorician, inebriated with the exuberance of his own verbosity, and gifted with an egotistical imagination that can at all times command an interminable and inconsistent series of arguments to malign an opponent and to glorify himself.