Skip to content

Commit

Permalink
timer improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
melazarus committed May 11, 2022
1 parent c6862dd commit 9f6fa64
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 3 deletions.
99 changes: 99 additions & 0 deletions Documentation/Communication.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# Communication over the wire and IR

## Infrared
Infrared data is transmitted on 940 nm with a 38khz carrier wave.

## Protocol
IR and badgelink share the same protocol. The protocol they use is the standard JVC IR protocol. https://www.sbprojects.net/knowledge/ir/jvc.php
This protocol is simple to implement and gives us 16 bits of data.

The implementation on the Time Blaster is compatible with the JVC protocol but has a few small differences:
- Receiving/decoding only measure the full pulse (HLH) length. This is possible because the JVC protocol has unique pulse lengths for each type of signal (Start, 0, 1)
- Data is not split into a Address and command area but in a data(12bit) and CRC(4bit) area.

# Data

## data packet
The Time Blaster uses this 16 bit packet for all communication between blasters and also to and from the badge.

| Bit | Name | Use |
| ---------: |-------------| -----|
| 0 | Team Color Red | Set/get blaster team color |
| 1 | Team Color Green | |
| 2 | Team Color Blue | |
| 3 | Trigger Status | Indicate if a shot was fired <br> also used for disabling the trigger |
| 4 | Command_0 | Send commands like animations |
| 5 | Command_1 | |
| 6 | Command_2 | |
| 7 | Command_3 | |
| 8 | Parameter_0 | 4 parmeter bits for the command given |
| 9 | Parameter_1 | |
| 10 | Parameter_2 | |
| 11 | Parameter_3 | |
| 12 | CRC_0 | Checksum for data validation |
| 13 | CRC_1 | |
| 14 | CRC_2 | |
| 15 | CRC_3 | |

Depending on the source of the packet the blaster allows or rejects some commands.

## Team Color

### Badge to Blaster
000 : team is set by the hardware switch.
All other: team is set by this.

### Blaster to Badge
Indicates the current selected team. The can be the team selected by switch or the team set by the badge in an earlier packet.
value 000 is not allowed.

### Blaster to Blaster
Carries the team color of the blaster that fired.
value 000 is not allowed.

## Trigger Status

### Badge to Blaster
0: Trigger on the blaster is disabled. Team LED is turned off
1: Trigger on the blaster is enabled. Team LED is turned on

### Blaster to Badge
1: Blaster just fired a pulse
0: All other cases

### Blaster to Blaster
Always 1

## Command and Parameters

To be completed.

The blaster can send and receive commands. Each command can have a 4 bit parameter.

Some examples:
* Shoot
* Heal
* Set IR Channel
* Set Fire Type (Damage|Healing)
* Set Game mode (Timeout when hit, zombie mode, slave(badge controlled))
* Got Hit
* Play Animation
* Team Switch Changed
* Chatter (Blasters start a short "discussion" about the meaning of life)
* Pull trigger (force blaster to shoot)
* Blaster ready

## CRC
This is the calculation used to get the check.

d0..d11 are the bits in position 0..11
the 4 crc bits will be added so crc_0 will be bit_12, crc_1 => bit_13 etc.

```c++
crc[0] = d11 ^ d10 ^ d9 ^ d8 ^ d6 ^ d4 ^ d3 ^ d0;
crc[1] = d8 ^ d7 ^ d6 ^ d5 ^ d3 ^ d1 ^ d0 ^ 1;
crc[2] = d9 ^ d8 ^ d7 ^ d6 ^ d4 ^ d2 ^ d1 ^ 1;
crc[3] = d10 ^ d9 ^ d8 ^ d7 ^ d5 ^ d3 ^ d2;
```

Applying the same function to a packet with a CRC the CRC should become 0 if no corruption was detected.
8 changes: 5 additions & 3 deletions Firmware/Firmware.ino
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,17 @@ void transmit_raw(unsigned int data, bool ir_out, bool badgelink_out)
void setup_ir_carrier()
{
/* Setup Timer 1 to toggle the IR-LED D1 at 38khz
Note: this will generate a wave at 38.5khz due to limitations of this timer.
This will generate a wave of approximately 38.1khz.
http://www.8bit-era.cz/arduino-timer-interrupts-calculator.html
*/
cli(); // Stop interrupts while we set up the timer
TCCR1B = B00000000; // Stop Timer/Counter1 clock by setting the clock source to none.
TCCR1A = B00000000; // Set Timer/Counter1 to normal mode.
TCNT1 = 0; // Set Timer/Counter1 to 0
OCR1A = 25; // Set the Output Compare A for Timer/Counter1, 100 Hz
OCR1A = 209; // = 16000000 / (1 * 76190.47619047618) - 1 (must be <65536)
TCCR1A = B01000100; // Set Timer/Counter1 to CTC mode. Set OC1A to toggle.
TCCR1B = B00001010; // Start Timer/Counter1 clock by setting the source to CPU source. Set prescalar to 1/8 (2Mhz).
TCCR1B |= (1 << WGM12);
TCCR1B |= (0 << CS12) | (0 << CS11) | (1 << CS10);
sei(); // allow interrupts
}

Expand Down

0 comments on commit 9f6fa64

Please sign in to comment.