Subscribe RSS

In my computer science blog I will try to provide source code snippets and small solutions, to small problems that occur often and take unexpected long time to solve :P

I am publishing these solutions in order to equalize somehow the huge amount of computer science shared knowledge, that helped me to become a better programmer :)

To all other "ambush" style "scientists" I would like to take the quote as a personal greeting:

"The secret to creativity is knowing how to hide your sources"

So I'll let those people prove themselves "creative" and will share stuff I didn't found that much online :)

Check out our little VR demo at Uni-Saarland

As part of Max Ophuels Preis we demonstrated our markerless motion capturing system to visitors in form of real-time Unity virtual reality game, where you have jump on boxes over lava and avoid flying ball, tracked and augmented as a fireball.

Running is one of the most natural human activities, yet it yields all sorts of injuries, causing dissatisfaction often followed by a withdraw. In this video we explore in depth how you can improve your running style and avoid injuries all together with the help of a motion capturing system.

1. 0:36 Foot Strike: Start from the bottom where the foot hits the ground. It’s called foot-strike and most people hit it with their heel at first. This is not only inefficient, because it breaks your forward motion, but it creates a shock that goes all the way from your heels through the ankles, shins and knees, to reach your hips and waist, making all of them vulnerable to injuries. To avoid this first try to land between the middle and the front part of your feet with a slightly bent knee. Thus you won’t create a breaking force against your motion. Having your knee not fully extended will allow your muscles to absorb the landing impact instead of your joints and bones. This is believed to be the leading cause of the so called Patellofemoral pain syndrome or runner’s knee and Plantar fasciitis or heel pain. Furthermore, you should always strive to step close to your center of mass projection on the floor. The farther you step from your center of gravity the more your muscles and joints will have to compensate in order to keep you balanced. Besides wasting energy you are opening yourself for injuries not only at your ankle area, but also at the inner- and outer-side of the knee. Mostly it irritates your so called iliotibial band, causing the ITB syndrome, which feels like a fire on the outer-side of the knee and it’s like someone is stabbing you there with a needle on each step. Further indicators for stepping far from the center of mass are high ankle pronation and supination.

2. 2:00 Knee Motion: Same goes for your knees – their main motion should be flexion and extension and they should point towards your running direction and rotate as little as possible. Having your knees pointing inwards is a common for woman and having them pointing outwards is common for man, both contributing for the medial tibial stress syndrome (MTSS) a.k.a. shin splints.

3. 2:23 Hip Motion: Next crucial point are your hips – you should have strong hip abductors to be able to take the step impact without going up and down absorbing the force instead of carrying out the body. High variation of hip ab- and adduction slowly causes all kinds of injuries in the waist area, including lumbar spinal disk herniation.

4. 2:43 Upper Body: From there on, you should have a straight posture between your upper-body and your head to avoid bending on each step. Lastly, your elbow flexion should be around a right angle and your hands should move forward as well. So don’t do such over-exaggerating motions and postures while running, because this will twist your spine, neck and everything around unnecessarily.

5. 3:06 Putting All Together: Try to be conscious about each aspect of you running, as well as all together, because all parts are somewhat connected. Bad knee rotations are likely to connect with crooked ankles, which leads to bad body balance and so on. On the good side of things is that if you make some parameter better, it will improve others as well. Like, if you strive to move your hands forward with your running rhythm, you are likely to improve your upper-body posture to support them.

6. 3:43 Side Balance: Further aspect, you should keep an eye on is how balanced is your running. Just compare your left and right strides in terms of length, width and joint angles across foot strikes. Those can indicate possible misalignments that can result in long term injuries. It might be the case that you are doing things wrong from one side only, thus result in injuries on that side, which happened to me many times already. If your numbers seem fine, it can also be the case, that your corresponding body parts are just in different sizes, which causes the imbalance.

7. 4:36 Different Speeds: Keep in mind that you should do this check at different speeds as well, because they have different mechanics. Here you can see when lowering your run speed, you’ll have less hip ab- and adduction, as well as less knee flexion angle variance. Hence you might have already noticed your injuries often occur when you are running at your limits. Still you should pay attention at your running style at lower speeds, because you might be slowly building up an injury – step by step, run after run.

8. 5:10 Different Runs at Same Speed: It is important to compare your stats among same speed runs, yet this is what everyone already does, thus only briefly mentioned here.

9. 5:16 Same Run at Different Times: Even more important is how your running style evolves along a long run. You can compare the beginning versus the end of your run, to see indications towards wrong running style. Some muscles might get tired faster than others and despite your good feeling of proceeding with your training, you’ll literally run against your rest muscles, joints, bones and tendons, because they’ll be the one to absorb and compensate for the tired muscles.

10. 6:10 Enjoy: Joy is very overlooked aspect of running, yet the most important. If you don’t like running, but do it a lot for whatever reasons, it is absolutely sure that you’ll get injured, which will increase your hatred towards running. If you don’t enjoy running you’ll hardly get motivated to pay attention to any of the aspects mentioned above and when something happens to your condition you’ll blame it all to running and quit. Just ask yourself, how often have you spotted people dressed with the latest sports industry fashion clothes with all of their devices, earphones and chewing gums doing strange movements that should resemble running? How persistent are those people? Couple of days, week, maybe a month? Don’t run just because you are required to do so in order to loose weight or gain endurance. That’s why my final wish to all of you is to enjoy running and stay healthy to be able to do it as long as you wish!

Thanks for watching! If you are interested in our motion capturing system visit our website

Further Links:
1. Runner’s World: The Big 7 Body Breakdowns -
2. Competitor Running: The 5 Most Troublesome Running Injuries -
3. UWTV: Common Running Injuries -
4. Howcast: How to have proper running form -
5. Sage Running: Essential Running Technique Tips for Proper Form & How to Run Faster -

Here you go a class for Serial Communication with Texas Instruments eZ430-TMS37157 RFID written on C#. I had some lot of troubles by creating this class mainly due to the lack of documentation from TI side. Nevertheless, I found a lot of sources online that I've combined and wrote some stuff by myself so I hope this code will help you too! :)

Note that the code might be a little bit inconsistent in terms of variable naming and also the communication protocol is only partially implemented, because I've needed only the message part.


// Serial Class with all stuff for communication with eZ430-TMS37157 written on C# // // by :) // using System; using System.Collections.Generic; using System.Diagnostics; using System.IO.Ports; using System.Linq; using System.Management; using System.Runtime.Remoting.Metadata.W3cXsd2001; namespace tudos { /// <summary> /// The Type of the Serial Error /// </summary> enum SerialErrorType { OK, SEND_TIMEOUT, SEND_INVALID_OPERATION, READ_OVERLOAD, READ_TIMEOUT, NO_ANSWER } /// <summary> /// Whole Serial Message used to determine errors when class is deployed /// </summary> class SerialMessage { public string message = "OK :)"; public SerialErrorType type = SerialErrorType.OK; public object exceptionObject = null; } /// <summary> /// Serial class to deal with eZ430-TMS37157 /// </summary> class Serial { /// <summary> /// No Answer Standard /// </summary> const string NOANSWER = "010B00000000000000000000000B"; /// <summary> /// Serial Port instance /// </summary> private static SerialPort _serialPort; /// <summary> /// Last Serial Message /// </summary> public SerialMessage LastMessage { get; private set; } /// <summary> /// All received Buffers stored when debugging /// </summary> public List<byte[]> ReceivedBuffers { get; private set; } /// <summary> /// All sent buffers stored when debugging /// </summary> public List<byte[]> SentBuffers { get; private set; } /// <summary> /// Save the available comports to enable switching /// </summary> public List<COMPortInfo> AvailableComports { get; private set; } /// <summary> /// Standard Constructor to connect to eZ430-TMS37157 /// </summary> public Serial() { OpenSerial(RefreshAvailablePorts()); #if DEBUG { ReceivedBuffers = new List<byte[]>(); SentBuffers = new List<byte[]>(); } #endif LastMessage = new SerialMessage { message = "Serial Class Initialized" + DateTime.Now }; } ~Serial() { _serialPort.Close(); } /// <summary> /// Open serial port /// </summary> /// <param name="name">the unfriendly name</param> private void OpenSerial(string name) { // taking the standard settnigs of eZ430-TMS37157 _serialPort = new SerialPort { PortName = name, BaudRate = 9600, Parity = Parity.None, DataBits = 8, StopBits = StopBits.One, Handshake = Handshake.None, ReadTimeout = 500, WriteTimeout = 500 }; _serialPort.Open(); _serialPort.ErrorReceived += ErrorEvent; } /// <summary> /// Refreshes the serial ports /// </summary> /// <returns>gives back the probable unfriendly comport name of some eZ430-TMS37157</returns> private string RefreshAvailablePorts() { string portName = String.Empty; AvailableComports = COMPortInfo.GetCOMPortsInfo(); foreach (COMPortInfo comPort in AvailableComports) { Debug.WriteLine(string.Format("{0} – {1}", comPort.Name, comPort.Description)); // query the friendly name if (comPort.Description.Contains("USB Serial Port")) { portName = comPort.Name; } } return portName; } /// <summary> /// Catching the errors out of the Serial Class /// </summary> /// The Serial Class is pretty strange, as the functions don't return success parameter /// <param name="sender">Serial Class</param> /// <param name="e">System.IO.Ports.SerialError</param> private void ErrorEvent(object sender, EventArgs e) { SerialError errorOccured = ((SerialErrorReceivedEventArgs)e).EventType; switch (errorOccured) { case SerialError.TXFull: break; case SerialError.RXOver: break; case SerialError.Overrun: break; case SerialError.RXParity: break; case SerialError.Frame: break; default: throw new ArgumentOutOfRangeException(); } } /// <summary> /// Read One Buffer /// </summary> /// <param name="receivedBuffer"></param> /// <returns></returns> public SerialMessage ReadBuffer(out byte[] receivedBuffer) { try { if (!_serialPort.IsOpen) { _serialPort.Open(); } string buffer = _serialPort.ReadTo("\r\n "); buffer = _serialPort.ReadTo("\r\n"); if ("" != _serialPort.ReadExisting()) { receivedBuffer = null; return new SerialMessage { message = "Serial Receive Overload", type = SerialErrorType.READ_OVERLOAD }; } if (buffer == NOANSWER) { receivedBuffer = null; return new SerialMessage { message = "Serial Received No Answer", type = SerialErrorType.NO_ANSWER }; } byte[] framedBuffer = BufferConverter.GetStringToBytes(buffer); receivedBuffer = RemoveFrames(framedBuffer); #if DEBUG { ReceivedBuffers.Add(receivedBuffer); } #endif return new SerialMessage(); } catch (TimeoutException e) { receivedBuffer = null; return new SerialMessage { message = "Serial", exceptionObject = e, type = SerialErrorType.READ_TIMEOUT }; } } /// <summary> /// Writes buffer to the comport /// </summary> /// <param name="buffer">buffer to write</param> /// <returns>success status</returns> public SerialMessage WriteBuffer(byte[] buffer) { try { if (!_serialPort.IsOpen) { _serialPort.Open(); } byte[] fullBuffer = AddFrames(buffer); _serialPort.Write(BufferConverter.GetBytesToString(fullBuffer)); #if DEBUG { SentBuffers.Add(fullBuffer); } #endif //_serialPort.Close(); } catch(TimeoutException e) { LastMessage = new SerialMessage { message = "Command Send Time Out: " + e.Message, type = SerialErrorType.SEND_TIMEOUT, exceptionObject = e }; return LastMessage; } catch (InvalidOperationException e) { LastMessage = new SerialMessage { message = "Command Send Invalid Operation: " + e.Message, type = SerialErrorType.SEND_INVALID_OPERATION, exceptionObject = e }; return LastMessage; } LastMessage = new SerialMessage { message = "Command Sent", }; return LastMessage; } /// <summary> /// Add the headers to the raw buffer /// </summary> /// <param name="rawBuffer">buffer body</param> /// <returns>framed buffer</returns> byte[] AddFrames(byte[] rawBuffer) { const int HEADERS_LENGHT = 6; const int FOOTERS_LENGHT = 5; const int FRAMES_LENGTH = HEADERS_LENGHT + FOOTERS_LENGHT; byte[] buffer = new byte[rawBuffer.Length + FRAMES_LENGTH]; buffer[0] = 0x01; // fixed buffer[1] = (byte)(rawBuffer.Length + FRAMES_LENGTH - 3); // size of buffer buffer[2] = 0x06; // CMD1 buffer[3] = 0x32; // PB1 buffer[4] = 0x48; // #TX Bits buffer[5] = 0x7d; // MSP ACCESS Write Address Array.Copy(rawBuffer, 0, buffer, 6, rawBuffer.Length); // Actual Data byte[] CRCData = buffer.Skip(5).Take(7).ToArray(); // Data for the CRC byte[] CRC = Crc16Ccitt.GetCRC(CRCData); // The Actual CRC buffer[HEADERS_LENGHT + rawBuffer.Length] = CRC[0]; // CRC MSB buffer[HEADERS_LENGHT + rawBuffer.Length + 1] = CRC[1]; // CRC LSB buffer[HEADERS_LENGHT + rawBuffer.Length + 2] = 0x0F; // PB2 buffer[HEADERS_LENGHT + rawBuffer.Length + 3] = 0x0A; // Size of awaited Answer (add this to variable some day to check...) byte[] LRCData = buffer.Skip(1).Take(buffer.Length - 2).ToArray(); byte lrc = LRC.calculateLRC(LRCData); buffer[buffer.Length - 1] = lrc; // LRC return buffer; } /// <summary> /// Remove frames from the buffer /// </summary> /// <param name="rawBuffer">framed buffer</param> /// <returns>body buffer</returns> byte[] RemoveFrames(byte[] rawBuffer) { const int HEADERS_LENGHT = 6; const int BODY_LENGTH = 6; byte[] CRCData = rawBuffer.Skip(5).Take(7).ToArray(); // Data for the CRC byte[] CRC = Crc16Ccitt.GetCRC(CRCData); // The Actual CRC if (CRC[0] != rawBuffer[HEADERS_LENGHT + BODY_LENGTH] && CRC[1] != rawBuffer[HEADERS_LENGHT + BODY_LENGTH + 1]) { // throw CRC error } byte[] LRCData = rawBuffer.Skip(1).Take(rawBuffer.Length - 2).ToArray(); byte lrc = LRC.calculateLRC(LRCData); if (rawBuffer[rawBuffer.Length - 1] != lrc) { // throw LRC error } return rawBuffer.Skip(4).Take(6).ToArray(); } } /// <summary> /// Convert byte buffers back and forth /// </summary> static class BufferConverter { static public byte[] GetStringToBytes(string value) { SoapHexBinary shb = SoapHexBinary.Parse(value); return shb.Value; } static public string GetBytesToString(byte[] value) { SoapHexBinary shb = new SoapHexBinary(value); return shb.ToString(); } } /// <summary> /// Cyclic Redundancy Check (CRC) /// </summary> /// CCITT (x^16 + x^12 + x^5 + 1) public static class Crc16Ccitt { public static byte[] GetCRC(byte[] data) { ushort crc = 0x3791; // eZ430-TMS37157 initial value for (byte i = 0; i < data.Length; i++) { byte bits = 8; byte current = data[i]; // Next byte while (bits-- > 0) { bool lsb = (crc & 1) > 0; // Store LSB crc >>= 1; // Shift right 1 bit bool rxdt = (current & 1) > 0; if (rxdt) { crc |= 0x8000; // Shift in next bit } if (lsb) // Check stored LSB { crc ^= 0x8000; // Invert MSB } if (0x8000 == (crc & 0x8000)) // Check MSB { crc ^= 0x0408; // Invert bits 3 and 10 (have in mind those are x^12 and x^5) } current >>= 1; // Next bit } } return BitConverter.GetBytes(crc); } } /// <summary> /// Longitudinal Redundancy Check (LRC) /// </summary> public static class LRC { public static byte calculateLRC(byte[] bytes) { byte LRC = 0; for (int i = 0; i < bytes.Length; i++) { LRC ^= bytes[i]; } return LRC; } } /// <summary> /// Get Friendly Comport Names :) /// </summary> /// based on public class COMPortInfo { public string Name; public string Description; public static List<COMPortInfo> GetCOMPortsInfo() { List<COMPortInfo> comPortInfoList = new List<COMPortInfo>(); ManagementScope connectionScope = new ManagementScope { Path = new ManagementPath(@"\\" + Environment.MachineName + @"\root\CIMV2"), Options = new ConnectionOptions { Impersonation = ImpersonationLevel.Impersonate, Authentication = AuthenticationLevel.Default, EnablePrivileges = true } }; connectionScope.Connect(); ObjectQuery objectQuery = new ObjectQuery("SELECT * FROM Win32_PnPEntity WHERE ConfigManagerErrorCode = 0"); ManagementObjectSearcher comPortSearcher = new ManagementObjectSearcher(connectionScope, objectQuery); using (comPortSearcher) { string caption; foreach (ManagementObject obj in comPortSearcher.Get()) { if (obj != null) { object captionObj = obj["Caption"]; if (captionObj != null) { caption = captionObj.ToString(); if (caption.Contains("(COM")) { COMPortInfo comPortInfo = new COMPortInfo { Name = caption.Substring(caption.LastIndexOf("(COM")).Replace("(", string.Empty).Replace(")", string.Empty), Description = caption }; comPortInfoList.Add(comPortInfo); } } } } } return comPortInfoList; } } }

It's time for new blog post, briefly about how to deal with implementing easy bit field structure layout in C :)

Let us see the following structure in C++


10 /*****************************************************************************

11 *

12 *    Splitt structure we are about to cast :)

13 *

14 *****************************************************************************/


16 // prototype C++


18 const int A_Flag_width = 2;

19 const int A_FrameID_width = 6;

20 const int A_Link_width = 8;

21 const int A_SplittID_width = 8;

22 const int A_Empty_width = 8;


24 typedef struct

25 {

26 DWORD32 A_Flag : A_Flag_width;

27 DWORD32 A_FrameID : A_FrameID_width;

28 DWORD32 A_Link: A_Link_width;

29 DWORD32 A_SplittID : A_SplittID_width;

30 DWORD32 A_Empty: A_Empty_width;


32 char * Data;

33 }

34 splitt_struct;


36 /****************************************************************************/

It represents a recent structure I wanted to code in C about some Frame with header, which should be splitted in structure called Splitt, that has also some header and look like above declared. So to spare space you might wanna have precise bit layout. One way to deal nicely with bitlayouts are the bitfields like this (click), other way is to use bitsets (click), also few others using diverse libraries.

Anyway, enough said, the C variant is this:


39 /*****************************************************************************

40 *

41 *    This is the nice C variant :)

42 *

43 *****************************************************************************/


45 // define shit C


47 typedef char structure;

48 typedef structure * p_structure;



51 #define MEMBER_POINTER(number,t)                (t *)((p_structure)Example + number * sizeof(t))

52 #define MEMBER_VALUE(number,t)                    (*MEMBER_POINTER(number, t))


54 #define SUBMEMBER_CLEAR(member, t, start, end)    do { MEMBER_VALUE(member, t) &= ~(((SIZE_MAX << end) >> (end + start)) << start); } while (0);

55 #define SUBMEMBER_GET(member, t, start, end)    ((MEMBER_VALUE(member, t) << end) >> (end + start))

56 #define SUBMEMBER_SET(member, t, val, start, end) do { SUBMEMBER_CLEAR(member, t, start, end); MEMBER_VALUE(member, t) |= val << start ; } while (0);


58 #define MEMBER_LAST_BIT(t)                        (8 * sizeof(t) - 1)

59 #define END_OFFSET(t, end)                        (MEMBER_LAST_BIT(t) - end)



62 typedef structure Splitt;

63 typedef p_structure p_Splitt;


65 #define splitt Example


67 #define A                                        0

68 #define CRC                                        1

69 #define DATA                                    2


71 #define A_flag_start                            0

72 #define A_flag_end                                END_OFFSET(size_t, 1)

73 #define A_FID_start                                2

74 #define A_FID_end                                END_OFFSET(size_t, 7)

75 #define A_Link_start                            8

76 #define A_Link_end                                END_OFFSET(size_t, 15)

77 #define A_SID_start                                16

78 #define A_SID_end                                END_OFFSET(size_t, 23)

79 #define A_Empty_start                            24

80 #define A_Empty_end                                END_OFFSET(size_t, 31)


82 #define splitt_flag_get                            (SUBMEMBER_GET(A, DWORD32, A_flag_start, A_flag_end))

83 #define splitt_flag_set(val)                    SUBMEMBER_SET(A, DWORD32, val, A_flag_start, A_flag_end)

84 #define splitt_FID_get                            (SUBMEMBER_GET(A, DWORD32, A_FID_start, A_FID_end))

85 #define splitt_FID_set(val)                        SUBMEMBER_SET(A, DWORD32, val, A_FID_start, A_FID_end)

86 #define splitt_link_get                            (SUBMEMBER_GET(A, DWORD32, A_Link_start, A_Link_end))

87 #define splitt_link_set(val)                    SUBMEMBER_SET(A, DWORD32, val, A_Link_start, A_Link_end)

88 #define splitt_SID_get                            (SUBMEMBER_GET(A, DWORD32, A_SID_start, A_SID_end))

89 #define splitt_SID_set(val)                        SUBMEMBER_SET(A, DWORD32, val, A_SID_start, A_SID_end)

90 #define splitt_empty_get                        (SUBMEMBER_GET(A, DWORD32, A_Empty_start, A_Empty_end))

91 #define splitt_empty_set(val)                    SUBMEMBER_SET(A, DWORD32, val, A_Empty_start, A_Empty_end)


93 #define p_splitt_CRC                            (MEMBER_POINTER(CRC, DWORD32))

94 #define splitt_CRC                                (*p_splitt_CRC)


96 #define splitt_data                                MEMBER_POINTER(DATA, p_Splitt)



99 /****************************************************************************/

Nice :)

To explain briefly, I cast all to byte buffer and then use some shifting functions to extract different bit fields! (taking mask for example is pretty easy by shifting the other bits left and right to clear them). The code shown also forces using the name splitt for Splitt (e.g. you cannot use the functions after declaring "Splitt bubble")(I find this good).

What you can further do is to have the nice defines in "template style" way and then cast a multiple structures with the token pasting operator, which will make your code to look like the real deal! (I did it but then reduced the structures so it turned out to be obsolete)

You can also nest common headers with defines, which I also did with the structures later on (not shown, too much stuff)

Anyway, I don't intend to explain any further for now, just click to download the code with example test!