Sunday, May 22, 2016

Raspberry Pi Server

I've been using a company that offers subversion (SVN) hosting, and it has been working very well. Subversion is an excellent way to manage code/resources. It keeps records of every change, notes on each change, and who committed the changes. Git is another, probably more popular, code/resource management system. Although Git has some technical advantages, especially with larger remote teams, I find it overly complicated when I just want to mess with my own projects. If you have ongoing coding projects, set yourself up with some kind of revision control such as Subversion or Git. Trust me!

As my projects have grown in size, the cost to host them remotely is also increasing. On top of that, I just want to have my Raspberry Pi do some useful server stuff for me anyways. So, I moved all my SVN hosting to my local Raspberry Pi. Then I thought - shoot a local FTP server would come in handy to transfer files, let me get that setup. I've got some projects that could be hosted on a website in the future, let me get that setup. It would be nice to remotely login to the Raspberry Pi so it no longer needs a keyboard/screen, let me get that setup.


There are tons of tutorials for each individual task online already, but at this point writing about my snowball of requirements may be something useful to others. So I'll do my best to recap what I did! (I was basically done by the time I thought others may find this helpful.)

I'm working with a Raspberry Pi B+. At the time I bought it, it was the highest performance Pi on the market. I'll probably buy a Raspberry Pi 3 fairly soon and do all the instructions all over again. (So did I write this to help others online? Or just so I wouldn't forget all the steps when I do it again in a few months? ... Can it be both?)

Quick list of what will be done:

1. Setup Network
2. Shell Access (SSH)
3. Version Control (SVN)
4. File Transfer (SFTP)
5. Web Server


My steps...

1 Setup Network

You may have your Raspberry Pi using DHCP (Pi asks your router for the IP address), but in my case my Pi will not necessarily be connected to my router. Only do the following steps if you want a static IP! If you can access the Internet right now with your Pi and you don't have a reason to change it, then you're probably good - skip this step.

To configure the network from a terminal window:
sudo nano /etc/network/interfaces


The lines with "lo" are for configuring the loopback.

To configure the ethernet device for static IP:
iface eth0 inet static
My local network is 10.0.1.### so I configured the IP address to something memorable and out of the way (high):
address 10.0.1.200

All computers on my local network are 10.0.1.###, so I configured the subnet to:
netmask 255.255.255.0

Broadcast to my local network:
broadcast 10.0.1.255

And the gateway set to:
gateway 10.0.1.1


In nano, hit CTRL X to exit, Y to agree to save, and then push enter to confirm the filename.

And restart the networking:
sudo ifdown eth0 && sudo ifup eth0

To see how the network is configured, type:
ifconfig


2 Shell Access (SSH)

I have no plans on using X Windows on the Raspberry Pi, so remote shell access is more than enough for me. I downloaded PuTTY as a SSH Client on my Windows computer so that I could login to my Raspberry Pi remotely.

To enable shell access on the Raspberry Pi, type:
sudo raspi-config



Then select "8 Advanced Options"


Then select "A4 SSH"


Then select "Enable"


The SSH server should be good to go!

I set my Pi to a static IP address of 10.0.1.200.

When I run PuTTY on my Windows computer:


And clicking Open...


No more keyboard and monitor required for the Pi!

3 Version Control (SVN)

To download and install the SVN software:
sudo apt-get install subversion

Then to create the folder for all the repositories to be held:
mkdir -p /home/pi/repos

Then create each repository:
svnadmin create /home/pi/repos/projectname
Where projectname is what your project is. I created a half dozen, including "arduinoComputer", "gps", and "orbitalAero".


Finally download and install Apache SVN:
sudo apt-get install apache2 libapache2-svn

Edit dav_svn.conf:
sudo nano /etc/apache2/mods-available/dav_svn.conf


The file has a bunch of entries almost ready to go, they're just commented out. I modified mine to look like:
<Location /svn>

  # Uncomment this to enable the repository
  DAV svn

  #Alternatively, use SVNParentPath if you have multiple repositories under
  # a single directory (/var/lib/svn/repo1, /var/lib/svn/repo2, ...)
  # You need either SVNPath and SVNParentPath, but not both.
  SVNParentPath /home/pi/repos

  # Basic Authentication is repository-wide. It is not secure unless
  # you are using https.  See the 'htpasswd' command to create and
  # manage the password file - and the documentation for the
  # 'auth_basic' and 'authn_fiel' modules, which you will need for this
  # (enable them with 'a2enmod').
  AuthType Basic
  AuthName "Subversion Repository"
  AuthUserFile /etc/apache2/dav_svn.passwd

  # Require a valid account to checkout.
  Require valid-user

</Location>

In nano, hit CTRL X to exit, Y to agree to save, and then push enter to confirm the filename.

Restart Apache:
sudo /etc/init.d/apache2 restart

Then change the permissions of the repository folder:
sudo chown -R www-data:www-data /home/pi/repos

Then create the user:
sudo htpasswd -c /etc/apache2/dav_svn.passwd <username>
where <username> is your login name.

You can navigate to the repository using your web browser.
In my case: 10.0.1.200/svn/gps

(This screenshot was taken after I committed the GPS and Libraries folders in the following steps.)

However, viewing in the web browser isn't very helpful. At this point I went back to my Windows computer. I use TortoiseSVN so I can have a nice GUI to interact with my repos.

I did a checkout of each of the new projects from the Raspberry Pi to my Windows computer.


I copied the latest code of each project into their corresponding new repository, then committed them into the Raspberry Pi.


Unfortunately I lost all my change logs up to that point, but that's ok.

By the way - take a look at the Tortoise Log screenshot above. It has a record of every commit to the repository, who did it, the date, and all the files that have changed. If you double click on one of the files, it shows you all the changes that were made. If you went down a rabbit hole with your code and decided it's a disaster, you can revert back to any prevision revision. If you see a weird change to a file and are wondering who / what / when / why it happened, you can do that by looking at the log! You definitely want to use some kind of version control such as SVN or Git or similar!

4 File Transfer (SFTP)

I want easy file transfer access to my Pi for sharing files / backup purposes. SFTP (SSH File Transfer Protocol) is turn-key once you have SSH going!

Both WinSCP and FileZilla work very well for this purpose.

I created a Pi site with IP address 10.0.1.200


And connected!


Now I can drag files / folders between my workstation and the Pi server. To modify some files I have to log into the Pi as root (or change the folder's permissions).

5 Web Server

Apache was already installed in the SVN steps above, but if you skipped them, then from a terminal window type:
sudo apt-get install apache2

The default folder for the website is /var/www


And when opened in a web browser:


One of my projects will be taking advantage of this web server soon*!

* Soon is a very relative term. ;-)

A few handy functions when dealing with Apache...

To stop Apache (SVN and website):
sudo /etc/init.d/apache2 stop

To start Apache (SVN and website):
sudo /etc/init.d/apache2 start

To restart Apache (SVN and website):
sudo /etc/init.d/apache2 restart



Monday, May 9, 2016

Orbital Aero Model: Indirect Forces

The orbital aero model calculates two forces I'm calling "Indirect" - Gravity and Atmospheric Drag.

Gravity


The force of gravity is dependent on the mass of the two bodies and distance between them. The simulation calculates the influence of gravity from every object to every other object every frame. Not only will satellites be pulled to the Earth, the Earth will be pulled to the satellites!



Fg is the force of gravity, in newtons.
G is the gravitational constant, 6.67408 x 10^-11 in m^3/(kg * s^2).
m1 is one body, in kilograms.
m2 is another body, in kilograms.
r is the distance between the bodies, in meters.

https://en.wikipedia.org/wiki/Gravity

Atmospheric Drag


Some bodies have an atmosphere modeled around them. For any body within the atmosphere of another body, the drag force is calculated and applied.



Fd is the force of drag, in newtons.
rho is the density of the fluid, in kg / m^3.
A is the drag reference area, in m^2.
Cd is the coefficient of drag, which is unitless.
v is the velocity relative to the object, in m/s.

https://en.wikipedia.org/wiki/Drag_(physics)

Orbital Aero Code - update()


To recap from a previous post, there is an update() function in the OrbitalAeroModel class:

// Primary function to process the physics and kinematics all bodies.
bool OrbitalAeroModel::update(double timeStep_seconds)
{
 // It's important all forces are calculated before all positions are updated before all collisions
 // are checked, etc. That way the order in which bodies are looped through does not affect how they
 // affect other bodies in space.

 // First calculate indirect forces, such as those from gravity and atmospheric drag.
 // Currently includes the thrust of engines, but that may be separated eventually.
 processIndirectForces();

 // Next add frictional forces.
 // This must be done after gravity/atmospheric forces to know the normal force for friction.
 processDirectForces();

 // Can now translate and rotate each body.
 processKinematics(timeStep_seconds);

 // After moving the body, check for collisions and adjust positions/velocities as appropriate.
 processCollisions(timeStep_seconds);

 return true;
}


Orbital Aero Code - processIndirectForces()


// Function to loop through all bodies and calculate indirect forces from the gravity
// and atmosphere of other bodies.
bool OrbitalAeroModel::processIndirectForces()
{
// Loop through all bodies and calculate their forces upon each other.

// The outer loop is for the primary body that we're calculating forces for.
map <uint64, BodyModel>::iterator iteratorPrimaryBody = tableBodies.begin();
while (iteratorPrimaryBody != tableBodies.end())
{
BodyModel &primaryBody = iteratorPrimaryBody->second;

// Only process the primary body if it's active.
if (primaryBody.active)
{
// Create a vector to contain the sum of all forces.
Vector sumForces_newtons;

// Start with the thrust force of any onboard engines.
sumForces_newtons = primaryBody.forceThrust_newtons;

// The inner loop is for all other bodies that influence the primary.
map <uint64, BodyModel>::iterator iteratorSecondaryBody = tableBodies.begin();
while (iteratorSecondaryBody != tableBodies.end())
{
BodyModel &secondaryBody = iteratorSecondaryBody->second;

// Only contribute the secondary body's influence if it's active and not the primary body.
if (secondaryBody.active && (&primaryBody != &secondaryBody))
{
// Get the relative distance between the two bodies.
Vector relativeDistance_meters = primaryBody.location_meters - secondaryBody.location_meters;
double relativeDistanceMagnitude_meters = relativeDistance_meters.magnitude();

if (relativeDistanceMagnitude_meters > 0.0)
{
// Calculate the force of gravity.
// Fg = GMm/r^2
// G = Gravitational Constant
// M = Mass of first body
// m = Mass of second body
// r = Distance between bodies
double secondayGravitationalForce_newtons = Constants::gravitationalConstant_meters3PerKilogramSecond2 * (primaryBody.massTotal_kilograms() * secondaryBody.massTotal_kilograms()) / 
relativeDistanceMagnitude_meters / relativeDistanceMagnitude_meters;

// Add passive body gravitational influence to the total force.
sumForces_newtons += -relativeDistance_meters.unit() * secondayGravitationalForce_newtons;

// Are we within the atmosphere of the secondary body?
double altitudeMSL_meters = relativeDistanceMagnitude_meters - secondaryBody.radius_meters;
if (altitudeMSL_meters < secondaryBody.atmosphereMaximumAltitudeMSL_meters())
{
// Get the air density at our current altitude.
double atmosphereDensityMSL_kilogramsPerMeter3 = secondaryBody.getAtmosphereDensityMSL_kilogramsPerMeter3(altitudeMSL_meters);

// Calculate and add passive body atmospheric forces. (Should implement 3-D drag & area.)
// Fd = 0.5 * rho * A * Cd * v * v
// rho = Density of Fluid
// Cd = Coefficient of Drag
// A = Reference Area
// v = Relative velocity through fluid
Vector relativeVelocity_metersPerSecond = primaryBody.velocity_metersPerSecond - secondaryBody.velocity_metersPerSecond;
sumForces_newtons += -relativeVelocity_metersPerSecond.sign() * relativeVelocity_metersPerSecond * relativeVelocity_metersPerSecond * 0.5 * atmosphereDensityMSL_kilogramsPerMeter3 * primaryBody.dragCoefficient * primaryBody.dragReferenceArea_meters2;
}
} // relativeDistanceMagnitude_meters > 0.0

} // ((secondaryBody.active) && (primaryBody.index != secondaryBody.index))
iteratorSecondaryBody++;
} // (iteratorSecondaryBody != tableBodies.end())
primaryBody.forceIndirect_newtons = sumForces_newtons;
} // (primaryBody.active)
iteratorPrimaryBody++;
} // (iteratorPrimaryBody != tableBodies.end())
return true;
}

Results


A couple screenshots from the simulation...


The moon satellite is orbiting the moon while the moon is orbiting the Earth. The smaller satellites are contributing gravitational effects on the planet and moon, but their influence is so small they do practically nothing.


The two satellites closest to the Earth are within the atmosphere and are influenced by atmospheric drag. The lower the altitude, the higher the air density, and thus the higher drag force.

The code as-is supports the concept of a terminal velocity. A body dropped to another body with an atmosphere will stop accelerating when its force of gravity equals the force of drag.

Copyright (c) 2016 Clinton Kam
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.