Skip to content

Software

Lan Sovinc edited this page Nov 4, 2018 · 3 revisions

Table of Contents

Software

Finally we get to the aspect of this project, which is the reason, I decided to upload the project here. I am going to explain what libraries I included in my project, where and why they are used, and then walk you through my code. It is not complex in any means, but I would like to explain my thought process behind it and the choices I made.

Libraries

I used 3 software libraries for the project to ease my communication via SPI devices (NRF24L01 transceiver modules) and to manipulate the Arduino timers.

SPI

This library enables communication between Arduino (master) with Serial Peripheral Interface (SPI) devices, in our case NRF2401L (slave). The data protocol uses three pins: MISO (Master In Slave Out), MOSI (Master Out Slave In) and SCK (Serial Clock). This library is included in order to make the RF24 library work. Read more

RF24

The mentioned library eases our work with the SPI protocol and the NRF2401L+ in general. It is very well documented and supports various features for beginners and advanced users. I wanted to be able to:

  • send the commands for the throttle/break and 2 lights from the remote control to the longboard
  • receive the battery voltage from the longboard on the remote to display it to the user.

I was able to send all 3 needed parameters in a single payload using C data structures, then I just needed a way to receive the battery voltage level. A basic example for communication between two transceivers shows how they alternately switch transmitter and receiver roles to exchange information, but there is a more efficient way. The library allows us to write a custom payload to an autoACK response, and therefore enables faster call-response communication. When the longboard transceiver receives a payload with the required values, it sends an autoACK in which it includes the current battery voltage level.

Read more

PWM

I used the PWM library to modify the frequency of the Timer1 hardware timer on the Arduino. This allows for more precise 50 Hz PWM signal, with the 1-2ms duty cycle, needed to send the throttle/break values to the FOCBOX speed controller. But the number of states between 0 and 100% duty cycle (resolution) depends on the frequency the timer operates on. There is a function called TimerX_GetTop(), which calculates the highest resolution of the timer X at a given frequency. At 50Hz there are TODO

Read more

Arduino Sketch

a) Board Arduino

Right before the loop() function the PWM duty cycle is set to neutral so the board stays still after boot.

The loop() void listens for incoming radio signal from the remote and when the signal is caught it is saved as a data structure, simply called "data". The content of the struct is then checked to make sure the received signal matches the expected values. If the condition is met the values for throttle/break are re-mapped to the range between the calculated full-break to full-throttle values. Those values are then used by the PWM library to generate the corresponding PWM signal for the FOCBOX.

After that, the lightsControl() functions are used to check, if any of the lights need to be turned on/off. If the throttle/break value is lower than the neutral variable value, meaning the user is breaking, the breaking light is turned on. It is also checked if user turned on/off any of the lights manually.

Every time the values are received successfully, the time of completion is recorded and used to check the elapsed time between successful transmissions. If the signal is lost or jammed for more than 500ms, the boards sets throttle to neutral, the lights are turned off and the transceiver is reset in order to try to reconnect to the remote control.

After every successful transmission, the voltage at the analog input, connected to the voltage divider, is read, to determine the voltage of the skateboard battery pack and the value is then sent as a custom ACK payload to the remote control.

b) Remote control Arduino

Every 20ms user inputs are read using readLightButton() and readCruiseButton() functions. The values are written to the data structure "data" and sent to the receiver. It is then expected to receive an ACK response from the board, with the battery pack voltage as a custom payload. The voltage is displayed on the remote control using TODO.

The readLightButton() is used to check the "C" button state. Every time the button is pressed, another lights combination is chosen. User can choose between 3 combinations: only back light on, both lights on and both lights off.

The readCruiseButton() is used to check the "Z" button state. The button is used as a cruise control toggle. When the button is released, the joystick value is read, but if the button is being held, the joystick value is not read, and the previouis value is being sent to the receiver inside the skateboard.

TODO led lučke funkcija

Clone this wiki locally