User Tools

Translations of this page:

Site Tools


Sidebar

en:examples-py

ATTENTION: the current version of the Python environment does not support characters other than ASCII (including Cyrillic)!

0. Working with the designer

To work with the designer connect the library libschsat.py:

from schsat import *

in Python, the names of libraries and modules are called without the extension .py, the '*' in this context means the import of all fields. All functions for controlling the construction set must be called inside the control() function in the text of your program. Example:

def control():
    print ("Hello, world!")

1. Example of a code to take a photo

# function definition
def take_frame(frame):
    camera_turn_on()
    sleep(2)                    # turn on the camera and wait for the device to load
    ready = camera_get_state()          # Check if the camera is ready
    if ready:
	err = camera_take_photo(frame)  # take a photo
	if err:
            print 'Error while taking frame (frame=%d)!' % frame # is used print () python 2.7
            camera_turn_off()
            return 1                    # error code
    else:
        print 'Camera is not ready!'
        camera_turn_off()
        return 1 
    camera_turn_off()                   # be sure to turn off the camera!
    return 0                            # normal execution
# use of the function
def control():
    op_result = take_frame(0)
    print(op_result)

2. Code recommended for testing the magnetometer

# Function - wrapper for magnetometer_request_raw (num) -> err function
def magn_get(nmg, off = True): # nmg - magnetometer number, variable off - whether to switch off the sensor 
     # function that works with the sensor checking whether it is on
     # and returning the list [0, x_raw, y_raw, z_raw] if it is possible 
     # to get the data, and [1, 0, 0, 0] if an error occurred. 
    if not magnetometer_get_state(nmg): # if the sensor is not turned on
        if magnetometer_turn_on(nmg): return [1, 0, 0, 0] # try to turn it on
        sleep(0.5) # if the sensor was turned on wait 0.5 seconds for the loading 
    magn_ret = magnetometer_request_raw(nmg) # save the speed value to variable magn_ret
    if magn_ret[0]: # if the sensor returned an error message
        magnetometer_turn_off(nmg) # turn off the sensor
        return [1, 0, 0, 0] # return [1, 0, 0, 0]
    if off: magnetometer_turn_off(nmg) # turn off if needed
    return magn_ret # return result in case of success
 
def control(): # The main function of the program in which you should call other functions
    mgn_ret = [0,0,0,0]     # Initialize mgn_ret
    while not mgn_ret[0]:   # while the sensor returns normal values
        mgn_ret = magn_get(1, False) # save the returned values to the variable mgn_ret
        print ("state: {}, x_raw = {}, y_raw = {}, z_raw = {}".\ # Notice the line break character!
        format(mgn_ret[0], mgn_ret[1], mgn_ret[2], mgn_ret[3])) # Print them to the console
    print("Program failed!") # If the sensor does not take readings display an error message

3. Code recommended for checking the AVS

# Function - wrapper for hyro_request_raw (num) -> err function
def hyro_get(nhyr, off = True): # nhyr - AVS number 
     # variable off - should the sensor be turned off at the end of the call
     # function that works with the sensor, checking whether it is on
     # and returning the list [0, x_raw, y_raw, z_raw] if it is possible
     # to get the data, and [1, 0, 0, 0] if an error occurred.
    if not hyro_get_state(nhyr): # if the sensor is not turned on
        if hyro_turn_on(nhyr): return [1, 0, 0, 0] # try to turn on
        sleep(0.5) # if the sensor was turned on wait 0.5 seconds for the loading 
    hyro_ret = hyro_request_raw(nhyr) # save the speed value to variable hyro_ret
    if hyro_ret[0]: # if the sensor returned an error message
        hyro_turn_off(nhyr) # turn off the sensor
        return [1, 0, 0, 0] # return [1, 0, 0, 0]
    if off: hyro_turn_off(nhyr) # turn off if needed
    return hyro_ret # return result in case of success
 
def control(): # The main function of the program in which you should call other functions
    hyr_ret = [0,0,0,0]     # Initialize hyr_ret
    while not hyr_ret[0]:   # while the sensor returns normal values
        hyr_ret = hyro_get(1, False) # save the returned values to the variable hyr_ret
        print ("state: %d, x_raw = %d, y_raw = %d, z_raw = %d",\ # Notice the line break character!
        % (hyr_ret[0], hyr_ret[1], hyr_ret[2], hyr_ret[3]) # Print them to the console
    print("Program failed!") # If the sensor does not take readings display an error message

4. Code recommended for testing solar sensors

def sunsensors_get(off = True): # True - the default argument off 
# sensor numbers - from the 1st to the 4th
    s_is_on = [sun_sensor_get_state(i+1) for i in range(4)] # checking if the sensor is on
    if 0 in s_is_on: # if the sensor is not on
        s_on_bus = [sun_sensor_turn_on(i+1) for i in range(4)] # turn it on
        sleep(2)  # waiting the devices to load
        s_is_on = [int(not(i)) for i in s_on_bus] # write the values in the list s_is_on
    data = [] # create a list for storing data
    for idx, i in enumerate(s_is_on): all sensors
        if s_is_on: data.append(sun_sensor_request_raw(idx+1)) # the value of sensors illumination
        else: data.append([1, 0, 0, 0]) # value if the sensor is off
    if off: [sun_sensor_turn_off(i+1) for i in range(4)] # turn off the sensors if it is necessary
    return data     # to return readings
# use of the function
sun_raw = sunsensors_get(False) # replace the default argument, the sensors are not turned off after the call

5. Code recommended for checking the flywheel control

def motor_check(num):
    if not motor_get_state(num): # checking if the motor is on
        if motor_turn_on(num): return 1 # try to turn on, if not possible display the error code
        sleep(2)                        # wait for device to load
    if motor_set_speed(num, 1000): return 1 # set the speed or return an error code
    sleep(5)                                # five seconds waiting
    if motor_set_speed(num, 0): return 1    # set the speed or return an error code
    motor_turn_off(num)                     # turn off
# The main program 
def control():
    res = motor_check(1)
    print (res)

6. Сode recommended for checking the telemetry transmitter

# An example of a function to send any message sent as a msg variable of type str
def msg_send(msg, trn = 1, res = 2, off = True): 
    # msg - message, trn - transmitter number, res - ground receiver number, 
    # off - whether it is necessary to turn off the transmitter after the program termination  
    # (True - yes, False - no)
    if not transceiver_get_state(trn): # if the transmitter is off: turn it on
        if transceiver_turn_on(trn):   # try to turn on
            return 1                   # if failed, return error code 1
        sleep(0.5)                     # if the power-on signal is normally received, wait 0.5 s
    if not transceiver_send(res, trn, msg): # if the message could not be transmitted
        transceiver_turn_off(trn) # turn off the transmitter
        return 1 # return error code
    if off: transceiver_turn_off(trn)  # turn off the transmitter
    return 0 # return normal completion code

7. Сode recommended for checking the HF transmitter

def send_frame(num, frame, off = True):
    if not tranmitter_get_state(num): # check the status of the HF transmitter
        if transmitter_turn_on(num): return 1
        sleep(2)
    if transmitter_transmit_photo(num, frame): # transmit frame "frame"
        if off: transmitter_turn_off(name) # it is assumed that the shutdown is always successful,
        return 1                           # but, generally speaking, this may not be the case
    if off: transmitter_turn_off(1)
    return 0 # normal completion code

8. Code recommended for working with battery

def battery():
    ah = battery_get_charge()                 # check the charge
    cc = battery_get_charging_current()       # charging current
    dc = battery_get_discharging_current()    # discharging current
    print 'Battery capacity: %g A-H' % ah      # output the data
    print 'Charging current: %g mA' % cc
    print 'Discharging current: %g mA' % dc 
    return [ah, cc, dc]                       # return values
en/examples-py.txt · Last modified: 2020/03/25 16:28 (external edit)