# Конструктор спутника "ОрбиКрафт"

### Sidebar

en:lesson8

This is an old revision of the document!

# Lesson 08. Getting acquainted with the solar sensors

## Getting acquainted with smartphone’s light sensor

Download on smartphone the Andro sensor (or similar) application displaying the information from smartphone’s integrated sensors. Start the applecation, find the light sensor which is called LIGHT.

The illumination is measured in lux; one lux means the illumination of the 1 m² surface at 1 lumen light stream emission falling to this surface. Correspondingly, it is true that: 1 lux = 1 lumen/m2.

One lumen means the light stream that is emitted by point source with candlepower of one candela to the solid angle of one steradian.

The full light stream created by point source with candlepower of one candela is 4π lumen.

Candela is candlepower with energy of 1/683 W/steradian.

Candlepower of one paraffin candle is one candela approximately.

The requirements for the illumination of the work places equopped with PC according to SanPiN 2.2.2/2.4.1340-03:

Desk illumination: 300-500 lux

PC display illumination: lower than 300 lux

## Solar Sensors Functionality Test

Connect HOC with Power System and four solar sensors.

Python code.

sun_test.py
def control(): # Program’s main function
sun_result = [0,0,0] # Initialize sun_result
num = 1
print "Enable sun sensor №", num
sun_sensor_turn_on(num)
sleep(1)
print "Get RAW data from sun sensor"

for i in range(10):
sun_result = sun_sensor_request_raw(num)

if not sun_result[0]: # if the sensor has not returned error message,

print "state:", sun_result[0], "raw =", sun_result[1], \
sun_result[2]

elif sun_result[0] == 1:
print "Fail because of access error, check the connection"

elif sun_result[0] == 2:
print "Fail because of interface error, check your code"

sleep(1)

print "Disable sun sensor №", num
sun_sensor_turn_off(num)

С code.

sun_test.c
#include <stdio.h>
#include <stdint.h>
#include "libschsat.h"
#define LSS_OK 0
#define LSS_ERROR 1
#define LSS_BREAK 2

int control(){ // Program’s main function
uint16_t sun_result[] = {0, 0, 0}; // Initialize sun_result
uint16_t num = 1; // Solar Sensor’s number
printf("Enable sun sensor №%d\n", num);
sun_sensor_turn_on(num); // Switch on sun sensor
Sleep(1); // Wait 1 second for switching on
printf("Get RAW data from sun sensor №%d\n", num);
int i;
for (i = 0; i < 10; i++) //Read the readings 10 times
{
sun_result[0] = sun_sensor_request_raw(num,& sun_result[1],& sun_result[2]);
if (!sun_result[0]){ // if the sensor has not returned error message
printf("state: %d raw = %d, %d\n", i, sun_result[1], sun_result[2]);
}

else if (sun_result[0] == 1) { //if the sensor has returned error message 1
printf("Fail because of access error, check the connection\n");
}
else if (sun_result[0] == 2) { //if the sensor has returned error message 2
printf("Fail because of interface error, check you code\n");
}

}
printf("Disable sun sensor №%d\n", num);
sun_sensor_turn_off(num); //Switching sun sensor off
return 0;
}

Start the program; at room lighting sensors’ values will be in range from 70 to 300. Direct the light from solar imitator to the sensors – the values will be changed from 70 to 20000.

## Values’ collection for solar sensors calibration

Attach to the OrbiCraft top board angular velocity sensor and magnetometer, also attach 4 solar sensors to 4 Orbicraft’s side boards. The solar sensors must be attached in inverted position - that is for prevention of sensors windows’ overlapping by the flat cables.

Python code.

sun_raw.py
import time
import math
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Differential feedback coefficient.
kd = 200.0
# Algorithm working time step, s
time_step = 0.05
# Satellite’s target angular velocity, deg/s.
# For stabilization mode it is 0.0.
omega_goal = 0.0
# Maximum allowed flywheel speed, rev/min
mtr_max_speed = 5000
# Flywheel number
mtr_num = 1
# Angular velocity sensors’ number
hyr_num = 1
# Magnetometer number
mag_num = 1
# Measuring result’s number
i = 1
# Current turning angle
alpha = 0.0

# The function will switch on all apparatuses
# that will be used in the main program.

def initialize_all():
print "Enable angular velocity sensor №", hyr_num
hyro_turn_on(hyr_num)
sleep(1)
print "Enable magnetometer", mag_num
magnetometer_turn_on(mag_num)
sleep(1) # Wait 1 second for switching on
print "Enable Sun sensors 1-4"
sun_sensor_turn_on(1)
sun_sensor_turn_on(2)
sun_sensor_turn_on(3)
sun_sensor_turn_on(4)
sleep(1)
print "Enable motor №", mtr_num
motor_turn_on(mtr_num)
sleep(1)

# The function will switch off all sensors,
# that will be used in the main program.
def switch_off_all():
print "Finishing..."
hyro_turn_off(hyr_num)
magnetometer_turn_off(mag_num)
sun_sensor_turn_off(1)
sun_sensor_turn_off(2)
sun_sensor_turn_off(3)
sun_sensor_turn_off(4)
motor_set_speed(mtr_num, 0)
sleep(1)
motor_turn_off(mtr_num)
print "Finish program"

def mag_calibrated(magx,magy,magz):
# instead of these 3 lines of code with calibration factors there must be the lines with calibration factors of your magnetometer
#magx_cal = 1.04*magx - 0.26*magy + 0.05*magz - 68.76		# it is the 1st line that must be changed with results of your magnetometer calibration
#magy_cal = 0.24*magx + 1.04*magy + 0.29*magz + 256.92	# it is the 2nd line that must be changed with results of your magnetometer calibration а
#magz_cal = -0.09*magx - 0.19*magy + 0.77*magz + 159.41	# it is the 3rd  line that must be changed with results of your magnetometer calibration
magx_cal = 1.06*(magx + -7.49) + -0.01*(magy + -23.59) + 0.07*(magz + -108.24)
magy_cal = -0.01*(magx + -7.49) + 1.11*(magy + -23.59) + 0.09*(magz + -108.24)
magz_cal = 0.07*(magx + -7.49) + 0.09*(magy + -23.59) + 1.00*(magz + -108.24)
return magx_cal, magy_cal, magz_cal

# Functions that defines flywheel’s new speed.
# Flywheel’s new speed will be the sum of
# flywheel’s current speed and speed increment.
# Speed increment is proportioned by angle error
# and angular velocity error.
# mtr_speed - flywheel’s current angular speed, rev/min
# omega – satellite’s current angular speed, deg/s
# omega_goal - target satellite’s angular speed, deg/s
# mtr_new_speed - target flywheel’s angular speed, rev/min

def motor_new_speed_PD(mtr_speed, omega, omega_goal):
mtr_new_speed = int(mtr_speed
+ kd*(omega-omega_goal)
)
if mtr_new_speed > mtr_max_speed:
mtr_new_speed = mtr_max_speed
elif mtr_new_speed < -mtr_max_speed:
mtr_new_speed = -mtr_max_speed
return mtr_new_speed

# Program’s main function that will call other functions.
def control():
omega_goal = 0	# omega_goal - target satellite’s angular speed, deg/s
initialize_all()
# Initialize flywheel’s status
mtr_state = 0
# Initialize angular velocity sensor’s status
hyro_state = 0
sun_sensor_num = 0 	   # Initialize the variable for the solar sensor’s number
sun_result_1 = [0,0,0] # Initialize sun_result_1
sun_result_2 = [0,0,0] # Initialize sun_result_2
sun_result_3 = [0,0,0] # Initialize sun_result_3
sun_result_4 = [0,0,0] # Initialize sun_result_4
mag_alpha = 0

output_data_all = [0, 0, 0, 0, 0, 0, 0, 0, 0]
output_data = [0, 0, 0, 0, 0, 0, 0, 0, 0]
# Measuring result’s number
i = 0
# Put into memory rotation start time
time_start = time.time()
# Put into memory one-second interval start time
time_interval = time.time()
# solar sensors data output interval in seconds
time_output = 0.1

while True:

# angular velocity sensor and flywheel data request
hyro_state, gx_raw, gy_raw, gz_raw = hyro_request_raw(hyr_num)
mtr_state, mtr_speed = motor_request_speed(mtr_num)
mag_state, magx_raw, magy_raw, magz_raw = magnetometer_request_raw(mag_num)

# Processing the readings of angular velocity sensor,
# calculation of satellite angular velocity upon angular velocity sensor’s readings.
# If angular velocity sensor’s error code is 0 (no error)
if not hyro_state:
gx_degs = gx_raw * 0.00875
gy_degs = gy_raw * 0.00875
gz_degs = gz_raw * 0.00875
# if angular velocity sensor arranged via z-axis up, then satellite’s angular velocity
# matches to sensors’ readings of z-axis, otherwise
# it is required to reverse the sign: omega = - gz_degs
omega = gz_degs
elif hyro_state == 1:
print "Fail because of access error, check the connection"
elif hyro_state == 2:
print "Fail because of interface error, check your code"

# Processing the readings of flywheel and setting the target angular velocity.
if not mtr_state:	# if magnetometer returned error code 0 (no error)
# set flywheel’s new speed
mtr_new_speed = motor_new_speed_PD(mtr_speed,omega,omega_goal)
motor_set_speed(mtr_num, mtr_new_speed)

time.sleep(time_step)
time_current = time.time() - time_start
if not mag_state: # if magnetometer returned error code 0 (no error)
magx_cal, magy_cal, magz_cal = mag_calibrated(magx_raw,magy_raw,magz_raw)
magy_cal = - magy_cal	# transition from the left coordinate system which is plotted on the magnetometer, to the right coordinate system to position the positive angle direction counter clock-wise
mag_alpha = math.atan2(magy_cal, magx_cal)/math.pi*180

if (time.time() - time_interval) > time_output:
# Put into memory next one-second interval starting time
time_interval = time.time()
sun_result_1 = sun_sensor_request_raw(1)
sun_result_2 = sun_sensor_request_raw(2)
sun_result_3 = sun_sensor_request_raw(3)
sun_result_4 = sun_sensor_request_raw(4)
#print sun_result_1, sun_result_2, sun_result_3, sun_result_4, mag_alpha, time_current

output_data = [time_current, sun_result_1[1], sun_result_1[2], sun_result_2[1], sun_result_2[2], sun_result_3[1], sun_result_3[2], sun_result_4[1], sun_result_4[2], mag_alpha]
output_data_all += output_data

if i > 100:  # Start rotation upon 5 seconds delay from the moment of launching
omega_goal = 6.0  # omega_goal – satellite’s target angular velocity, deg/s

if 	time_current > 90:
break

i += 1

switch_off_all()

print "time_end = " , time.time() - time_start

for i in range(0, 5000, 10):
print output_data_all[i-1], output_data_all[i], output_data_all[i+1], output_data_all[i+2], output_data_all[i+3], output_data_all[i+4], output_data_all[i+5], output_data_all[i+6], output_data_all[i+7], output_data_all[i+8]

С code.

sun_raw.c
#include <stdio.h>
#include <stdint.h>
#include "libschsat.h"
#define LSS_OK 0
#define LSS_ERROR 1
#define LSS_BREAK 2
#include <math.h>
#include <time.h>

//Differential feedback coefficient.
const float kd = 200.0;

// Algorithm working time step, s
const float time_step = 0.1;

// Satellite’s target angular velocity, deg/s. For stabilization mode it is 0.0
const float omega_goal = 0.0;
// Maximum allowed flywheel speed, rev/min
const int mtr_max_speed = 5000;
const uint16_t mtr_num = 1;			// Flywheel number
const uint16_t hyr_num = 1; 		        // Angular velocity sensors’ number
const uint16_t mag_num = 1;			// Magnetometer number
// Measuring result’s number
int i = 1;
// Current turning angle
const float alpha = 0.0;

void initialize_all(void){/* // The function will switch on all apparatuses that will be used in the main program.
printf("Enable angular velocity sensor №%d\n", hyr_num);
hyro_turn_on(hyr_num);
Sleep(1);
printf("Enable magnetometer %d\n", mag_num);
magnetometer_turn_on(mag_num);
Sleep(1); // Wait 1 second for switching on
printf("Enable Sun sensors 1-4\n");
sun_sensor_turn_on(1);
sun_sensor_turn_on(2);
sun_sensor_turn_on(3);
sun_sensor_turn_on(4);
Sleep(1);
printf("Enable motor №%d\n", mtr_num);
motor_turn_on(mtr_num);
Sleep(1);
}

void switch_off_all(void){ // The function will switch off all apparatuses, that will be used in the main program.
printf("Finishing...");
int16_t new_speed = 0;
hyro_turn_off(hyr_num);
magnetometer_turn_off(mag_num);
sun_sensor_turn_off(1);
sun_sensor_turn_off(2);
sun_sensor_turn_off(3);
sun_sensor_turn_off(4);
motor_set_speed(mtr_num, 0, &new_speed);
Sleep(1);
motor_turn_off(mtr_num);
printf("\nFinish program\n");
}

int mag_calibrated(int16_t *magx, int16_t *magy, int16_t *magz ){
/* instead of these 3 lines of code with calibration factors
there must be the lines with calibration factors of your magnetometer */
//magx_cal = 1.04*magx - 0.26*magy + 0.05*magz - 68.76		// it is the 1st line that must be changed with results of your magnetometer calibration
//magy_cal = 0.24*magx + 1.04*magy + 0.29*magz + 256.92	// it is the 2nd line that must be changed with results of your magnetometer calibration а
//magz_cal = -0.09*magx - 0.19*magy + 0.77*magz + 159.41	// it is the 3rd  line that must be changed with results of your magnetometer calibration
float magx_cal;
float magy_cal;
float magz_cal;
magx_cal = 1.06*(*magx + -7.49) + -0.01*(*magy + -23.59) + 0.07*(*magz + -108.24);
magy_cal = -0.01*(*magx + -7.49) + 1.11*(*magy + -23.59) + 0.09*(*magz + -108.24);
magz_cal = 0.07*(*magx + -7.49) + 0.09*(*magy + -23.59) + 1.00*(*magz + -108.24);
*magx = magx_cal;
*magy = magy_cal;
*magz = magz_cal;
return 0;
}

int motor_new_speed_PD(int mtr_speed, float omega, int16_t omega_goal){
/*
Functions that defines flywheel’s new speed.
Flywheel’s new speed will be the sum of
flywheel’s current speed and speed increment.
Speed increment is proportioned by angle error
and angular velocity error.
mtr_speed - flywheel’s current angular speed, rev/min
omega – satellite’s current angular speed, deg/s
omega_goal - target satellite’s angular speed, deg/s
mtr_new_speed - target flywheel’s angular speed, rev/min
*/
int16_t mtr_new_speed;
mtr_new_speed = (int)(mtr_speed + kd * (omega - omega_goal));
if (mtr_new_speed > mtr_max_speed)
{
mtr_new_speed = mtr_max_speed;
}
else if (mtr_new_speed < -mtr_max_speed)
{
mtr_new_speed = -mtr_max_speed;
}
return mtr_new_speed;
}

int control(){// Program’s main function that will call other functions.
int16_t omega;
int omega_goal = 0;		// omega_goal - target satellite’s angular speed, deg/s
initialize_all();
int mtr_state = 0;		// Initialize flywheel’s status
int hyro_state = 0;		// Initialize angular velocity sensor’s status
int mag_state = 0; 		// Initialize magnetometer
int16_t mtr_speed;
int16_t mtr_new_speed;
//angular velocity sensor’s data
int16_t gx_raw;
int16_t gy_raw;
int16_t gz_raw;
int16_t *hyrox_raw=&gx_raw;
int16_t *hyroy_raw= &gy_raw;
int16_t *hyroz_raw = &gz_raw;
//magnetometer data
int16_t mgx_cal=0;
int16_t mgy_cal=0;
int16_t mgz_cal=0;
int16_t *magx_raw = &mgx_cal;
int16_t *magy_raw = &mgy_cal;
int16_t *magz_raw = &mgz_cal;

float gx_degs;
float gy_degs;
float gz_degs;
uint16_t sun_result_1[] = {0,0,0}; // Initialize sun_result_1
uint16_t sun_result_2[] = {0,0,0}; // Initialize sun_result_2
uint16_t sun_result_3[] = {0,0,0}; // Initialize sun_result_3
uint16_t sun_result_4[] = {0,0,0}; // Initialize sun_result_4
int mag_alpha = 0;
const int sizeOD = 10;
int sizeODA = 0;
int tempSODA;
//	double* output_data = (double*)calloc(sizeOD, sizeof(double));
double* output_data_all = (double*)calloc(sizeODA, sizeof(double));

// Measuring result’s number
int i = 0;
// Put into memory rotation start time
long int time_start = time(NULL);
// Put into memory one-second interval start time
long int time_interval = time(NULL);
// solar sensors data output interval in seconds
int time_output = 0.1;
int j;
char a=1;

while (a==1){
// angular velocity sensor and flywheel data request
hyro_state = hyro_request_raw(hyr_num,hyrox_raw,hyroy_raw,hyroz_raw);
mtr_state = motor_request_speed(mtr_num, &mtr_speed);
mag_state = magnetometer_request_raw(mag_num, magx_raw, magy_raw, magz_raw);

if (!hyro_state){
/*Processing the readings of angular velocity sensor,
calculation of satellite angular velocity upon angular velocity sensor’s readings.
If angular velocity sensor’s error code is 0 (no error) */
gx_degs = gx_raw * 0.00875;
gy_degs = gy_raw * 0.00875;
gz_degs = gz_raw * 0.00875;
/* if angular velocity sensor arranged via z-axis up, then satellite’s angular velocity
matches to sensors’ readings of z-axis, otherwise
it is required to reverse the sign: omega = - gz_degs */
omega = gz_degs;
//			printf("gx_degs=%f, gy_degs=%f, gz_degs=%f\n", gx_degs, gy_degs, gz_degs);//ну так на всякий
}
else if (hyro_state == 1){
printf("Fail because of access error, check the connection\n");
}
else if (hyro_state == 2) {
printf("Fail because of interface error, check your code\n");
}

//Processing the readings of flywheel and setting the target angular velocity.
if (!mtr_state)	{// if error code 0 (no error)
int16_t mtr_speed=0;
motor_request_speed(mtr_num, &mtr_speed);
//			printf("Motor_speed: %d\n", mtr_speed);
// set flywheel’s new speed
mtr_new_speed = motor_new_speed_PD(mtr_speed,omega,omega_goal);
motor_set_speed(mtr_num, mtr_new_speed, &omega);
}

Sleep(time_step);
long int time_current = time(NULL) - time_start;

if (!mag_state){
mag_calibrated(magx_raw,magy_raw,magz_raw);
*magy_raw = - *magy_raw; /*transition from the left coordinate system which is plotted on the magnetometer, to the right coordinate system to position the positive angle direction counter clock-wise*/
mag_alpha = atan2(mgy_cal, mgx_cal)/M_PI*180;
}

if ((time(NULL) - time_interval) > time_output){
// Put into memory next one-second interval starting time
time_interval = time(NULL);
sun_result_1[0] = sun_sensor_request_raw(1, &sun_result_1[1],&sun_result_1[2]);
sun_result_2[0] = sun_sensor_request_raw(2,&sun_result_2[1],&sun_result_2[2]);
sun_result_3[0] = sun_sensor_request_raw(3,&sun_result_3[1],&sun_result_3[2]);
sun_result_4[0] = sun_sensor_request_raw(4,&sun_result_4[1],&sun_result_4[2]);

int output_data[] = {time_current, sun_result_1[1], sun_result_1[2], sun_result_2[1], sun_result_2[2], sun_result_3[1], sun_result_3[2], sun_result_4[1], sun_result_4[2], mag_alpha};
tempSODA = sizeODA;
sizeODA += sizeOD;
output_data_all = (double*)realloc(output_data_all, sizeODA*sizeof(double));
for (j=tempSODA; j<sizeODA; j++) {
output_data_all[j] = output_data[j-sizeODA+1];
}
}
if (i > 100){  // Start rotation upon 5 seconds delay from the moment of launching
omega_goal = 6.0;  // omega_goal - omega_goal – satellite’s target angular velocity, deg/s
}

if 	(time_current > 90){
break;
}
i += 1;
}

switch_off_all();

printf("time_end = %ld" , time(NULL) - time_start);

for (i = 0; i < 5000; i = i + 10){
printf("%f, %f, %f, %f, %f, %f, %f, %f, %f, %f\n",output_data_all[i], output_data_all[i+1], output_data_all[i+2], output_data_all[i+3], output_data_all[i+4], output_data_all[i+5], output_data_all[i+6], output_data_all[i+7], output_data_all[i+8], output_data_all[i+9]);
}
printf ("Ok\n");
return 0;
}

Based on the program run’s results there will be displayed 500 data lines in such form:

onmessage0.132400989532 74 117 27 25 156 214 156 61 -34.6566118491
onmessage0.281419992447 74 116 27 25 156 215 156 61 -34.555539497
onmessage0.438189029694 74 116 27 25 156 214 156 61 -33.6820711721
onmessage0.585952043533 74 116 27 25 156 215 156 61 -34.6566118491
onmessage0.733724832535 74 116 27 25 156 214 156 61 -33.6705914701
onmessage0.88149189949  74 117 27 25 156 214 156 61 -33.7733709383
onmessage1.02926301956  74 117 27 25 156 214 156 61 -33.6745869595
onmessage1.1782848835   74 117 27 25 156 214 156 61 -33.4062495287
onmessage1.32730197906  74 117 27 25 156 214 156 61 -33.2488503276
onmessage1.47507381439  74 117 27 25 156 214 156 61 -33.8923918648 

when the first value is the time elapsed from measurement start, next two values are the data from the first solar sensor, next two values are the data from the second solar sensor, next two values are the data from the third solar sensor and the next two – from the fourth sensor. The tenth value is magnetometer readings (OrbiCraft’s turning angle as related to direction to the magnetic pole). To analyze the collected data they must be copied from browser (select the data with Ctrl-A and copy with Ctrl-С) and saved in the new text document created with Notepad++ (insert with Ctrl-V). Than the data must be cleared from service information that is present at the file start and end. The frequent service word onmessage must be deleted with Notepad++ replace function. Press Ctrl-H, enter to the “Find” field “onmessage”, leave the “Replace” field blank and press “Replace all” or “Replace in all open documents”.

Save the clear document in .txt form file. Now it may be analyzed with Excel.