Skip to content

Commit

Permalink
Some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
eviallet committed May 21, 2019
1 parent 9a2e76f commit a00b81d
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 13 deletions.
4 changes: 2 additions & 2 deletions AIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace myRIO {
BO0 = AOB_0VAL,
BO1 = AOB_1VAL,
CO0 = AOC_0VAL,
CO1 = AOC_0VAL
CO1 = AOC_1VAL
};

/** Analog inputs ports */
Expand All @@ -30,7 +30,7 @@ namespace myRIO {
BI2 = AIB_2VAL,
BI3 = AIB_3VAL,
CI0 = AIC_0VAL,
CI1 = AIC_0VAL
CI1 = AIC_1VAL
};


Expand Down
2 changes: 1 addition & 1 deletion Encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void Encoder::direction(bool &dir) {
* Start a thread that will monitor the encoder register.
* It will alert the user if a given threshold is reached, executing the given function.
* The threshold is a variation since the last threshold reach (or 0 if none have been reached)
* 1 increment ~ 0.58°, @see MotorPID.toAngularSpeed
* 1 increment ~ 0.58°, @see MotorPID::toAngularSpeed
* @param func a function to execute when threshold is reached
* @param threshold the number of increments before executing the function
*/
Expand Down
35 changes: 27 additions & 8 deletions PWM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ extern NiFpga_Session myrio_session;
*@param out the pin where the PWM signal will be created (PWM0, PWM1)
*@param frequency the frequency of the PWM signal
*@param duty_cycle the duty cycle of the signal; for 50%, duty_cycle = 50
*@param prescaler the clock prescaler. Must be a power of 2 between 1 and 64.
*/
PWM::PWM(uint32_t out, long frequency, double duty_cycle) : out(out), dutyCycle(duty_cycle) {
PWM::PWM(uint32_t out, double frequency, double duty_cycle, short prescaler) : out(out), dutyCycle(duty_cycle), frequency(frequency), prescaler(prescaler) {
uint8_t select, pin;
// Activate the PWM signals in the selected pin
if(out == PWMA_0CNFG || out == PWMA_1CNFG || out == PWMA_2CNFG) {
Expand Down Expand Up @@ -42,8 +43,6 @@ PWM::PWM(uint32_t out, long frequency, double duty_cycle) : out(out), dutyCycle(
return;
}

max = (long)40e6/(32*frequency)-1; //Maximum value that the PWM counts

// Selection of the needed PWM registers (can be configured as needed)
if(out == PWMA_0CNFG){
outcs = PWMA_0CS;
Expand Down Expand Up @@ -93,20 +92,40 @@ PWM::PWM(uint32_t out, long frequency, double duty_cycle) : out(out), dutyCycle(
NiFpga_WriteU8(myrio_session, out, 0b100)); //PWM generation mode , not inverted (can be configured as needed)
NiFpga_MergeStatus(&status,
NiFpga_WriteU8(myrio_session, outcs, 0b110)); // Clock divider : 32 (can be configured as needed)
NiFpga_MergeStatus(&status,
NiFpga_WriteU16(myrio_session, outmax, max));

setDutyCycle(duty_cycle);

setPrescaler(prescaler); // will also set the frequency and the duty cycle
}

/** Set Duty Cycle
*@param dutyCycle set the duty cycle
/** Set the Duty Cycle
*@param dutyCycle the duty cycle to set
*/
void PWM::setDutyCycle(double dutyCycle) {
uint16_t cmp = round((double)max*dutyCycle/100.0);
status = NiFpga_WriteU16(myrio_session, outcmp, cmp);
}

/** Set the Frequency
* Will try to keep the current duty cycle.
*@param frequency the frequency to set
*/
void PWM::setFrequency(double frequency) {
this->frequency = frequency;
max = (long)40e6/(prescaler*frequency)-1; //Maximum value that the PWM counts
NiFpga_MergeStatus(&status,
NiFpga_WriteU16(myrio_session, outmax, max));
setDutyCycle(dutyCycle);
}

/** Set the clock prescaler
* Will try to keep the old frequency.
*@param clockDiv the prescaler. Must be 1, 2, 4, 8, 16, 32 or 64
*/
void PWM::setPrescaler(short clockDiv) {
prescaler = clockDiv;
setFrequency(frequency);
}

/**Destructor
* Desactivate the channel
*/
Expand Down
6 changes: 5 additions & 1 deletion PWM.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,18 @@ namespace myRIO {
*/
class PWM {
public:
PWM(uint32_t out, long frequency, double duty_cycle);
PWM(uint32_t out, double frequency, double duty_cycle, short prescaler = 1);
void setDutyCycle(double dutyCycle);
void setPrescaler(short prescaler);
void setFrequency(double frequency);
~PWM();
private:
uint32_t out; /**< Pin PWM*/
uint16_t max; /**< Max value that the PWM counts
* the counter that generates the PWM signal counts up to the max value to generate the signal that we want*/
double dutyCycle; /**< PWM Duty Cycle*/
double frequency; /**< PWM Frequency */
uint16_t prescaler; /**< PWM Clock prescaler */
uint32_t outcs; /**< PWM Clock Select register*/
uint32_t outmax; /**< PWM Max register*/
uint32_t outcmp; /**< PWM Compare register*/
Expand Down
8 changes: 7 additions & 1 deletion Wifi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,36 @@ void Wifi::openServer() {
short val;
char buffer[2];

// opening a server : AF_INET (IPv4), SOCK_STREAM (TCP)
int err, opt = 1;
int server_fd = socket(AF_INET, SOCK_STREAM, 0);
if(server_fd==0) { perror("socket"); return; }

err = setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_KEEPALIVE, &opt, sizeof(opt));
if(err) { perror("setsockopt"); return; }

// defining server address and port
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(50000);

// binding the server address
err = bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr));
if(err<0) { perror("bind"); return; }

// listening for incoming connections : blocking
err = listen(server_fd, 10);
if(err<0) { perror("listen"); return; }

do {
val = 0;

struct sockaddr client_addr = {0};
socklen_t client_addr_size = sizeof(client_addr);
std::cout << "Wifi - Waiting for connection" << std::endl;

// wait for a client connection
_socket = accept(server_fd, &client_addr, &client_addr_size);
if(_socket==-1) { perror("accept"); return; }

Expand Down
4 changes: 4 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ int main() {
if(!myRIO_init()) {cout << "Error initializing myRIO"; return -1;}


PWM p(PWMA0, 10e3, 40);
Time::wait_s(10);


return 0;
}

0 comments on commit a00b81d

Please sign in to comment.